Statement execution order - can somebody explain?
hardkraft
Member Posts: 28
Hi,
I created a simple test project which gives very unexpected results.
I have a game variable var1. Inside an actor I initialize it to 1, then display it’s value on the screen, then I have a rule if var1=1 and have a debug statement inside that rule as well as text display of var1.
At the end of the actor code I reset var1 to 0
https://dropbox.com/s/jfrqd2wjnvm8lni/Screen%20Shot.JPG
What happens:
- first display shows 0 even though I initialized var1 to 1 just prior
- displays inside the rule don’t execute, which would make sense if it thinks that var1 is 0 however log statements Do execute and show var1=1
Can anyone explain what’s going on?
Attached is the copy of the project:
https://dropbox.com/s/xseaoiuccapf3xz/test1.gameproj.zip?dl=0
Comments
You might be better off sticking to GameSalad terms to avoid confusion, I take it a variable is an attribute and you are using 'initialize' to mean change a value with the change attribute behaviour, or maybe by 'initialise' you mean you change the default value of the attribute ?
Are you asking why you don't actually see the text updating on screen? If so it's because it all happens so quickly (in milliseconds) so the zero is the only thing that ever gets drawn to the screen.
If you add a Timer Behavior around your three bottom rules so it looks like:
You can see the numbers update on screen...
http://jamie-cross.net/posts/ ✮ Udemy: Introduction to Mobile Games Development ✮ Learn Mobile Game Development in One Day Using Gamesalad ✮ My Patreon Page
Thanks, that’s correct.
Thanks, but that doesn’t explain why if I remove the initial text output none of the text from the inside the rule gets outputted to the screen. The box just stays blank.
You have a change attribute at the bottom of your rules thats changes it back to 0. rules run top to bottom, layers run bottom to top.
I tried the timer and it did work, sort of. It displayed the value of 0 which I assume is because I reset it back to 0 after the rule. The question still is why nothing gets displayed without the timer.
because its too fast without a timer.
I’d expect them to run top to bottom. I imagine they should go like this:
It doesn’t seem to be happening in that order. Do you know the order that it’s actually happening and why?
Sorry, I don’t follow. Too fast to display anything? The rule gets entered, the log says so, but the text next to the log does not display.
. . . are using 'initialize' to mean change a value with the change attribute behaviour, or maybe by 'initialize' you mean you change the default value of the attribute ?
Ah, the old 'answer-'Yes'-to-a-choice-question' method, lol
In this case I believe it’s both. I change the attribute which changes the default value that was in it.
I provided the screen shot if you have any questions.
GameSalad runs very fast and the change from 1 to 0 is to fast for you to see but it does happen your using the same attribute to display the same thing so you only see a 0.
heres a trimmed down version of your code does same thing.
Honestly I think the changes occur so quickly when the program runs, you just don't 'see' it.
http://jamie-cross.net/posts/ ✮ Udemy: Introduction to Mobile Games Development ✮ Learn Mobile Game Development in One Day Using Gamesalad ✮ My Patreon Page
But you trimmed away the most important part, the rule. It’s the rule that’s causing the issues. Things execute ok outside the rule but not inside of it.
One is achieved without a behaviour by simply changing the default value, one is done using a behaviour, the point was really one about avoiding using non-GameSalad terms as they might introduce some confusion, it was just a suggestion, you can use whatever language you like of course.
Maybe the confusion stems from the first display behavior. Did you have a chance to look at the code I posted? If you turn off the first display the code should enter the rule and display something. Why doesn’t it display anything at all even though it does enter it?
your rule did nothing thats my point the result was still attribute changed from 0 to 1 back to 0 here your test a little different with timers you can watch what happens.
The example I provided is simple but the point is to see what happens inside the rule and in which order it gets executed, the use of the rule is the whole point. As of now it looks the rule gets executed AFTER the reset of the attribute even though it comes BEFORE it (but not really since the log statements do get executed in the proper order).
Can you see a few posts up I listed the order that I think it should execute in. Are my assumptions wrong, could you list what you think gets executed and in what order?
I did another test. I switched off the attr change at the bottom. So basically var1 gets 1 at the top and that’s it. Now the rule enters correctly. Why would attr change have an effect on the rule that comes before it?
because rules in actors run from top to bottom, and the layers read from bottom to top and I get what your asking now see new test. the top has a timer so you can see it happen the bottom has no timer and will happen instant.
top uses var1
bottom I made a var 2
Here is what I see in your source file:
And my debug log says
Which is exactly what I'd expect. You just never see the display texts being 1 because it all happens so quickly... it only ever shows 0.
http://jamie-cross.net/posts/ ✮ Udemy: Introduction to Mobile Games Development ✮ Learn Mobile Game Development in One Day Using Gamesalad ✮ My Patreon Page
@jamie_c
Great, looks like we are on the same page. Could you tell me why you wouldn’t expect any of the displays to display anything?
Besides the fact that it happens to fast to display, no. Honestly, it's just happening so fast you can't see it... by the time you change var1 to 0, that is all you see displayed.
Adding Timers to slow it down, like BBEnk and I have done already, allow the text to display.
http://jamie-cross.net/posts/ ✮ Udemy: Introduction to Mobile Games Development ✮ Learn Mobile Game Development in One Day Using Gamesalad ✮ My Patreon Page
@jamie_c I thought that once something is displayed it stays on the screen, it doesn’t display and then disappear. If you remove the very top display the screen will remain blank.
Besides, when I remove the bottom attr change everything displays as expected, why?
Also, why do logs display but not the display texts which are right next to them?
@BBEnk, thanks for the effort.
From I can see in the top box:
that’s pretty clear, thanks. Then would you say my code executes like this:
Is my understanding correct?
The reason it would change when adding the timers is that the timers triggering force it to update. If you are asking why doesn't your code display only a 1, again its probably because it changes from 1 to 0 so fast it never actually displays the 1 on screen.
Because you are never changing it to 0. If you change the bottom 0 to 100 it will display 100. Because it's the last Change to happen, it's what gets displayed.
Logs 'log' the action. Other action is too fast to display on screen I'm guessing...
Not sure why that happens....
http://jamie-cross.net/posts/ ✮ Udemy: Introduction to Mobile Games Development ✮ Learn Mobile Game Development in One Day Using Gamesalad ✮ My Patreon Page
yes, this is the main mystery to me. I change it to 0 AFTER I’d expect it being displayed as 1 inside the rule. The debug does see it as 1 inside the rule and it’s not too fast, but somehow the display that’s right next to it doesn’t do anything. That’s what I don’t understand.
OK, take my original project and remove the very top display. Now you’ll see that no displays inside the rule ever get triggered. Does that make sense?
For clarification I’m posting the new project here. The only change is that I removed the top display (the cause of all the confusion). Now the text box remains blank.
https://dropbox.com/s/xseaoiuccapf3xz/test1.gameproj.zip?dl=0