Once Awake

Started by Clean0nion, February 07, 2014, 12:43:38 PM

Previous topic - Next topic

Clean0nion

If I do this:

:Awake
   once
      @DoThing
   endonce


Will that have something happen as the game is loading/resetting for the first time, then never again?

knucracker

Yeah, that once section should prevent the contents from executing more than once (even across save/load cycles).
You could also call GetUpdateCount and check to see of that is 0 to make it so that something only runs once in :Awake.

But you are free to use once/endonce blocks in other places and this looks like a potentially useful place.

Grayzzur

Really?
so if I did something like this in the middle of a script:

<-something <-somethingelse eq if
  once
    @DoSomeStuff
  endonce
  @DoSomeOtherStuff
endif


Then @DoSomeStuff only gets called the first time we drop into that if? I didn't even think about that being a valid place to use once/endonce.
"Fate. It protects fools, little children, and ships named 'Enterprise.'" -William T. Riker

knucracker

Yeah... that's supposed to be right.  I can't say I have tested it in every nook and cranny possible.  But the way it is implemented is that the ONCE command sets an internal bool to true based on the location of the ONCE command in the command list.  CRPL compiles to a list of opcodes (literally a list of them) with each opcode at a fixed location in the list.  The once command checks if it has ever executed before by looking at that bool it sets.  If so, execution jumps the the location of the ENDONCE command.  And of course this table of bools is persisted so it survives save/load cycles.  Recompiling a script clears the table, though.

Language wise, this is a funky thing and I'm not sure it has an comparable thing in other languages.  In other languages it would be left to lower left constructs and the programmer to implement something like this.  But I was thinking the use case might come up so frequently that adding language support might be useful/interesting in CRPL.

Grauniad

Quote from: virgilw on February 07, 2014, 03:11:52 PM

Language wise, this is a funky thing and I'm not sure it has an comparable thing in other languages.  In other languages it would be left to lower left constructs and the programmer to implement something like this.  But I was thinking the use case might come up so frequently that adding language support might be useful/interesting in CRPL.

I am not familiar with constructs of this type. Can you, for my elucidation and education, contrast those with simple left constructs,, or perhaps even with higher right constructs?

Personally, I've always avoided invoking Higher Right constructs for fear of divine wrath if the passing arguments were not up to the correct moral code. :P
A goodnight to all and to all a good night - Goodnight Moon

knucracker

Why is there a second "left" in that sentence I wrote?  You got me...


(Audio is NSFW)

Clean0nion

Anyway, thanks for answering my question!

thepenguin

Quote from: Grayzzur on February 07, 2014, 03:04:21 PM
Really?
so if I did something like this in the middle of a script:

<-something <-somethingelse eq if
  once
    @DoSomeStuff
  endonce
  @DoSomeOtherStuff
endif


Then @DoSomeStuff only gets called the first time we drop into that if? I didn't even think about that being a valid place to use once/endonce.
Yep, that works.  I've used it before.
We have become the creeper...