wait next tick

Discussion and feedback on Construct 2

Post » Mon Mar 25, 2013 12:10 am

Hey there,

I just had this simple idea.
It's ver well know, now that when we have a newly created or destroyed object, we need to wait for the next tick to access them.
For instance when we use functions a lot even when the function is called in the created block, the newly created object won't be visible.

My usual work around is to put a wait 0 seconds

But it's not really intuitive. And at some point, nothing insure that this behavior will be kept in future versions (since it's more like a hack). Maybe in the future, the implementation of wait will be changed and wait 0 seconds will trigger instantly... who knows?... and why should we care about internal implementation? (:

So maybe an action dedicated for just that situation, like a "Wait next tick" OR "trigger next tick" action would be better (although "triger next tick" looks more like a condition)

What do you think @Ashley ? (:
B
66
S
22
G
14
Posts: 1,484
Reputation: 16,511

Post » Mon Mar 25, 2013 12:36 am

Created objects are accessible after the next top-level event - you don't really need to wait a full tick. But I guess the easiest workaround is to wait a tick.

Wait 0 seconds is actually deliberately designed to postpone until the end of the current run of events (not even the next tick - the same tick, but after all other events). This is intentional and I won't change it since it will break useful things such as this! So I think it's fine to keep using "Wait 0 seconds" like that.
Scirra Founder
B
395
S
232
G
88
Posts: 24,371
Reputation: 193,762

Post » Mon Mar 25, 2013 1:11 am

oh , good ! I was lookin' for this one for a long time ... Meaning , no wait 1 tick @Ashley ?
B
42
S
17
G
17
Posts: 2,247
Reputation: 17,461

Post » Mon Mar 25, 2013 1:36 am

@Ashley

Yeah I know about the next top level thingy, but that makes a capx look a bit harder to read when you do that:
[code]on left click
-> create object
on left click
-> function call "checkThings"

on right click on object
-> destroy object
on right click on object // should that trigger by the way?
-> function call "checkThings"[/code]

instead of:
[code]on left click
-> create object
-> next tick
-> function call "checkThings"

on right click on object
-> destroy object
-> next tick
-> function call "checkThings"[/code]
(plus you probably would have to repeat some picking condition/calculation to end up with the same context in more complex situations)


And the idea behind creating an alias to wait 0 seconds, is that it looks less wrong ('cause if I understand, you actually wait more or less dt seconds)Yann2013-03-25 01:38:10
B
66
S
22
G
14
Posts: 1,484
Reputation: 16,511

Post » Mon Mar 25, 2013 7:21 am

I've been thinking about this "top level delayed picking" for some time and I like the idea Yann is proposing. If calling it Pick Created Instances (or similar) it would be clear in the code what's going on: 'I now discard any previous pickings and continue to configure the newly created'
B
7
Posts: 2
Reputation: 530

Post » Sat Jun 17, 2017 11:06 pm

Ashley wrote:Created objects are accessible after the next top-level event


Mother. Of. God. Is this documented anywhere?

I've been trying to figure out what's wrong for, like, days. I accidentally figured that "Wait 0 seconds" solves the problem but I didn't know why.

Thanks. I guess.
B
10
S
5
Posts: 14
Reputation: 880

Post » Wed Jun 21, 2017 10:48 am

Just for my own understanding... (because I keep seeing this subject pop up and I never understand it)

I am quite deep into my project,
must be 600 events in total now,
In game I am creating dozens of enemies and in subsequent events or sub events I am changing their instance variables, positions and pinning stuff to them etc at the point of creation. and as far as I can see everything is working well and smoothly. I have never ever needed to use wait 0 seconds for a anything, I don't even know why or when I would need to use it, it sounds like a hack, could someone enlighten me?
...
B
44
S
23
G
7
Posts: 286
Reputation: 7,985

Post » Wed Jun 21, 2017 12:35 pm

NetOne wrote:Just for my own understanding... (because I keep seeing this subject pop up and I never understand it)

I am quite deep into my project,
must be 600 events in total now,
In game I am creating dozens of enemies and in subsequent events or sub events I am changing their instance variables, positions and pining stuff to them etc at the point of creation. and as far as I can see everything is working well and smoothly. I have never ever needed to use wait 0 seconds for a anything, I don't even know why or when I would need to use it, it sounds like a hack, could someone enlighten me?


I use it when I need to evaluate all objects with a function after they are created.

For example I have a radar minimap made with a tilemap. In order to drawn enemy positions on the minimap I have a function, the function uses for each enemy set tile at x = radar.positiontotilex(enemy.x) and y = radar.positiontotilex(enemy.y) to a tile representing an enemy. Since it is not necessary to refresh the radar map every tick (for each is fairly expensive CPU utilization-wise), I only call the radar refreshing function when needed.

That is to say it the bare minimum when it needs to be called is when an entity that can move changes its current location to another tile or when new enemies are created. But if I call it without wait 0 after creating new enemies those new enemies will not show up on the map.

Of course there are workarounds which would achieve the same thing for this example, but they all would require adding more new actions than just "wait 0, call function". Less events is better if the performance penalty is not material (less to change/debug if you decide to revise something). Sometimes it is a matter of convenience, there are probably a few cases where it is absolutely necessary and cannot be achieved otherwise. But to be honest nothing comes to my mind at the moment.

Another use is if you want to make buttons with sprites that have different frames/animation when they are pressed. You can do the pressed/unpressed change in one event:

conditions
mouse over object, left mouse button down

actions
set frame 1 (pressed)
wait 0
set frame 0 (unpressed)

The effect is that once the mouse stops being over the button sprite it will revert to the previous state by itself. However if the mouse is still over the button, the frame 0 will be changed to frame 1 anyway and the user will not see it has changed. No need to check if it was pressed but the mouse is no longer over it.
B
15
S
6
Posts: 85
Reputation: 1,195

Post » Wed Jun 21, 2017 3:25 pm

Ok, good explanation, I get it.
Thanks for taking the time.
Sure shows the flexibility of C2/3.
That second example is very interesting and seemingly efficient.
I tend towards dividing code into small chunks and using functions where possible, which is probably why I haven't had the need to use wait 0.
Cheers.
...
B
44
S
23
G
7
Posts: 286
Reputation: 7,985

Post » Wed Jun 21, 2017 4:01 pm

The Timer behavior also works well, and can be used instead of functions.
However, just like functions it's not great for multiple instances being triggered at the same time with the same call/ tag.
Image ImageImage
B
169
S
50
G
169
Posts: 8,286
Reputation: 108,216

Next

Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 8 guests