How to setup kwargs for action "Function"? (KeyError)

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
Grauen
Newbie
Posts: 6
Joined: Tue Jan 19, 2016 2:56 pm
Location: Germany
Contact:

How to setup kwargs for action "Function"? (KeyError)

#1 Post by Grauen » Fri Feb 05, 2016 4:47 pm

I want to add some arguments to the renpy Function action (http://www.renpy.org/doc/html/screen_ac ... l#Function).
I can't access my kwargs, renpy will throw this exception:

Code: Select all

  File "game/test_item_callable.rpy", line 62, in script
    "Click the Button"
  File "renpy/common/00action_other.rpy", line 430, in __call__
    rv = self.callable(*self.args, **self.kwargs)
  File "game/test_item_callable.rpy", line 17, in useItem
    currentItem = kwargs['i']
KeyError: 'i'
The related source:

Code: Select all

...

    imagebutton xpos 0.5 ypos 0.12 idle girlObj.imagename hover girlObj.imagename action [Function(useItem ,[],{'i': item, 'g': girlObj}),Hide("item_usage_screen")]
...

   def useItem(*args, **kwargs):
        currentItem = kwargs['i']
        currentCharacter = kwargs['g']
        renpy.call_in_new_context('executeItemLabel')
        return
I think I've made a mistake, but I can't find any documentation or examples to pass arguments in the Function action.
I hope you can help me to get this work.

Sincerly,

Grauen

neowired
Regular
Posts: 199
Joined: Mon Dec 01, 2008 2:33 pm
Contact:

Re: How to setup kwargs for action "Function"? (KeyError)

#2 Post by neowired » Fri Feb 05, 2016 7:22 pm

Ok, first, i don't think this is going to do what you want it to do
if you assign a variable inside a function in python, i think that variable is private to the function, as an example:

Code: Select all

a = 5
renpy.say(n, a) # will output 5
def editValue():
	a = 3
	renpy.say(n, a) # will output 3, but this is the local a of the editValue function, this a is not the same a as the earlier a
renpy.say(n, a) => will again output 5, because this a was not changed, only the a inside the function was assigned/modified
What this means is that if you do:

Code: Select all

   def useItem(i,g):
        currentItem = i # this is meaningless, it assigns value to the local variable currentItem, which is discared once the function is over
        currentCharacter = g # the same with this one
        renpy.call_in_new_context('executeItemLabel')
Next:
Is the usage of kwargs required? if not then i would just do:

Code: Select all

[Function(useItem, item, girlObj),Hide("item_usage_screen")]

   def useItem(i,g):
        currentItem = i
        currentCharacter = g
        renpy.call_in_new_context('executeItemLabel')


if you must use kwargs, then I think the correct syntax would probably be something like this? Sorry, i'm not entirely sure.

Code: Select all

[Function(useItem, i=item, g=girlObj),Hide("item_usage_screen")]

   def useItem(**kwargs):
        currentItem = kwargs['i']
        currentCharacter = kwargs['g']
        renpy.call_in_new_context('executeItemLabel')

User avatar
Grauen
Newbie
Posts: 6
Joined: Tue Jan 19, 2016 2:56 pm
Location: Germany
Contact:

Re: How to setup kwargs for action "Function"? (KeyError)

#3 Post by Grauen » Fri Feb 05, 2016 9:51 pm

Thanks for your answer. As I understand, it would be enough to define the girl and currentChar variables as global. Then there should be no problem with private variables. I don't see any other way to make this work, anyway.

Maybe I could just use two global dictionaries, each for characters and items and pass the keys only. But I'm no python expert and I dont know whether this is good style.


You can pass args and kwargs. How can renpy see the difference in your example? Ill test it later, thanks. :)

User avatar
Grauen
Newbie
Posts: 6
Joined: Tue Jan 19, 2016 2:56 pm
Location: Germany
Contact:

Re: How to setup kwargs for action "Function"? (KeyError)

#4 Post by Grauen » Sat Feb 06, 2016 6:01 am

You can pass the parameters like you suggested:

Code: Select all

imagebutton xpos 0.5 ypos 0.12 idle girlObj.imagename hover girlObj.imagename action [Function(useItem , i=item, g=girlObj),Hide("item_usage_screen")]
Maybe it's possible to update the documentation or add this example to the cookbook.

Post Reply

Who is online

Users browsing this forum: Eyzi, renardjap