How do I use Wait in a Function

Get help using Construct 2

Post » Mon Aug 31, 2015 9:24 pm

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
B
4
Posts: 4
Reputation: 208

Post » Mon Aug 31, 2015 10:13 pm

It's working fine for me when I create a similar setup. There may be something wrong with your events elsewhere.
Moderator
B
113
S
41
G
20
Posts: 1,586
Reputation: 19,126

Post » Tue Sep 01, 2015 9:07 am

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.
New to Construct ? Where to start

Image Image

Image Image

Please attach a capx to any help request or bug report !
Moderator
B
294
S
117
G
96
Posts: 7,312
Reputation: 71,092

Post » Tue Sep 01, 2015 11:10 pm

Yea, object references and asynchronicity are the bane of my Construct existence.
https://www.ravenheart.ca/home
I don't check the forums much anymore, but I will receive an email for PMs.

"Someone once told me I bite off more than I can chew...

I told them I would rather choke on greatness than nibble on mediocrity."
B
23
S
6
G
1
Posts: 1,419
Reputation: 4,857

Post » Wed Sep 02, 2015 10:56 am

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.
B
4
Posts: 4
Reputation: 208

Post » Sun Sep 27, 2015 1:33 am

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?)
B
100
S
33
G
16
Posts: 1,204
Reputation: 16,865

Post » Mon Sep 28, 2015 1:50 pm

@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.
New to Construct ? Where to start

Image Image

Image Image

Please attach a capx to any help request or bug report !
Moderator
B
294
S
117
G
96
Posts: 7,312
Reputation: 71,092

Post » Tue Sep 29, 2015 1:49 pm

@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!
B
100
S
33
G
16
Posts: 1,204
Reputation: 16,865


Return to How do I....?

Who is online

Users browsing this forum: Christmas and 20 guests