Help explain why picking code behaves a certain way?

Get help using Construct 2

Post » Tue Jun 28, 2016 2:51 am


I have a game which randomly generates some items and I'm working on trying to make sure the items don't ever overlap each other. Do do that I was calling some functions to do rectangle intersection testing (using BBoxLeft,BBoxRight, etc, in expressions)

The bound box code seems to work, but sometimes the objects don't move like I would expect, I think its because they have "just been created", and I'm encountering the infamous "can't pick properly after creation" issue or something.


One thing I don't understand 100% is the "Wait 0 seconds"...."trick".

I put together a small test capx essentially that creates a red sprite randomly anywhere on the screen, and keeps doing so. But after it creates it, it calls a function to take all the rest of the red sprites that already exist, which belong in a family, and tells them all to turn "black" essentially, *IF* their "ID" property does not equal the newly created red sprite's UID.

The weird thing is, this works if you put the code in entirely straight as all sub events.

And it also appears to work even if I call a function to do the for each, the new red box stays red, while all previous ones turn black.

However, if you insert one single "wait 0" in the beginning of the function, now even the newly created red box turns black too! Which to me doesn't make sense..the "ID" property of the red box is set ..

You know what..maybe it's because the function loses its parameters because a "wait 0" goes around for another tick? Maybe the parameter ends up being "empty" or zero? Thus making the condition true for ALL red boxes now?

Screenshot below, of the setup, and the "Wait 0 " in place. If you take out the Wait 0 it works correctly.

Posts: 22
Reputation: 771

Post » Tue Jun 28, 2016 3:04 am


It looks like if I put a "Wait" at the beginning of a function, it loses its parameters. I didn't expect that...

I tested this by putting two textboxes on the screen. The first text shows me the UID of the newly created object.

Then after the "Wait" in the function, I set the second text box to "Function.Param(0)"...and I can see it is ZERO!

So it lost the value that was passed in, after the Wait!

Maybe I should store the value in a local varaible before a wait if I did this?

I'm just curious on how things work and in what sequence in some cases. I would not have expected a function with a wait to lose it's parameters
Posts: 22
Reputation: 771

Post » Tue Jun 28, 2016 3:07 am

Ok, here's what seems to happen.

If you reference the Function.Param AT ALL , BEFORE the wait, it will then remember it.

So, this looks like some sort of reference count or object hold or something

Here you can see, I still have the wait in the function , but I grab the Function.Param(0) before waiting. NOW the code works correctly again!


Still getting used to the way Construct 2 does things, very different from traditional programming
Posts: 22
Reputation: 771

Post » Tue Jun 28, 2016 5:38 am

That's really weird. I'm wondering if that is some sort of bug?
Posts: 1,098
Reputation: 37,933

Post » Tue Jun 28, 2016 3:33 pm

Here are some topics I found on the matter:

There are probably more. The gist of what happens is the wait delays the following actions till later, the function ends, and when the actions finally run the parameters are no longer there.

The cleanest solution is probably not to use that wait 0 trick at all. The events in your pic can be made like this and operate the same.

every 4 seconds
--- create sprite at random location
--- call function "detect"

on function "detect"
--- sprite: set blend to source out

But in the context of what you want to do you can pass the uid to the function and you can pick the new sprite "by uid" and you can pick the rest of the instances with "pick all". To make things simpler a family can be used like below:

// family "otherSprite" with object type sprite

every 4 seconds
--- create sprite at (random(640),random(480))
--- call function "detect" (sprite.uid)

on function "detect"
sprite: pick by uid function.param(0)
sprite overlaps otherSprite
--- sprite: set position to (random(640),random(480))

And just for completeness if you want all the instances to be picked you can set variable or enable a group to run events. There's an example for zordering in the bottom of the post here:
Posts: 5,272
Reputation: 69,455

Return to How do I....?

Who is online

Users browsing this forum: Construct Monkey, shawnzien, tatogame and 6 guests