Knuckle Cracker

Creeper World 3 => The Coder's Corner => Topic started by: Clean0nion on December 19, 2013, 07:29:21 PM

Title: Is there any way to have a script run ONCE?
Post by: Clean0nion on December 19, 2013, 07:29:21 PM
Hi guys! I'm new to this forum and this is my first post, so be kind please!

Is there any way to have a script run once?
For example, there is the obvious once -- endonce function, and things such as :Gameloaded and :Awake, but none of those do what I want, because they can repeat after the map is reloaded.

What I want is this:
- The game is loaded.
- The script immediately runs.
- It does not run again, even if the map is restarted.

The reason that I want this is because I have a fancy opening sequence.
Can anyone help?
Title: Re: Is there any way to have a script run ONCE?
Post by: Lost in Nowhere on December 19, 2013, 07:42:36 PM
As far as I'm aware, once -- endonce only runs when the scripts are recompiled, variables are changed, or the script has not yet run.
Title: Re: Is there any way to have a script run ONCE?
Post by: kwinse on December 19, 2013, 08:22:14 PM
Alternatively, destroy the core after it runs the script.
Title: Re: Is there any way to have a script run ONCE?
Post by: Annonymus on December 20, 2013, 06:04:10 AM
If you really want a way to not do it again even when the map is restarted, I think there is no way to do that...
Except...
Maybe you can do something with the dialogues that pop up when you first play a map, these don't show up a 2. time even if you restart a map, so if you find a way to check if those dialogues have already been seen you could do this.
Then you just need a way to get in a dialogue without making it creepy (because when the dialogue finishes another (your CRPL one) pops up...)
Title: Re: Is there any way to have a script run ONCE?
Post by: knucracker on December 20, 2013, 08:59:45 AM
A once/endonce section only runs once.  During development these sections gets reset when you recompile.  But for a released map a once/endonce section only executes once.  This is true across save/load cycles.

If you want something that only runs twice (for example). Then you can do it with a variable yourself that you check.  Variables get saved and loaded so if you have a variable named "executionCount" its count gets persisted in save file.  That's basically how the once/endonce section is implemented internally (the count vars are implicitly created for you in internal structures).
Title: Re: Is there any way to have a script run ONCE?
Post by: Grauniad on December 20, 2013, 09:01:43 AM
My thinking is if you get a key then you can check on the key and perhaps the core won't run again. Keys are persistent across map runs.
Title: Re: Is there any way to have a script run ONCE?
Post by: Grauniad on December 20, 2013, 09:49:39 AM
Quote from: Annonymus on December 20, 2013, 06:04:10 AM

Maybe you can do something with the dialogues that pop up when you first play a map, these don't show up a 2. time even if you restart a map,

Just on a point of clarification, you do know these are in the "Archived Transmissions" button in the Orbital section, right?
Title: Re: Is there any way to have a script run ONCE?
Post by: Clean0nion on December 20, 2013, 10:03:15 AM
Here's what I'm thinking so far.

Annonymous, your solution, as far as I know, isn't possible. I can check whether the conversation is showing or not, and that's it. However, my intro uses dynamically-created chat. So that's not possible for me even if the CRPL allowed it.

Grauniad, as far as I know keys can only be permanently collected if the map is beaten with it. So that's not viable either.

Virgil, your solution makes sense less. If a once -- endonce function is only ever run once per map, then that must mean that variables set in said once -- endonce function must persist across saves too? Unless more :GameLoaded functions are being used than I think.
But if variables persist across saves and loads, that is very interesting. Unless you can confirm this now, I'll run a few tests and see if it does persist.
And thinking of that - is this what the NotPersist command does? It makes a variable not persist across saves?

EDIT: Added NotPersist to the wiki
Title: Re: Is there any way to have a script run ONCE?
Post by: thepenguin on December 20, 2013, 04:08:15 PM
Quote from: Clean0nion on December 20, 2013, 10:03:15 AM
Virgil, your solution makes sense less. If a once -- endonce function is only ever run once per map, then that must mean that variables set in said once -- endonce function must persist across saves too? Unless more :GameLoaded functions are being used than I think.
But if variables persist across saves and loads, that is very interesting. Unless you can confirm this now, I'll run a few tests and see if it does persist.
And thinking of that - is this what the NotPersist command does? It makes a variable not persist across saves?
they do.  you are right on all counts here.  They persist in saves unless told to do otherwise.