How to use functions defined inside screen?

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
Milkymalk
Miko-Class Veteran
Posts: 760
Joined: Wed Nov 23, 2011 5:30 pm
Completed: Don't Look (AGS game)
Projects: KANPEKI! ★Perfect Play★
Organization: Crappy White Wings
Location: Germany
Contact:

How to use functions defined inside screen?

#1 Post by Milkymalk »

How can I use functions that are inside screens? I tried understanding dragging when I wrote this, and the card_dragged function that is the drag callback works flawlessly, but when it wants to call the function snapcard, I get an error that it is not defined.

I know I can just put the functions outside, but I like my data tidy and grouped.

Code: Select all

screen dragtest():
    python:
        def snapcard(card, slot):
            card.snap(slot.x,slot.y)
            return

        def card_dragged(drags, drop):
            if not drop:
                return
            card = drags[0].drag_name
            slot = drop.drag_name
            snapcard(drags[0], drop)
            return

# insert regular screen stuff
Crappy White Wings (currently quite inactive)
Working on: KANPEKI!
(On Hold: New Eden, Imperial Sea, Pure Light)

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2413
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: How to use functions defined inside screen?

#2 Post by Ocelot »

It is simple: you cannot.

Function definition should happen in init phase and Screen Language does not have any way to execute code in init phase.
< < insert Rick Cook quote here > >

User avatar
Milkymalk
Miko-Class Veteran
Posts: 760
Joined: Wed Nov 23, 2011 5:30 pm
Completed: Don't Look (AGS game)
Projects: KANPEKI! ★Perfect Play★
Organization: Crappy White Wings
Location: Germany
Contact:

Re: How to use functions defined inside screen?

#3 Post by Milkymalk »

Not that I don't believe you, but why is it then that the callback function works?
Crappy White Wings (currently quite inactive)
Working on: KANPEKI!
(On Hold: New Eden, Imperial Sea, Pure Light)

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2413
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: How to use functions defined inside screen?

#4 Post by Ocelot »

That's the beauty of the undefined behavior: anything can happen. Even that incorrect code will work.

While RenPy wants python functions to be defined during init phase, it does not take any additional steps to ensure that code that violates that principle won't work. Your callback simply didn't trip anything which requires that, while attempting to use other did (though I highly suspect that you actually run into scope issues there, which is another problem that arises from attempting to define something that is intended to be defined globally in local scope).

This is like running a red light or driving drunk: for many people it works and they did not run into any probles eve. Doesn't mean that it is a good idea to do so.
< < insert Rick Cook quote here > >

User avatar
Milkymalk
Miko-Class Veteran
Posts: 760
Joined: Wed Nov 23, 2011 5:30 pm
Completed: Don't Look (AGS game)
Projects: KANPEKI! ★Perfect Play★
Organization: Crappy White Wings
Location: Germany
Contact:

Re: How to use functions defined inside screen?

#5 Post by Milkymalk »

That's funny. I always imagined screens to behave like classes in that regard, so that I *can* define local functions that exist in the namespace of that screen only; and I'm sure I already used this to have small specialized functions there, probably small and straightforward enough (like a pythagorean calculation of distance) that it all worked.
So that's out the window :( Meh.
Crappy White Wings (currently quite inactive)
Working on: KANPEKI!
(On Hold: New Eden, Imperial Sea, Pure Light)

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot]