(SOLVED) Determining and Modifying Information Set In Variables

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
ArcialIntegra
Regular
Posts: 53
Joined: Mon Nov 13, 2017 12:10 am
Contact:

(SOLVED) Determining and Modifying Information Set In Variables

#1 Post by ArcialIntegra » Mon Nov 13, 2017 12:53 am

So, this will be a bit of a strange question, but for a project I'm in, I'm trying to come up with a way of determining specific information found in variables. Let me explain what I mean.

Let's say I have a variable:

Code: Select all

 $ variable = "abcdefgabc" 
How can I determine how many instances of the letter "a" appear within my "$ variable"?
Additionally, how can I modify "$ variable" so that it no longer is "abcdefgabc", but is now "cde" WITHOUT manually overwriting it by outright saying:

Code: Select all

 $ variable = "cde" 
An example would be this: I want to make it so the player can choose the character's name. I know how to do this, how to use name.strip() to remove excess spaces from the beginning and end of the entry, and how to concatenate a first name entry and last name entry into being a single player name variable as well via the following code. (In this code, assume the player used text entry to choose the name "Johnathon Smith".)

Code: Select all

$ firstname = "Johnathon"
$ lastname = "Smith"
$ fullname = firstname + " " + lastname
But, lets say I wanted to make it so Tina will only refer to Smith by the first 4 letters of his first name, "John". Now, I know I can manually force the name to be John, and problem solved, but I want this to be a sort of... "customizable" nickname of sorts. Of course, I understand "$ TinaCalls" would have to be it's own variable to refer to in order to keep the script "clean", but how would I generate this variable's value?

Then, let's say I want Millie to refer to Smith by the 4th through 7th letters, resulting in a nickname of "Nath". In theory, this seems like it'd work similarly to with Tina, Create a variable ("$ MillieCalls") and have the output of "nath" be it's value, but this then has an issue... "nath" and "Nath" are not the same. The first letter needs capitalized. So, to do this, I would need a way of "viewing" the first symbol in the variable's value (the "n" in "nath"), copying it into its own variable ($ symbol1 = "n"), removing the "n" from "nath", having the program replace the lowercase "n" with an uppercase "N" (See: Code Block A), then having the program concatenate "N" with "ath" (See: Code Block B).

Code: Select all

### CODE BLOCK A
if $ symbol1 == "a":
	$ symbol2 = "A"
if $ symbol1 == "b":
	$ symbol2 = "B"
if $ symbol1 == "c":
	$ symbol2 = "C"
## . . .
if $ symbol1 == "n":
	$ symbol2 = "N"
## . . .
(In between these 2 codes, I'm assuming the lowercase "n" in "$ MillieCalls = 'nath' " is already deleted so that "$ MillieCalls" now equals "ath".)

Code: Select all

### CODE BLOCK B
## MillieCalls should equal "ath" presuming the name "Johnathon" was chosen.
$ MillieCalls = symbol2 + MillieCalls
Additionally, if the player name is too short for Millie to turn into a nickname, I want her to then just refer to them by their first name, in this hypothetical. I presume I would do this by using an if statement, but how would I get Ren'py to count the number of symbols in a variable?

I know the rule is one question per thread. Maybe you'll see this differently, but I see this as one question: "How to manipulate variable values?" I am trying to make this project so that there is a degree of personality behind it to help with immersion and to give a bit of quirkiness to the characters. While this isn't exactly how the code will be used, this gives an accurate idea of what I need the code to do so that I can get my project going the way I want it to. I don't want to give specifics on my project because I don't want to announce progress on something that may ultimately fail to be completed for one reason or another, but I know I could use this information in any project I work on and I am sure it could help others. I'm sorry for the weird question, but I hope you understand!

Sorry for the inconvenience

~Arcial
Last edited by ArcialIntegra on Mon Nov 13, 2017 1:55 pm, edited 1 time in total.

User avatar
Divona
Miko-Class Veteran
Posts: 678
Joined: Sun Jun 05, 2016 8:29 pm
Completed: The Falconers: Moonlight
Organization: Bionic Penguin
itch: bionicpenguin
Contact:

Re: Determining and Modifying Information Set In Variables

#2 Post by Divona » Mon Nov 13, 2017 1:40 am

A read on Python Strings should help take you towards the right direction.
Completed:
Image

ArcialIntegra
Regular
Posts: 53
Joined: Mon Nov 13, 2017 12:10 am
Contact:

Re: Determining and Modifying Information Set In Variables

#3 Post by ArcialIntegra » Mon Nov 13, 2017 2:04 am

Divona wrote:
Mon Nov 13, 2017 1:40 am
A read on Python Strings should help take you towards the right direction.
I see. That actually helps me out a lot, thanks! :)

However, I seem not to be able to find anything on counting the number of symbols in a string, only on counting how many times a specific symbol appears in a string. Is there a way to modify that functionality? Other than this issue, the read on PythonStrings answered... I think it answered everything else actually. I just need this. Please for the help and thank you for all the help given. :)

User avatar
Divona
Miko-Class Veteran
Posts: 678
Joined: Sun Jun 05, 2016 8:29 pm
Completed: The Falconers: Moonlight
Organization: Bionic Penguin
itch: bionicpenguin
Contact:

Re: Determining and Modifying Information Set In Variables

#4 Post by Divona » Mon Nov 13, 2017 4:53 am

Counting symbols (characters?) in a string would be len().
Completed:
Image

ArcialIntegra
Regular
Posts: 53
Joined: Mon Nov 13, 2017 12:10 am
Contact:

Re: Determining and Modifying Information Set In Variables

#5 Post by ArcialIntegra » Mon Nov 13, 2017 1:55 pm

Oh! I'm so blind! I can't believe I missed that! Well, then... I guess my question is answered. Thank you. :)

Post Reply

Who is online

Users browsing this forum: No registered users