Imagebutton action

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
iDweadith
Regular
Posts: 63
Joined: Sun Mar 01, 2020 4:15 pm
Contact:

Imagebutton action

#1 Post by iDweadith » Wed May 20, 2020 6:06 am

Hi guys

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

User avatar
gas
Miko-Class Veteran
Posts: 759
Joined: Mon Jan 26, 2009 7:21 pm
Contact:

Re: Imagebutton action

#2 Post by gas » Wed May 20, 2020 7:11 am

Code: Select all

imagebutton auto "whatever_%s.png" action SetVariable("thevariable", thevariable+1)
If you want to debate on a reply I gave to your posts, please QUOTE ME or i'll be not notified about.

10 ? "RENPY"
20 GOTO 10

RUN

User avatar
Remix
Eileen-Class Veteran
Posts: 1505
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Imagebutton action

#3 Post by Remix » Wed May 20, 2020 7:38 am

action SetVariable("the_variable", the_variable + 1)

remembering to default the variable before using it

default the_variable = 5
Frameworks & Scriptlets:

User avatar
iDweadith
Regular
Posts: 63
Joined: Sun Mar 01, 2020 4:15 pm
Contact:

Re: Imagebutton action

#4 Post by iDweadith » Wed May 20, 2020 8:43 am

Ok thank guys,

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

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

User avatar
isobellesophia
Miko-Class Veteran
Posts: 979
Joined: Mon Jan 07, 2019 2:55 am
Completed: None
Projects: Maddox and Friends! (AI Teacher friend), Friendly Universities! (Soon)
Organization: Friendly Teachers series
Deviantart: SophBelle
itch: Child Creation
Location: Philippines, Mindanao
Contact:

Re: Imagebutton action

#5 Post by isobellesophia » Wed May 20, 2020 8:49 am

I think you need to define first.

Code: Select all

$ thevariable = "hello"
I am a friendly user, please respect and have a good day.


Image

Image


User avatar
isobellesophia
Miko-Class Veteran
Posts: 979
Joined: Mon Jan 07, 2019 2:55 am
Completed: None
Projects: Maddox and Friends! (AI Teacher friend), Friendly Universities! (Soon)
Organization: Friendly Teachers series
Deviantart: SophBelle
itch: Child Creation
Location: Philippines, Mindanao
Contact:

Re: Imagebutton action

#6 Post by isobellesophia » Wed May 20, 2020 8:55 am

One thing i forgot, it must be..

Code: Select all

$ thevariable += 1
I am a friendly user, please respect and have a good day.


Image

Image


User avatar
gas
Miko-Class Veteran
Posts: 759
Joined: Mon Jan 26, 2009 7:21 pm
Contact:

Re: Imagebutton action

#7 Post by gas » Wed May 20, 2020 9:05 am

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.
If you want to debate on a reply I gave to your posts, please QUOTE ME or i'll be not notified about.

10 ? "RENPY"
20 GOTO 10

RUN

User avatar
iDweadith
Regular
Posts: 63
Joined: Sun Mar 01, 2020 4:15 pm
Contact:

Re: Imagebutton action

#8 Post by iDweadith » 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

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 549
Joined: Fri Jul 12, 2019 5:21 am
Projects: The Button Man
Organization: NILA
Github: hell-oh-world
Location: Philippines
Contact:

Re: Imagebutton action

#9 Post by hell_oh_world » Wed May 20, 2020 11:45 am

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()
		)
	)

User avatar
gas
Miko-Class Veteran
Posts: 759
Joined: Mon Jan 26, 2009 7:21 pm
Contact:

Re: Imagebutton action

#10 Post by gas » Wed May 20, 2020 11:48 am

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")
If you want to debate on a reply I gave to your posts, please QUOTE ME or i'll be not notified about.

10 ? "RENPY"
20 GOTO 10

RUN

User avatar
iDweadith
Regular
Posts: 63
Joined: Sun Mar 01, 2020 4:15 pm
Contact:

Re: Imagebutton action

#11 Post by iDweadith » Wed May 20, 2020 12:46 pm

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)

User avatar
gas
Miko-Class Veteran
Posts: 759
Joined: Mon Jan 26, 2009 7:21 pm
Contact:

Re: Imagebutton action

#12 Post by gas » Wed May 20, 2020 4:24 pm

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.
If you want to debate on a reply I gave to your posts, please QUOTE ME or i'll be not notified about.

10 ? "RENPY"
20 GOTO 10

RUN

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Imagebutton action

#13 Post by trooper6 » Wed May 20, 2020 5:27 pm

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.
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

User avatar
gas
Miko-Class Veteran
Posts: 759
Joined: Mon Jan 26, 2009 7:21 pm
Contact:

Re: Imagebutton action

#14 Post by gas » Thu May 21, 2020 7:30 am

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.
If you want to debate on a reply I gave to your posts, please QUOTE ME or i'll be not notified about.

10 ? "RENPY"
20 GOTO 10

RUN

User avatar
iDweadith
Regular
Posts: 63
Joined: Sun Mar 01, 2020 4:15 pm
Contact:

Re: Imagebutton action

#15 Post by iDweadith » Thu May 21, 2020 11:53 am

You're so good guys thank you again!!!

Post Reply

Who is online

Users browsing this forum: Google [Bot], hell_oh_world, sdx231