Dynamic Menus: Expected Statement Error? [SOLVED]

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
dramaticaluniweeb
Newbie
Posts: 15
Joined: Sun Jun 12, 2016 1:28 am
Contact:

Dynamic Menus: Expected Statement Error? [SOLVED]

#1 Post by dramaticaluniweeb »

I'm having some issues with my dynamic menus :(

Code: Select all

class Player:
        def __init__(self):
            
            self.stats =  {'money':0}
            self.inventory = {'m_uniform':0, 'a_uniform':0, 'danceuniform':0}
        def change_stat(self,stat,modifier):
            self.stats[stat] = self.stats[stat] + modifier
        def change_inventory(self,item,amount):
            if item in self.inventory:
                self.inventory[item] = self.inventory[item] + amount
                self.inventory[item] = amount
    
    player = Player()
This is my example code for a player class, under init python (there are other stats, but for now, 'money' is the one that's giving me trouble). The thing about this is that I cannot, for the life of me, figure out how to actually make changes to the money stat. Currently, I'm using:

Code: Select all

player.change_stat('money',+10)
But this calls up this error:

Code: Select all

I'm sorry, but errors were detected in your script. Please correct the
errors listed below, and try again.


File "game/script.rpy", line 244: expected statement.
    player.change_stat('money',+10)
                                  ^

Ren'Py Version: Ren'Py 6.99.12.4.2187
I don't know where I went wrong, but because this code is set up like this (with the stats being part of a class), I can't seem to find help on other websites. If anyone could give any advice on why it's calling up an expected statement error, that would be great!
Last edited by dramaticaluniweeb on Wed Jun 20, 2018 10:07 pm, edited 1 time in total.

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: Dynamic Menus: Expected Statement Error?

#2 Post by kivik »

Whenever you see "expected statement" it's usually a syntax error on the line it's pointing to (sometimes it's related to the previous line). In this case it's probably because you're calling the function outside python mode.

If your line isn't inside a python block, you need to use $ before it.

Basically any statements where you're directly interacting with variables, objects, functions etc have to be called as python statements, either with a $ or inside a python: block.


Also, looking at your change_stat function - you don't need to call it with +10, just 10 will do. It's only when it's negative that you want to do -10.

Extended also: the function's a little dangerous in that it doesn't check if what you're passing in actually exists as a key to the dictionary - so it can crash if you make a typo when calling the function. One way to prevent that is to just wrap it inside a python try: block, though at the same time you shouldn't really have to worry since you're in complete control over calling that function (i.e. not basing it on user input)... Just a head's up that's all.

dramaticaluniweeb
Newbie
Posts: 15
Joined: Sun Jun 12, 2016 1:28 am
Contact:

Re: Dynamic Menus: Expected Statement Error?

#3 Post by dramaticaluniweeb »

Putting the command inside a python block fixed it! Thank you for the help!

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]