Turning a function into a creator-defined statement

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
rezaf
Newbie
Posts: 8
Joined: Wed Sep 20, 2017 2:56 am
Contact:

Turning a function into a creator-defined statement

#1 Post by rezaf » Sun May 10, 2020 5:05 am

I'm trying to steamline the code in my current WIP project.
One thing I'd like to change if possible is swap "$ xyz" calls with direct calls without Python, and from how I understand the documentation, Creator-defined statements seem like the way to go.

Simplest example:
$ showScene("myScene")

This calls a simple python function.

Code: Select all

def showScene(scenePicture, clearActors=True):
renpy.scene()
renpy.show(scenePicture)
(...)
Now, I'd like to be able to just write "showScene myScene", that'd look much slicker in code.
I tried to go by the description/example given in the documentation and ended up with this here:

Code: Select all

python early:

    def parse_showScene(lex):
        what = lex.simple_expression()
        return what

    def execute_showScene(o):
        what = o
        renpy.scene()
        renpy.show(what)

    def lint_showScene(o):
        what = o
        try:
            eval(what)
        except:
            renpy.error("Scene not defined: %s" % what)

    renpy.register_statement("showScene", parse=parse_showScene, execute=execute_showScene, lint=lint_showScene)
Okay, when you're done facepalming, maybe you can show me how it ought to be done? :wink:
Cause OBVIOUSLY that doesn't work and I get an "expected statement" error instead.
The code compiles if I write >showScene "myScene"< instead, i.e. put myScene in quotes, but then all hell breaks loose if I actually get to that point.

Thanks you very much in advance.

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

Re: Turning a function into a creator-defined statement

#2 Post by hell_oh_world » Sun May 10, 2020 5:13 am

I'm not sure why do you need that function in the first place, clearly your function can be achieved by doing something like this...

Code: Select all

label start:
    scene the_image # this clears the screen and show at the same time a new image, this is the exact equivalent of that in your function.

rezaf
Newbie
Posts: 8
Joined: Wed Sep 20, 2017 2:56 am
Contact:

Re: Turning a function into a creator-defined statement

#3 Post by rezaf » Sun May 10, 2020 5:50 am

Obviously the showScene should do additional things instead of just showing the scene, hence the (...) ;)

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

Re: Turning a function into a creator-defined statement

#4 Post by hell_oh_world » Sun May 10, 2020 7:17 am

Still doesn't make sense, as your function only requires one parameter. I wouldn't bother creating one just for this. Maybe if you have multiple parameters that can be split into multiple lines then that's the time to use creator defined statements. Typing `$ showScene("something")` it's not really laborious at all...

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: Turning a function into a creator-defined statement

#5 Post by isobellesophia » Sun May 10, 2020 7:56 am

I don't know python but, what do you mean by additional things from the image? Maybe you were talking about screen on the image, imagemaps... or interactive things maybe? Point and click? Movie?

Adding a block simply from the image can do positioning the image itself like xpos... linear... and more style functions from the code itself.
I am a friendly user, please respect and have a good day.


Image

Image


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

Re: Turning a function into a creator-defined statement

#6 Post by gas » Sun May 10, 2020 7:58 am

Write the thing in a script called something like '01mystates.rpy'.
And recompile the game.
If you want to debate on a reply I gave to your posts, please QUOTE ME or i'll not be notified about. << now red so probably you'll see it.

10 ? "RENPY"
20 GOTO 10

RUN

rezaf
Newbie
Posts: 8
Joined: Wed Sep 20, 2017 2:56 am
Contact:

Re: Turning a function into a creator-defined statement

#7 Post by rezaf » Sun May 10, 2020 3:30 pm

Heh, my question was not whether or not people thought it was a sensible thing to change this into a creator-defined statement, I asked how to do it. :P

I understand how to write calls with $ in front, as I mentioned in the first post, and I also wrote this was the simplest example, my intention there is to learn how to use this feature and then to use it several times.

As for the additional things, I currently have my own jury-rigged actor subsystem, and when I open up a new scene I'd like that to be automatically cleared. There's another system I haven't fully implemented that I might want to affect here as well. That's it, mostly.
The actor system itself is a little more complicated and comes with more parameters, I'd like to reimplement this as well, if possible.

@gas: Sure that this will affect anything? I'll give it a try tomorrow ...

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

Re: Turning a function into a creator-defined statement

#8 Post by gas » Sun May 10, 2020 3:40 pm

rezaf wrote:
Sun May 10, 2020 3:30 pm

@gas: Sure that this will affect anything? I'll give it a try tomorrow ...
From the same docs:

Creator-defined statements must be defined in a python early block. What's more, the filename containing the user-defined statement must be be loaded earlier than any file that uses it. Since Ren'Py loads files in Unicode sort order, it generally makes sense to prefix the name of any file containing a user-defined statement with 01, or some other small number.

A creator-defined statement cannot be used in the file in which it is defined.
If you want to debate on a reply I gave to your posts, please QUOTE ME or i'll not be notified about. << now red so probably you'll see it.

10 ? "RENPY"
20 GOTO 10

RUN

philat
Eileen-Class Veteran
Posts: 1853
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Turning a function into a creator-defined statement

#9 Post by philat » Sun May 10, 2020 9:49 pm

The reason everyone is saying maybe don't do it is because it's time and effort going into something with not a ton of benefit -- the difference in effort between $showScene(myScene) and showScene myScene is... negligible. The effort of getting a creator-defined statement to work is less negligible (unless you already understand how the lexer works, which frankly, I don't, and I took a stab myself at several creator-defined statements for the sake of learning) and that effort increases as you add more functionality to the statement (like, do you want to optionally include that clearActors=True/False part? Have fun figuring out dealing with that in the lexer).

That said, your example works if you do what gas tells you to, but it won't show an image with tags correctly -- if you write showScene bg room, it will clear the screen and show the image bg (not bg room).

rezaf
Newbie
Posts: 8
Joined: Wed Sep 20, 2017 2:56 am
Contact:

Re: Turning a function into a creator-defined statement

#10 Post by rezaf » Mon May 11, 2020 10:54 am

gas wrote:
Sun May 10, 2020 3:40 pm
(snip)
Yeah, that did indeed work. You know your stuff, thank's a bunch indeed.

@philat: Maybe you're right, but I'm still glad I at least go this simple statement to work. It's a start.
As for the clearActors thing, maybe I can just define a showSceneClear which does that, then it won't need another parameter.
We'll see, I'll try to play around with this some more asap.

Thanks again everyone.

rezaf
Newbie
Posts: 8
Joined: Wed Sep 20, 2017 2:56 am
Contact:

Re: Turning a function into a creator-defined statement

#11 Post by rezaf » Mon May 11, 2020 12:05 pm

@philat: Getting the clearActors to work turned out to be quite simple, I just had to use an integer instead of a boolean because the lexer doesn't appear to support bools.

drKlauz
Veteran
Posts: 237
Joined: Mon Oct 12, 2015 3:04 pm
Contact:

Re: Turning a function into a creator-defined statement

#12 Post by drKlauz » Mon May 11, 2020 1:19 pm

You can also hook scene/show statements:
https://www.renpy.org/doc/html/config.h ... nfig.scene
https://www.renpy.org/doc/html/config.h ... onfig.show
This will allow adding extra functionality without adding new statements and might be simpler way, depending what was hidden by (...) in first post.
I may be available for hire, check my thread: viewtopic.php?f=66&t=51350

Post Reply

Who is online

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