Item disappearing from the stack?

Started by Molay, May 30, 2013, 10:13:04 AM

Previous topic - Next topic

Molay

Hello!

It's as if items disappear from the stack, without me using them.
I've been trying to solve it for over an hour now. I can't fathom why that happens  :o

Here's the code:

$light1:"light1"
$light2:"light2"
$light3:"light3"
$light4:"light4"
$timer:600  
$colorRed:0
$colorGreen:0
$colorBlue:0
$lightCurrent:"done"


once
SetImageScale(self "main" 2 2)
SetImage(self <-light1 "Custom0")
SetImage(self <-light2 "Custom0")
SetImage(self <-light3 "Custom0")
SetImage(self <-light4 "Custom0")
SetImagePosition(self <-light1 -10  10 -0.01)
SetImagePosition(self <-light2  10  10 -0.01)
SetImagePosition(self <-light3 -10 -10 -0.01)
SetImagePosition(self <-light4  10 -10 -0.01)
SetTimer0(90)
ShowTraceLog
endonce

if(GetTimer0 eq0)
if(<-lightCurrent "done" eq)
<-light4 <-light3 <-light2 <-light1
dup trace

"next" ->lightCurrent
SetImageColor(self <-light1 255 0 0 255)
SetImageColor(self <-light2 255 0 0 255)
SetImageColor(self <-light3 255 0 0 255)
SetImageColor(self <-light4 255 0 0 255)
endif

if(<-lightCurrent "next" eq)
->lightCurrent
255 ->colorRed 0 ->colorGreen 0 ->colorBlue
endif
#dup trace
if(<-colorGreen eq0)
if(<-colorBlue 255 lt)
add(<-colorBlue 1) ->colorBlue
endif
if(<-colorBlue 255 eq)
if(<-colorRed 0 gt)
sub(<-colorRed 1) ->colorRed
endif
endif
if(<-colorRed eq0)
add(<-colorGreen 1) ->colorGreen
endif
endif

if(<-colorRed eq0)
if(<-colorGreen 255 lt)
add(<-colorGreen 1) ->colorGreen
endif
if(<-colorGreen 255 eq)
if(<-colorBlue 0 gt)
sub(<-colorBlue 1) ->colorBlue
endif
endif
endif
SetImageColor(self <-lightCurrent <-colorRed <-colorGreen <-colorBlue 255)
#dup trace
if(<-colorGreen 255 eq)
if(<-colorBlue eq0)
if(<-lightCurrent <-light4 eq)
"done" ->lightCurrent
SetTimer0(<-timer)
else
"next" ->lightCurrent
SetTimer0(<-timer)
endif
endif
endif
endif


So, here I create 4 items and put them on the stack. light 1 through 4

       if(<-lightCurrent "done" eq)
<-light4 <-light3 <-light2 <-light1
dup trace

"next" ->lightCurrent
SetImageColor(self <-light1 255 0 0 255)
SetImageColor(self <-light2 255 0 0 255)
SetImageColor(self <-light3 255 0 0 255)
SetImageColor(self <-light4 255 0 0 255)
endif


When my code come's back here, it appears there's nothing on the stack anymore:

if(<-lightCurrent "next" eq)
->lightCurrent
255 ->colorRed 0 ->colorGreen 0 ->colorBlue
endif


I've verified the code over a dozen times now. I don't see any place I would accidentally pop a value from the stack.
Really needing help here.
I could do another approach to cycle the lights, to avoid putting items on the stack I only intend to use much later. But the current design allows it to easily expand the amount of lights in use, while the alternative is just a number of interlinked if-else, which doesn't make for easy expansion.

Any suggestions as to why this happens are much appreciated!

Molay

Michionlion

As far as I know, dup only duplicates the top item of the stack, and trace does the whole stack.  That might be your problem.
"Remember kids, the only difference between science and messing around is writing it down."
                                                                                                                         - Adam Savage

My website
My CW1, and CW2 maps!

knucracker


Molay

#3
Okay, so I know now that the stack disappears at the end of the code. Right after the last endif.
At the next frame, the stack is empty when the script runs again.

Is it possible that the stack does not persist over multiple runs of the same script?
Because, my last instruction is a tracelog, and it has the items I expect to be in the stack.
The first instruction is a tracelog too, and it is always empty.

This would ruin my whole approach^^

Oh, and, welcome back, Virgil! :D

Edit: Must be that the stack is cleared once the script is executed. Found a way to circumvent that while keeping the main idea in place (thanks to concat).

Molay

Now that I've been playing with concat I do wonder:

Is it possible to automatically instanciate a number a variables?

$lightAmount:4

once
do(<-lightAmount 0)
concat( concat(concat(concat("$" "light") I) ":") concat("light" I) )
loop
endonce


Here I create the strings for my variables as I'd like to call them. How can I "add them to the code"? Is this even possible in CRPL?

knucracker

The stack is scoped for each execution of the core.  That means that the stack gets cleared after each frame.  Think of your CRPL core like a function call in C, C++, Java+ C#, etc.  You get a stack with each function call in those languages and the stacks lifetime is the function call.

Variables are scoped to the lifetime of a core. They can be used to store thing that you mean to access across invocations of your core.  They only get cleaned up once a core is destroyed.  Internally, the stack is held by a core and is cleared at the end of each invocation.  The variables are held by the core in a hash map and that hash map is only disposed of when the core is destroyed.

Syntax wise when you declare a variable with a $, as in:
$foobar:42
All that does is create a variable named foobar, assign it the value of 42, and tell the core to remember this name so the game can show it in the editor.  Other than the editor part, its the same as this at the top of your script.
once 42 ->foobar endonce

So keep in mind that the only reason the whole $ syntax exists is just to tag a variable as something that can be edited in the game's editor.  It ultimately creates a variable no different than just assigning a value to a variable.

If you have a fixed set of things you want to remember across core invocations, just create  that fixed set of variables, like:
42 ->foobar1
43 ->foobar2
99 ->foobar3
1.41 ->foobar4
"LarryMoeCurly" ->foobar5

If you want to dynamically name variables, you can do that with the reflection operators as described here:
http://knucklecracker.com/wiki/doku.php?id=crpl:docs:refwrite

And oh, I'm not yet back.  Still covered in sand and sea water :)

Molay

Thank you a lot for this nice description!

This also answers  another question I asked somewhere, if it is possible to hide variables in the editor.
This pretty much answers everything I was wondering about :)

Thanks for taking the time to write such a complete and clear reply, despite being on vacation!

Now, I assume being covered in sand is a good thing, yes? *chuckle* :D

thepenguin

#7
No, there is no way to hide variables in the editor.

EDIT: I see that your question was already answered…

Oh well, I tried.

-This was typed with voice recognition software.
We have become the creeper...