keymap to run a made function

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
Newprogrammer
Newbie
Posts: 17
Joined: Sat Oct 20, 2018 10:28 am
Contact:

keymap to run a made function

#1 Post by Newprogrammer »

Hello everyone, i was wondering if it is possible to configure keymap in order execute a function that i've made.

Code: Select all

init python:
    def add():
        result = (2+ 5)
        print (result)
    
    config.keymap['add'].append('a')

when i tried to run it i get a

Code: Select all

KeyError = u'['add']
I just made some error or is it an impossible thing to run a no default function with keymap?

(sorry for the stupid function but it was the first that came up to my mind)
Last edited by Newprogrammer on Sun Apr 07, 2019 5:10 pm, edited 2 times in total.

User avatar
Alex
Lemma-Class Veteran
Posts: 3094
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: keymap to run a made function

#2 Post by Alex »

You can make a screen, where set an action Function("my_add") for a key. This key should work while screen is shown.

https://www.renpy.org/doc/html/screens.html#key
https://www.renpy.org/doc/html/screen_a ... l#Function

Newprogrammer
Newbie
Posts: 17
Joined: Sat Oct 20, 2018 10:28 am
Contact:

Re: keymap to run a made function

#3 Post by Newprogrammer »

Thanks for the reply Alex, but i'm not totally sure i get it, do i have to do something like this?

Code: Select all

init python:
    def add():
        result = (2+ 5)
        print (result)
    
screen add:
    key "a" action Function("add")
        

User avatar
Alex
Lemma-Class Veteran
Posts: 3094
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: keymap to run a made function

#4 Post by Alex »

Oh, well, try this then

Code: Select all

default result = 42 # default value of the global var

init python:
    def my_add(x): #unique name
        global result # use global var "result" not a local one for this function
        result += x
        #print (result) # this will print the result in console
    
screen add_scr():
    key "a" action Function(my_add, 3)
    key "s" action Function(my_add, -3)
    text "[result]" align(0.5,0.05) # a way to show value


# The game starts here.

label start:
    "..."
    show screen add_scr # screen must be shown to let player operate the result var
    "... ..."
    "?!"
That was my mistake - name of custom function in a Function action doesn't need quotes.

In Ren'Py there are number of ways to show text (print will show it in console, also text might be shown in a screen, or "said" by character).

Variables used in a function are local for this function if not specified to use some global ones. In this sample screen will affect a global var "result".
https://www.renpy.org/doc/html/python.html

Screen must be shown or called to let player interact with it.
https://www.renpy.org/doc/html/screens. ... statements

And try to use long_and_easy_to_understand names for everything to avoid conflicts - https://www.renpy.org/doc/html/reserved ... rved-names

Newprogrammer
Newbie
Posts: 17
Joined: Sat Oct 20, 2018 10:28 am
Contact:

Re: keymap to run a made function

#5 Post by Newprogrammer »

Thank you so much for your help and support Alex, now it works perfectely

Post Reply

Who is online

Users browsing this forum: Google [Bot], Ocelot