Math: Cannot add two 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
User avatar
Lezalith
Regular
Posts: 82
Joined: Mon Dec 21, 2015 6:45 pm
Contact:

Math: Cannot add two variables

#1 Post by Lezalith » Thu Dec 24, 2015 7:28 pm

Code: Select all

$ InTeaching = "Skill1"
$ Skill1Value = 1
$ Skill1ValueOld = 0
$ MoodAngry = 4
$ MoodHappy = -2
$ MoodAffection = 0
$ TeachingMorning = 0

if InTeaching == "Skill1":
        $ Skill1ValueOld = Skill1Value
        $ MoodAffection = (MoodAngry + MoodHappy)
        $ TeachingMorning = [(MoodAffection + 6)*1.66667]
        $ Skill1Value = (Skill1ValueOld + TeachingMorning)
    else:
        pass
I do not get what isn't understandable here. First, Skill1Value is reflected into Skill1ValueOld. Then, MoodAffection is counted (=2), is calculated in TeachingMorning(=13,33336), and Skill1Value is then calculated (=14,33336).

I get error on the "$ Skill1Value = (Skill1ValueOld + TeachingMorning)" line. TypeError: unsupported operand type(s) for +: 'int' and 'RevertableList'.
What I got from that is, either Skill1ValueOld or TeachingMorning is not an integer value, but when I remove their addition, run it and check developer menu, both of them integers are.
And that line is written the exactly same way as addition of MoodAngry and MoodHappy, and I don't get errors there! What the hell am I doing wrong?!

User avatar
Zetsubou
Miko-Class Veteran
Posts: 513
Joined: Wed Mar 05, 2014 1:00 am
Completed: See my signature
Github: koroshiya
itch: zetsuboushita
Contact:

Re: Math: Cannot add two variables

#2 Post by Zetsubou » Thu Dec 24, 2015 7:35 pm

The problem is this line: $ TeachingMorning = [(MoodAffection + 6)*1.66667]
Those square brackets mean TeachingMorning is a list.
So remove the square brackets, round the result (it'll be a float) to int (might need to import the math module), then you'll be able to add them.

Unrelated, but your else clause needs to be indented to the same level as the if statement.
Finished games
-My games: Sickness, Wander No More, Max Massacre, Humanity Must Perish, Tomboys Need Love Too, Sable's Grimoire, My Heart Grows Fonder, Man And Elf, A Dragon's Treasure
-Commissions: No One But You, Written In The Sky, Diamond Rose, To Libertad, Catch Canvas, Love Ribbon, Happy Campers, Wolf Tails

Working on:
Sable's Grimoire 2

Image

Onishion
Veteran
Posts: 295
Joined: Mon Apr 20, 2015 10:36 am
Contact:

Re: Math: Cannot add two variables

#3 Post by Onishion » Fri Dec 25, 2015 6:52 am

Yeah, just do

Code: Select all

        $ TeachingMorning = int((MoodAffection + 6)*1.66667)
Also, in that context, you wouldn't need to establish "Skill1ValueOld," this would work just as well:

Code: Select all

        
        $ Skill1Value = Skill1Value + TeachingMorning
It won't get confused by that. You could even just make it a single line if you prefer,

Code: Select all

        
        $ Skill1Value = Skill1Value + (int((MoodAngry + MoodHappy+ 6)*1.66667)) 
Might be an unnecessary parentheses in there, I tend to err on the side of caution. In any case, that would work, unless you intended to use those other variables someplace else.

Oh, and also, if you're initializing variables in that first bit, it's a good idea to instead use

Code: Select all

default Skill1Value = 1
Using the default thing is a good idea, because it means that if the variable does not exist, even if they are loading a save from the middle of the game, it'll be initialized to that value.

Post Reply

Who is online

Users browsing this forum: Google [Bot], _ticlock_