To make each inst. do stuff at different intervals

Get help using Construct 2

Post » Fri Apr 20, 2012 11:19 am

Hi everyone,

First of all, here is a stripped down .capx with the single relevant event.



I have a sprite named "Enemy" that has an instance variable named "AttackInterval" Every time the amount of time set in AttackInterval is passed, the value is recalculated with choose or random and a bullet is launched from the sprite.

I would like this recalculation to occur for every instance of the "Enemy" sprite seperately so that the bullets are launched in seperately calculated intervals.

When I say:

Every Enemy.AttackInterval seconds -> Enemy Set AttackInterval to choose (2,4)

It does indeed recalculate the variable values in each instance seperately.

But then it only takes the number of the first picked instance into account and the other instances obey to the picked instance, all firing the bullets together.

I'm almost certain this behaviour is intended for other practical uses but the proper C2 syntax to get this thing occur the way I want it is unclear to me. "Every Self.AttackInterval seconds" can not work as "Self" can not be assigned to system objects and my attempts with "pick all" and "for each" combinations have all failed.

The .capx I linked above demonstrates how each instance can be resized according to their own variable but the rest is lost to me. It's probably a simple matter but assure you, I have spent several days solving other seemingly complex problems with my project and am now legitimately stuck. :p

Thanks all for reading.Wronghands2012-04-20 11:21:02
B
47
S
3
G
5
Posts: 56
Reputation: 4,630

Post » Fri Apr 20, 2012 11:49 am

Adding dt to a variable counts up 1 every second.

So try adding another "AttackTimer" instance variable and doing:

+ Every tick
-> Enemy: Add dt to AttackTimer

+ Enemy AttackTimer >= AttackInterval
+ For each Enemy
-> Run timer event with Enemy

System conditions like "Every X seconds" only run once without any regard for object picking, so usually instance variables is the best way around that.
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,518

Post » Fri Apr 20, 2012 12:47 pm

Thanks but I'm having a hard time putting it to action. :/

Your pseudocode works for telling the instances when to start do things but I can't get attackintervals to work afterwards. Once "Enemy AttackTimer >= AttackInterval" becomes true bullets are fired from all picked instances every tick or I need to pick only one again. (enable the disabled condition in this relevant .capx)

Also I really don't want to make picking to effect the timing itself because my control over timing is crucial for this project.
Wronghands2012-04-20 12:48:19
B
47
S
3
G
5
Posts: 56
Reputation: 4,630

Post » Fri Apr 20, 2012 1:28 pm

@Ashley
Ok I were too early to give up and can't thank you enough.
This works.

I had problems with dt thought. Bullet synchronization was very off that way so I'm adding exactly 1 to AttackTimer every second. I hope I won't be sorry later. :)
B
47
S
3
G
5
Posts: 56
Reputation: 4,630

Post » Fri Apr 20, 2012 3:00 pm

Oops, I forgot to add: you reset the timer with "Subtract Enemy.AttackInterval from AttackTimer". If you just set it to 0, you introduce small errors, e.g. if it was 0.1 over the interval, that gets ignored.

Here's a perfectly synchronised version using dt: http://dl.dropbox.com/u/15217362/attackinterval.capx (using dt is better than using 'every' because it should be more accurate)
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,518

Post » Wed Apr 25, 2012 9:51 am

Working nicely Ashley, thanks again. :)
B
47
S
3
G
5
Posts: 56
Reputation: 4,630


Return to How do I....?

Who is online

Users browsing this forum: blackhornet and 5 guests