Making a variable such as "If clicked then.."?

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.
Message
Author
User avatar
GreyWolfXx
Regular
Posts: 68
Joined: Fri Jun 05, 2015 1:00 am
IRC Nick: Grey
Skype: GreyWolfXxII
Contact:

Making a variable such as "If clicked then.."?

#1 Post by GreyWolfXx »

I want to make a variable that will activate if the player clicks a certain thing. More specifically, I want a keymap to activate (change) when the player clicks on a certain thing.
For example: keymap for right-click is default -> player clicks on button -> keymap is changed to something different than default. How would I do that with a variable?
Last edited by GreyWolfXx on Wed Aug 05, 2015 12:04 am, edited 1 time in total.

"We have been to the moon, we have charted the depths of the ocean and the heart of the atom,
but we have a fear of looking inward to ourselves because we sense that is where
all the contradictions flow together." -- Terrence Mckenna

User avatar
orz
Regular
Posts: 126
Joined: Tue Apr 21, 2015 10:19 pm
Contact:

Re: Making a variable such as "If clicked then.."?

#2 Post by orz »

textbutton "Hello this is a test" pos (.5,.5) action SetVariable("myvariablename", 5000)

...or were you thinking of something else?

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: Making a variable such as "If clicked then.."?

#3 Post by trooper6 »

I'm not convinced you need a variable at all.

You just need to put your new key map on a screen and then show the screen with your button. If you want to get fancy, you can set up your button so that it will show the screen if the screen isn't being show and hide it if it is.

Code: Select all

text button "Change Key Bindings" action If(renpy.get_screen("key_screen"), Hide("key_screen"), Show("key_screen"))
That is your button, and your screen has the keybinding for whatever you want your mouse click to do when the screen is shown.
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
GreyWolfXx
Regular
Posts: 68
Joined: Fri Jun 05, 2015 1:00 am
IRC Nick: Grey
Skype: GreyWolfXxII
Contact:

Re: Making a variable such as "If clicked then.."?

#4 Post by GreyWolfXx »

The problem for me is how to actually set it up so that the keymaps change...if that makes sense. The textbutton works though and it's one step closer than before.
Trooper6 is that code just supposed to be copy pasted and added? When I do it gives me an error. Saying that the end of the line wasn't right.

"We have been to the moon, we have charted the depths of the ocean and the heart of the atom,
but we have a fear of looking inward to ourselves because we sense that is where
all the contradictions flow together." -- Terrence Mckenna

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: Making a variable such as "If clicked then.."?

#5 Post by trooper6 »

Where are you putting the code?

If you want to have a screen element visible, in this case a text button, you need to put it on a screen and then show that screen.
So, you make a screen that has your "Change Key Bindings" button.
You show the screen.
The user presses the button and it shows your key binding screen and changes the key binding. The user presses the button again and the screen is hidden and everything goes back to the original key binding.

Have you read the section on screen language in the documentation? It is very useful and an important foundation for doing lots of things you might want to do.
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
GreyWolfXx
Regular
Posts: 68
Joined: Fri Jun 05, 2015 1:00 am
IRC Nick: Grey
Skype: GreyWolfXxII
Contact:

Re: Making a variable such as "If clicked then.."?

#6 Post by GreyWolfXx »

No, I haven't read it. I guess I'll have to :p. I'll do what you said about the screen and see.

"We have been to the moon, we have charted the depths of the ocean and the heart of the atom,
but we have a fear of looking inward to ourselves because we sense that is where
all the contradictions flow together." -- Terrence Mckenna

User avatar
GreyWolfXx
Regular
Posts: 68
Joined: Fri Jun 05, 2015 1:00 am
IRC Nick: Grey
Skype: GreyWolfXxII
Contact:

Re: Making a variable such as "If clicked then.."?

#7 Post by GreyWolfXx »

Code: Select all

]File "game/misc/misc.rpy", line 40: expected a keyword argument, colon, or end of line.
    text button "Change Key Bindings" action If(renpy.get_screen("key_screen"), Hide("key_screen"), Show("key_screen")) 
                ^
I put it in a separate screen in misc.rpy >_<
and called for the screen in script

"We have been to the moon, we have charted the depths of the ocean and the heart of the atom,
but we have a fear of looking inward to ourselves because we sense that is where
all the contradictions flow together." -- Terrence Mckenna

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: Making a variable such as "If clicked then.."?

#8 Post by trooper6 »

textbutton should be one word with no spaces. My autocorrect corrected it and I didn't notice.

Try fixing that and seeing if that works.

Also, here is the page on screen language.
http://www.renpy.org/doc/html/screens.html

If none of that is working for you, post your code. Specifically:
Your screen with your button
Your screen with your key
When you call your button screen

Also, if you are going to be writing screens, you really should read up on screen language.
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
GreyWolfXx
Regular
Posts: 68
Joined: Fri Jun 05, 2015 1:00 am
IRC Nick: Grey
Skype: GreyWolfXxII
Contact:

Re: Making a variable such as "If clicked then.."?

#9 Post by GreyWolfXx »

I'm trying to figure how to make a keymap changing varaible. I've decided that a button wouldn't work for what I'm doing (I want a preference screen) so I'm using imagemaps like so:

Code: Select all

screen key_map_screen:
    tag menu
    imagemap:
         ground "ui/pref/key_idle.png"
         idle "ui/pref/key_idle.png"
         hover "ui/pref/key_hover.png"
         hotspot (1379, 63, 134, 51) action ShowMenu("preferences")
         hotspot (170, 466, 343, 44) action None
         #hotspot (165, 534, 223, 43) action SetVariable("
         #hotspot (169, 601, 361, 48) action SetVariable("
         #hotspot (166, 667, 293, 50) action SetVariable("
         #hotspot (165, 749, 219, 41) action SetVariable("
         #hotspot (619, 458, 293, 47) action SetVariable("
         #hotspot (621, 527, 377, 49) action SetVariable("
         #hotspot (620, 596, 231, 52) action SetVariable("
         #hotspot (628, 673, 239, 49) action
         #hotspot (642, 737, 345, 55) action
I've commented them out for now. I was wondering on how to make a variable that such as "If clicked then change keymap to ___"

How would I do that? Sorry, for not being specific enough before. The screen call and such works fine as well. The variables are the only problem.

"We have been to the moon, we have charted the depths of the ocean and the heart of the atom,
but we have a fear of looking inward to ourselves because we sense that is where
all the contradictions flow together." -- Terrence Mckenna

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: Making a variable such as "If clicked then.."?

#10 Post by trooper6 »

You have a lot of hotspots there. What specific keymap change options do you want? What exactly is this for?
Can you be really specific what these preferences are supposed to do? What the buttons are supposed to do?
What are the keymaps you are changing? Are these just toggles of option A or option B?
If we knew exactly what you wanted in specific detail that will help in figuring out the best way to answer your question.

For example:
This is a game that is a VN+Dungeon crawler.
In this game, by default, pressing W triggers a moveForward() function I made, S triggers a moveBackward() function I made, A triggers a moveLeft() function I made, D triggers a moveRight() function I made. I want the player to be able to...what?
-Instead of WASD, have the arrow keys trigger those function.
-Allow the player to be able to toggle between each of those options so they could have up arrow, A, down arrow, D if they wanted
-Allow the player to map any key they want to their moveFunctions

So...what exactly is your game doing and what do you want to happen?

Side Note, you don't have to use an image map, you can use imagebuttons: http://lemmasoft.renai.us/forums/viewto ... +framework
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
GreyWolfXx
Regular
Posts: 68
Joined: Fri Jun 05, 2015 1:00 am
IRC Nick: Grey
Skype: GreyWolfXxII
Contact:

Re: Making a variable such as "If clicked then.."?

#11 Post by GreyWolfXx »

Basically, these are the keymap changes I'm trying to add. This is only a sample - not final GUI - which is why I'm just using imagemaps because for me it's simpler this way.
The users are supposed to be able to select what the right-click and space key do in this screen. It's a screen within the preferences screen and is supposed to be changeable at any time.

Hope this clarifies.

Image

"We have been to the moon, we have charted the depths of the ocean and the heart of the atom,
but we have a fear of looking inward to ourselves because we sense that is where
all the contradictions flow together." -- Terrence Mckenna

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: Making a variable such as "If clicked then.."?

#12 Post by trooper6 »

I know how to do what you want from within the game, but I don't know how to do what you want from the preferences screen. I made a few tests, and can't get it to work from the Preferences screen. Perhaps one of our advanced Renpy users know how to do that.
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
GreyWolfXx
Regular
Posts: 68
Joined: Fri Jun 05, 2015 1:00 am
IRC Nick: Grey
Skype: GreyWolfXxII
Contact:

Re: Making a variable such as "If clicked then.."?

#13 Post by GreyWolfXx »

Hm, then is there a different way to do that? Like to prompt the user at the beginning to ask what their preference is?

"We have been to the moon, we have charted the depths of the ocean and the heart of the atom,
but we have a fear of looking inward to ourselves because we sense that is where
all the contradictions flow together." -- Terrence Mckenna

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: Making a variable such as "If clicked then.."?

#14 Post by trooper6 »

Now, there may be a way to do it from the preferences screen...but I don't know how and I couldn't get it to work when I did a test game--the problem seemed to be that the game menu is not in the game state so trying to set variable from there resulted in errors. But! I'm sure one of our experts would know how to do something like that if it is possible.

But from within the game, you'd do it just like I said before.

You start the game, you have your keymap screen up and invisible in the background. You show your keymap choice screen (there is more than one approach to this) to your player asking for their choice, record their choices, and make those choices be reflected on your keymap screen.

I will be very explicit so you can see it in detail. But I will work backwards. I will also use a fictional example that you'd need to adapt to your own needs.

First things first. In the script.rpy, before the game starts (I did it right before the start label), I make two variables that will hold our choices for the right click and space bar, but since no choices have been made, I initialize them to Null:

Code: Select all

default r_choice = Null
default s_choice = Null
Note, these are not persistent variables so your choices will not persist in between different games. But I think that is okay. If you want them to be persistent, you'd have to use persistent variables instead: http://www.renpy.org/doc/html/persistent.html

Then I create the screen that holds all of my alternate keymap stuff.

Code: Select all

screen keymap_s():
    if r_choice == "red":
        key "mouseup_3" action Function(renpy.notify, "Red")
    if r_choice == "blue":
        key "mouseup_3" action Function(renpy.notify, "Blue")
    if s_choice == "red":
        key "K_SPACE" action Function(renpy.notify, "Red")
    if s_choice == "blue":
        key "K_SPACE" action Function(renpy.notify, "Blue")
If you know this is a set of if statements based on the r_choice and s_choice variable. In my fictional case, if r_choice is "red" then pressing the right click will trigger an action that would flash the word "Red" on the screen, if r_choice is "blue" then pressing the right click will trigger an action that would flash the word "Blue" on the screen. Note: These these behaviors would only happen if this screen is being shown! In your case you would have actions that do whatever you want. ShowMenu the Save menu...or View Clean Background....or View Backlog...or whatever other stuff you want.

So, when the game starts, right after the label start, I show this keymap_s:

Code: Select all

show screen keymap_s()
Now you have to have some way for the user to change your variables. The best way is through a screen. Here is an example of my keymap_choice screen:

Code: Select all

screen keymap_choice():
    vbox:
        hbox:
            frame:
                has vbox

                label _("Right Click")
                textbutton _("Red") action SetVariable("r_choice", "red")
                textbutton _("Blue") action SetVariable("r_choice", "blue")
                
            frame:
                has vbox

                label _("Space Bar")
                textbutton _("Red") action SetVariable("s_choice", "red")
                textbutton _("Blue") action SetVariable("s_choice", "blue")
        hbox:
            textbutton _("Clear") action [SetVariable("r_choice", Null), SetVariable("s_choice", Null)]
            textbutton _("Close") action Hide("keymap_choice")
For you it cold be a simple screen with textbuttons like this, or it could be your imagemap. I would make sure you included some way to clear the options in case they want to go back to default.

So now you have to have a way for you players to get access to this keymap_choice screen. You suggested that they could be asked when the game starts. That isn't my preference....because what if they want to witch it later? I would just add a button to the QuickMenu that shows the keymap_choice screen

Like so:

Code: Select all

##############################################################################
# Quick Menu
#
# A screen that's included by the default say screen, and adds quick access to
# several useful functions.
screen quick_menu():

    # Add an in-game quick menu.
    hbox:
        style_group "quick"

        xalign 1.0
        yalign 1.0

        textbutton _("Back") action Rollback()
        textbutton _("Save") action ShowMenu('save')
        textbutton _("Q.Save") action QuickSave()
        textbutton _("Q.Load") action QuickLoad()
        textbutton _("Skip") action Skip()
        textbutton _("F.Skip") action Skip(fast=True, confirm=True)
        textbutton _("Auto") action Preference("auto-forward", "toggle")
        textbutton _("Prefs") action ShowMenu('preferences')
        textbutton _("Key Map") action Show("keymap_choice")

init -2:
    style quick_button:
        is default
        background None
        xpadding 5

    style quick_button_text:
        is default
        size 12
        idle_color "#8888"
        hover_color "#ccc"
        selected_idle_color "#cc08"
        selected_hover_color "#cc0"
        insensitive_color "#4448"
That does it. There is some more polish you could do...for example, you could have the key map_choice screen be modal so the player couldn't progress in the game until they closed the screen.

But those are the mechanics of how to do what you want to do from within the game.
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
GreyWolfXx
Regular
Posts: 68
Joined: Fri Jun 05, 2015 1:00 am
IRC Nick: Grey
Skype: GreyWolfXxII
Contact:

Re: Making a variable such as "If clicked then.."?

#15 Post by GreyWolfXx »

Thank you for this ^_^. Now, I should be able to get it working with some tweaks. I've imported your script and am now tweaking it for View Background, View Backlog, etc.
On a side note, I played your Young Earth Roadtrip game and was thoroughly amused.

"We have been to the moon, we have charted the depths of the ocean and the heart of the atom,
but we have a fear of looking inward to ourselves because we sense that is where
all the contradictions flow together." -- Terrence Mckenna

Post Reply

Who is online

Users browsing this forum: No registered users