How to make one variable x different from a previously defined one?

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
Chungfriend
Newbie
Posts: 7
Joined: Sat May 23, 2020 8:52 pm
Contact:

How to make one variable x different from a previously defined one?

#1 Post by Chungfriend »

As an example, trying to make one person's age 2 years older than a different person to give freedom but prevent the user from making other dialogue not make any sense.

Code: Select all

$ 1_age = renpy.input("I am __ years old. (enter your age.)(default = 19)")
    if 1_age == "":
        $ 1_age = "19"
Works perfectly for defining the first age.

Then I figured I would be able to do:

Code: Select all

$ 2_age = 1_age
$ 2_age += 2
But it doesn't work.

Code: Select all

TypeError: unsupported operand type(s) for +=: 'unicode' and 'int'
I'm assuming it's something really simple I'm doing wrong but I've tried searching and the stuff people are having trouble with is way more complicated than what I'm trying to do so I haven't been able to find anything.

Also tried:

Code: Select all

$ 2_age = '1_age'
$ 2_age += 2

Code: Select all

$ 2_age = '[1_age]'
$ 2_age += 2
Thanks!

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Contact:

Re: How to make one variable x different from a previously defined one?

#2 Post by hell_oh_world »

Eh... You can't use number as a starting character for variable names or include special characters in its name such as `@`. That very basic foundation is found in almost all of the programming languages, not just python.

Code: Select all

default a1 = 0					# always default your variables and don't put numbers on the very first letter of your variable name... you can put the number anywhere not just as the first character...
default a2 = 0
label start:
    $ age = renpy.input("Age?", allow="0123456789").strip()
    $ a1 = int(age) if age else 19		# defaults to 19 if the age entered is falsy / an empty string
    $ a2 = a1 + 2
    "A1: [a1], A2: [a2]"

Chungfriend
Newbie
Posts: 7
Joined: Sat May 23, 2020 8:52 pm
Contact:

Re: How to make one variable x different from a previously defined one?

#3 Post by Chungfriend »

hell_oh_world wrote: Sat May 23, 2020 9:37 pm Eh... You can't use number as a starting character for variable names or include special characters in its name such as `@`. That very basic foundation is found in almost all of the programming languages, not just python.

Code: Select all

default a1 = 0					# always default your variables and don't put numbers on the very first letter of your variable name... you can put the number anywhere not just as the first character...
default a2 = 0
label start:
    $ age = renpy.input("Age?", allow="0123456789").strip()
    $ a1 = int(age) if age else 19		# defaults to 19 if the age entered is falsy / an empty string
    $ a2 = a1 + 2
    "A1: [a1], A2: [a2]"
Thank you, but the real variables do not start with numbers, they are mc_name and ch_name. I just changed it for the sake of the example. I can copy paste your code but don't understand why it works and mine does not. Other than being more idiot proof with what I imagine the strip does and the capitalization thing it seems like it's almost the same as things I have tried.

I have no programming or game development experience whatsoever and am just doing this for fun so perhaps I should just keep it simple and get the player to enter the second age and tell them why it should be 2 years greater than the previous one.

EDIT:

Nevermind, I got it to work. I guess the secret is converting / making the engine realize the variable is an integer. I kept my same code and added.

Code: Select all

    $ mc_age = int(mc_age)
    $ ch_age = mc_age + 2
and it worked.

Thank you!

Post Reply

Who is online

Users browsing this forum: chesarty