How do I use Wait in a Function

0 favourites
  • 8 posts
  • Hello guys,

    I am searching for a while now and could not find any answers to my problem.

    I am trying to work a little with functions in construct 2 but whatever I do I cannot get "Wait" to work inside a function. It will just ignore the wait :/

    Here is a screenshot:

    http://prntscr.com/8b5415

  • It's working fine for me when I create a similar setup. There may be something wrong with your events elsewhere.

  • Never forget functions do not pick the instances picked from the event that calls them.

    But otherwise, yes, the wait action should work as intended in such a code.

    The answer is elsewhere.

  • Yea, object references and asynchronicity are the bane of my Construct existence.

  • Thanks for the quick answers. It always ignores the wait and instantly does the stuff after the wait when I call it through this function. Dont know why exactly.

  • I have a related question Kyatric

    if you use a Wait in a function and call the function with a param, does it asses the param source after the wait?

    i.e.

    Player OnCollision with Item

    Call Function ShowItemAchievement(Item.text)

    delete Item

    Function ShowItemAchievement

    Wait 1

    Set DisplayText to Function.Param(0)

    My code always shows the item text as 0 even though I am passing it string. My guess is the Param reference doesn't happen until after the Wait and since it has been destroyed it takes empty memory which is zeroed out. Can you confirm this?

    Maybe I need it to hold the param in memory.. I could probably use a local variable in the Function, but not sure how that would respond if I had 2 Item Collisions in a row (would it stack up? or overwrite the local variable?)

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • jobel: In the absolute the code would work, and the Item instance you are referring to should be the one picked.

    The issue there is as you suspect that you execute the function (that registers the action but waits for one complete second before executing it) and order the destruction of the instance (which happens right at the next tick).

    So in your current code C2 picks the default instance (0) since the expected instance no longer exists.

    Holding the value of the instance instead of the reference to the instance could indeed be a solution, and indeed in case of multiple collisions, only the last collision would be kept in memory if you use only a single variable to hold it.

    An alternate (but complex) solution there is to keep the values in an array that you dynamically resize as collisions go and remove the current index (either back or top according to the "direction" you want to go).

    This is pretty much LIFO-FIFO stack management.

  • Kyatric thanks, that helps.. all I'm trying to achieve is a short delay before displaying an achievement. When my player collects items I have sfx and particle fx... when the player collects a rare and special item I want to display an achievement banner but I don't want it to interfere with the normal effects of picking up an item...hence the delay. Conceivably, a player could pick up two specials consecutively which I'd want to account for...

    I messed around with making the object invisible (OnCollision) and shutting off collisions then destroying it later, but that feels a tad messy...

    the lifo approach seems like overkill but it may be the best option...thanks!

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