Knuckle Cracker

Creeper World 3 => The Coder's Corner => Topic started by: Clean0nion on February 07, 2014, 12:43:38 PM

Title: Once Awake
Post by: Clean0nion on February 07, 2014, 12:43:38 PM
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?
Title: Re: Once Awake
Post by: knucracker on February 07, 2014, 02:27:12 PM
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.
Title: Re: Once Awake
Post by: 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.
Title: Re: Once Awake
Post by: knucracker on February 07, 2014, 03:11:52 PM
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.
Title: Re: Once Awake
Post by: Grauniad on February 07, 2014, 03:32:59 PM
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
Title: Re: Once Awake
Post by: knucracker on February 07, 2014, 06:49:10 PM
Why is there a second "left" in that sentence I wrote?  You got me...

http://www.youtube.com/watch?v=6SfEpm-f-wc
(Audio is NSFW)
Title: Re: Once Awake
Post by: Clean0nion on February 07, 2014, 06:58:45 PM
Anyway, thanks for answering my question!
Title: Re: Once Awake
Post by: thepenguin on February 07, 2014, 07:26:23 PM
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.