Need help with SetVariable [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
User avatar
GeeSeki
Regular
Posts: 112
Joined: Sat Dec 17, 2016 3:39 am
Projects: A Town Uncovered
itch: geeseki
Contact:

Need help with SetVariable [SOLVED]

#1 Post by GeeSeki »

Code: Select all

imagebutton auto "btn bag_cheat_plus_%s" xpos 0 ypos 0 focus_mask True action SetVariable("inventory.money", If(inventory.money<9001, inventory.money+50, 9001))
The error above says 'Store Module' object has no attribute 'inventory.money'

Code: Select all

imagebutton auto "btn bag_cheat_plus_%s" xpos 0 ypos 0 focus_mask True action SetVariable("inventory.money", If([inventory.money]<9001, [inventory.money]+50, 9001))
and

Code: Select all

imagebutton auto "btn bag_cheat_plus_%s" xpos 0 ypos 0 focus_mask True action SetVariable("[inventory.money]", If([inventory.money]<9001, [inventory.money]+50, 9001))
The errors above says TypeError: can only concatenate list (not "int") to list

I don't know how it should be written
Last edited by GeeSeki on Thu Oct 19, 2017 1:29 am, edited 1 time in total.

User avatar
Belgerum
Regular
Posts: 110
Joined: Thu Nov 06, 2014 12:24 am
Skype: belgerum09
Soundcloud: Belgerum
itch: Belgerum
Contact:

Re: Need help with SetVariable

#2 Post by Belgerum »

According to renpy documentation, SetVariable does not take conditionals. It only takes a variable name and a value to set.

In this case, the desired action can be done with a function.

First, define the function somewhere:

Code: Select all

init python:
    def add_money():
        inventory.money += 50
        if inventory.money > 9001:
            inventory.money = 9001
and then, in the screen:

Code: Select all

imagebutton auto "btn bag_cheat_plus_%s" xpos 0 ypos 0 focus_mask True action Function(add_money)

philat
Eileen-Class Veteran
Posts: 1909
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Need help with SetVariable

#3 Post by philat »

Couple things: 1. You're looking for SetField, since you're trying to change a field in a class object, not a simple variable. 2. There are multiple ways to do what you want in terms of setting boundaries for values. The simplest is using min/max functions.

Code: Select all

imagebutton auto "btn bag_cheat_plus_%s" xpos 0 ypos 0 focus_mask True action SetField(inventory, "money", min(9001, inventory.money+50))

User avatar
GeeSeki
Regular
Posts: 112
Joined: Sat Dec 17, 2016 3:39 am
Projects: A Town Uncovered
itch: geeseki
Contact:

Re: Need help with SetVariable

#4 Post by GeeSeki »

philat wrote: Thu Oct 19, 2017 1:09 am Couple things: 1. You're looking for SetField, since you're trying to change a field in a class object, not a simple variable. 2. There are multiple ways to do what you want in terms of setting boundaries for values. The simplest is using min/max functions.

Code: Select all

imagebutton auto "btn bag_cheat_plus_%s" xpos 0 ypos 0 focus_mask True action SetField(inventory, "money", min(9001, inventory.money+50))
Thank you so much, this is exactly what I needed and it worked!

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot]