Page 1 of 1

Imagebutton action

Posted: Wed May 20, 2020 6:06 am
by iDweadith
Hi guys

How to give +1 to a variable when I click on an imagebutton?

Re: Imagebutton action

Posted: Wed May 20, 2020 7:11 am
by gas

Code: Select all

imagebutton auto "whatever_%s.png" action SetVariable("thevariable", thevariable+1)

Re: Imagebutton action

Posted: Wed May 20, 2020 7:38 am
by Remix
action SetVariable("the_variable", the_variable + 1)

remembering to default the variable before using it

default the_variable = 5

Re: Imagebutton action

Posted: Wed May 20, 2020 8:43 am
by iDweadith
Ok thank guys,

But what if I want to use a check list, like:

if thevariable == "hello":
thevariable += 1
elif thevariable == "pizza":
thevariable += 2

Re: Imagebutton action

Posted: Wed May 20, 2020 8:49 am
by isobellesophia
I think you need to define first.

Code: Select all

$ thevariable = "hello"

Re: Imagebutton action

Posted: Wed May 20, 2020 8:55 am
by isobellesophia
One thing i forgot, it must be..

Code: Select all

$ thevariable += 1

Re: Imagebutton action

Posted: Wed May 20, 2020 9:05 am
by gas
In that specific case...

Code: Select all

imagebutton auto "whatever_%s.png" action If('the_thing == "hello"' , true = [SetVariable("the_variable", thevariable+1)], false = [SetVariable("the_variable", the_variable+2)])
This add +1 if 'the_thing' is hello, and +2 any other case.

Mind that this work only for a couple of values.

If you want for a larger set of options, you should probably do this:

Code: Select all

if the_thing == "hello":
    imagebutton auto "whatever_%s.png" action SetVariable("the_variable", the_variable+1)
elif the_thing == "Gas":
    imagebutton auto "whatever_%s.png" action SetVariable("the_variable", the_variable+10)
else:
    imagebutton auto "whatever_%s.png" action SetVariable("the_variable", the_variable+3)
Using if... elif... else.. allow you to show one imagebutton (at the same position) with a different effect based on a condition.

Re: Imagebutton action

Posted: Wed May 20, 2020 11:12 am
by iDweadith
Thanks guys!

Yes! This is it

imagebutton auto "whatever_%s.png" action If('the_thing == "hello"' , true = [SetVariable("the_variable", thevariable+1)], false = [SetVariable("the_variable", the_variable+2)])

but how to add more elif in this line?

In my case I can't use the second example you did Gas

Because I have an inventory with items, and every item is an imagebutton

Re: Imagebutton action

Posted: Wed May 20, 2020 11:45 am
by hell_oh_world
just use another if action for the false action...

Code: Select all

button:
	action If(
		var == 1,
		SetVariable("var", var + 1), # true action
		If( # false action
			var == 2,
			SetVariable("var", var + 2), # will happen if var == 2
			NullAction()
		)
	)

Re: Imagebutton action

Posted: Wed May 20, 2020 11:48 am
by gas
iDweadith wrote: Wed May 20, 2020 11:12 am Thanks guys!

Yes! This is it

imagebutton auto "whatever_%s.png" action If('the_thing == "hello"' , true = [SetVariable("the_variable", thevariable+1)], false = [SetVariable("the_variable", the_variable+2)])

but how to add more elif in this line?

In my case I can't use the second example you did Gas

Because I have an inventory with items, and every item is an imagebutton
Well, if you've started with the full problem since the beginning that helped a lot :).

So, your last problem is you have a number of items in an inventory, and each one when pressed do something?
OR
you have a number of items in an inventory and each one can possibly do TWO OR MORE things based on a condition?

You can safely explain what exactly you want to obtain, and probably we can come with a wholesome solution.
(So, for example, "It's an inventory of digesters, and the effect of items in the menu change if the player previously eaten a biscuit, a cake or an icecream")

Re: Imagebutton action

Posted: Wed May 20, 2020 12:46 pm
by iDweadith
Thank you guys you're amazing I solved with this:

imagebutton auto "whatever_%s.png" action SetVariable("thevariable", thevariable+1)

That was easier than I thought

I have a last one question what are the words inside the parentheses of the screen used for?

screen tooltip(item = False, seller = false)

Re: Imagebutton action

Posted: Wed May 20, 2020 4:24 pm
by gas
They are parameters that you can use for variables used only inside the screen and you can pass to change the value when you show/call the screen.

Code: Select all

screen myscreen (name = "greg")
    text "[name]"
So you can do...

Code: Select all

show screen myscreen(name = "gas")
and the name 'gas' will be used instead. If you don't pass nothing, 'greg' will be used as default.

Re: Imagebutton action

Posted: Wed May 20, 2020 5:27 pm
by trooper6
isobellesophia wrote: Wed May 20, 2020 8:49 am I think you need to define first.

Code: Select all

$ thevariable = "hello"
Variables should not be defined this way. This will cause problems with save and rollback.
Variables should be defined:

Code: Select all

default thevariable = "hello"
Outside of a code block.

Re: Imagebutton action

Posted: Thu May 21, 2020 7:30 am
by gas
A little tutorial seems necessary at this point :).

VARIABLES are small memory slots with a tag, that can contain things.
So, for example
pocket = 3
The variable pocket now contain the number '3'.

In coding, you usually quote variables instead of actual numbers, like this:

Code: Select all

 $  fruits = apples + pears
Obviously if the code reach this point it does need to 'know' about how much apples and how much pears, or throw you an error 'cause such variables tags doesn't exist.

So, when you should code a function like this, you should be aware that since the beginning of the program there's a defined default value for pears and apples, even if later they surely change. This way even if you don't touch pears and apples, the code can do something anyway.

In renpy you use a special starting statement called 'default'.
So you can begin your script with a number of defaults for variables that you're quite sure to use later.

Code: Select all

default apples = 1
default pears = 2
1 and 2 are the starting values, if you're sure they change you can use placeholder numbers.
Now, even if don't quote anymore these variables, the program know about them and use such default at least.
Using 'default' this way is convenient as it's also compliant with a number of rules for saving your game.

Anyway, you can always create new variables on the fly.
That's mean you can quote them in any other area of the script.

Code: Select all

label start:
    $ peaches = 5
you created a new variable called 'peaches', and you can quote this same variable everywhere else.
ANYWAY you should mind that renpy CANNOT save the variables made this way, so is not a good idea to create variables that will be later used somewhere else, like for example in screens.

If you'll create variables (both by 'default' than $ =) in the normal script, they will be GLOBAL variables.
If you instead create a variable inside a function or screen, is a LOCAL variable and you can't quote it elsewhere.

Code: Select all

screen my_screen():
    $ my_name = 'Elodie'
I can't quote 'my_name' out of this screen as I've created a LOCAL variable.

It's fun to note that GLOBAL and LOCAL variables can have the same names. Python is smart enough to understand if you're quoting a global or a local (in fact, invisibly, while they look the same they are something like
store.my_name
store.my_screen.my_name
so in fact they are 2 different memory slots with just a 'similar' name).

This is a nice example:

Code: Select all

    $ my_name = "Gas"
    show screen my_screen
    e "The name shown is [my_name]!" 
    
screen my_screen():
    $ my_name = 'elodie'
    
What the ouctome will be?
'The name shown is Gas!'
... even if you assigned another value to a variable called 'my_name' in the screen. That's cause the first 'my_name' is GLOBAL, and the second one in the screen is just LOCAL (it does exist only inside the screen context).

Obviously a global variable stay global whatever you do, so maybe you want to change it locally but not globally.

Code: Select all

screen my_screen():
    the_name = my_name
now 'the_name' is a LOCAL copy of the GLOBAL variable 'my_name', and you can use it freely.

Renpy is so nice that allow you to determine the starting value of local variables passing values.
So, for example:

Code: Select all

screen my_screen (name ="Harry")
    $ full_name ="Sir "+name
    text "[full_name]"
So you can pass different values for your local variables and have different effects.
show screen (name = "Mary")
show screen (name = my_name)
show screen (name = "Wolowizt")
they all have 3 different effects, while the screen is practically the same. That avoid you to type a screen for every possible circumstance.


So, in the end:
For global variables (the ones that probably manage the whole game), is better if you create them at the start of the script using 'default' and a placeholder value.
You can create small variables on the fly for some computing, but don't quote them elsewhere (like in screens)
You can quote variables inside screens, but the ones created INSIDE a screen can't be quoted elsewhere
You can pass arguments to a screen to have it process different values each time, making it more flexible.

Re: Imagebutton action

Posted: Thu May 21, 2020 11:53 am
by iDweadith
You're so good guys thank you again!!!