Help explain why picking code behaves a certain way?

0 favourites
  • 5 posts
From the Asset Store
Game with complete Source-Code (Construct 3 / .c3p) + HTML5 Exported.
  • Hi,

    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.

    (references:

    and

    )

    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.

    http://imgur.com/unYwp4H

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Whoa,

    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

  • 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!

    Unexpected!

    Still getting used to the way Construct 2 does things, very different from traditional programming

    http://imgur.com/WwoII6p

  • That's really weird. I'm wondering if that is some sort of bug?

  • 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)

    while

    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:

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)