How to define python function at game loading?

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
tmp0000
Newbie
Posts: 11
Joined: Tue Mar 06, 2018 5:46 am
Contact:

How to define python function at game loading?

#1 Post by tmp0000 »

Hi. I've writed a simple function (it's defined near 'start' label) that improves game process and inserted it along code many times like this:

Code: Select all

$ MyFunction()
Hovewer, when I load game and script control goes to one of those lines, I'm getting this error:

Code: Select all

NameError: name 'MyFunction' is not defined
So all I need is to define this function again while game is loading.

How it could be done? Thanks in advance.

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3785
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: How to define python function at game loading?

#2 Post by Imperf3kt »

Run it during initialisation:

Code: Select all

init python:
    Your function
You may need to lower or raise the level:

Code: Select all

init python -1:
    your function

Code: Select all

init python +2:
    your function
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

tmp0000
Newbie
Posts: 11
Joined: Tue Mar 06, 2018 5:46 am
Contact:

Re: How to define python function at game loading?

#3 Post by tmp0000 »

Imperf3kt wrote: Mon Mar 19, 2018 3:15 am Run it during initialisation:

Code: Select all

init python:
    Your function
Thank you. Seems that I missed it in docs. Game is not crashing now. But I think that after loading game this function is not executed. Any ideas?

User avatar
Lord Hisu
Regular
Posts: 58
Joined: Sun Feb 11, 2018 2:31 am
Contact:

Re: How to define python function at game loading?

#4 Post by Lord Hisu »

Your function is defined in the init block, before the game even start, as Imperf3kt said.
tmp0000 wrote: Wed Mar 21, 2018 1:51 amThank you. Seems that I missed it in docs. Game is not crashing now. But I think that after loading game this function is not executed. Any ideas?
Now, if I understand you correctly, you don't know how to execute your function whenever the player loads the game, right?
In that case, you could use the special label after_load.

Code: Select all

label after_load:
    # Your function here
Simple as that.

tmp0000
Newbie
Posts: 11
Joined: Tue Mar 06, 2018 5:46 am
Contact:

Re: How to define python function at game loading?

#5 Post by tmp0000 »

Lord Hisu wrote: Wed Mar 21, 2018 2:44 am Your function is defined in the init block, before the game even start, as Imperf3kt said.

...

Now, if I understand you correctly, you don't know how to execute your function whenever the player loads the game, right?
Nope. Am sorry if I tangled you.

But the result seems like this: user plays game, then saves it. I modify script and define function that make gameplay randomisations. And paste it's executions many times in game script too. When user loads his old game with new script, it doesn't crash anymore (because the function declaration was moved to the init block, as Imperf3kt adviced). Then user plays game further. When script control goes on lines where this function is executed, it seems that nothing happens. But when user starts a new game all is working as I expected.

Where could be an error?
In that case, you could use the special label after_load.

Code: Select all

label after_load:
    # Your function here
Simple as that.
Cool! It seems I can solve some other problems with it. But. There is no "before save" label (or it's analog), is it?

User avatar
Lord Hisu
Regular
Posts: 58
Joined: Sun Feb 11, 2018 2:31 am
Contact:

Re: How to define python function at game loading?

#6 Post by Lord Hisu »

tmp0000 wrote: Wed Mar 21, 2018 11:52 amBut the result seems like this: user plays game, then saves it. I modify script and define function that make gameplay randomisations. And paste it's executions many times in game script too. When user loads his old game with new script, it doesn't crash anymore (because the function declaration was moved to the init block, as Imperf3kt adviced). Then user plays game further. When script control goes on lines where this function is executed, it seems that nothing happens. But when user starts a new game all is working as I expected.

Where could be an error?
Can you post a shor version of your code? I can try to find the problem for you. =)
tmp0000 wrote: Wed Mar 21, 2018 11:52 amCool! It seems I can solve some other problems with it. But. There is no "before save" label (or it's analog), is it?
No, but you can work with the save screen and modify the actions of the buttons, doing something and then saving the game.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], VESTED