Cooldown / Timer

For questions about using Classic.

Post » Tue Jun 29, 2010 8:13 am

Complete programming noob here. This is actually a bit of a logic problem, and I'm sure I'm overlooking something really simple, but:

I've got a special power for my spaceship that can be fired once every 10 seconds (or longer, we'll see).

Condition: On key X pressed
For Loop 0 - 100 (making plenty of bullets)
Set each bullet to random angles

It works perfectly, and inflicts the carnage I like. But like I said, for whatever reason I can't get a cooldown working. I know I have to reinitialize the timer object to 10,000ms at the end of the bullet explosion, and wrap the whole thing in a while loop to evaluate whether times up or not...but getting that work is another thing entirely :P

On a more positive note, having never done any scripting before I've already created something fun in a few hours - so thanks for such a great program!

-A
B
1
G
2
Posts: 4
Reputation: 633

Post » Tue Jun 29, 2010 10:06 pm

Hi. There are several ways to do that. I made an example of a simple one, here:

[url:30raka6l]http://dl.dropbox.com/u/5868916/cooldown.cap[/url:30raka6l]

The text object is just there for a visual cue as to what is going on. For example, try removing the Trigger Once and see what it does. I use this method quite often to ensure that I know what is being executed, when.
B
3
S
2
G
2
Posts: 187
Reputation: 1,449

Post » Tue Jun 29, 2010 10:54 pm

Perfect. Got it working, Thanks so much for the help!

-A
B
1
G
2
Posts: 4
Reputation: 633

Post » Wed Jun 30, 2010 6:59 am

One last question in regards to delays / cooldowns:

I'm trying to add a delay in a for loop, but what looks logical, only spawns 1 object (instead of 10 over 10 seconds)

For "AINum" from 0 to global('MaxAI')
spawn 1 AI
Wait 1 second
resume

Either they all show up at the same time, or just the one. I'm sure it's a simple order thing, but so far I've yet to figure it out.

-A
B
1
G
2
Posts: 4
Reputation: 633

Post » Wed Jun 30, 2010 8:34 am

For loops all occur in the same frame.

What you probably want is to set up a counter variable and initialize it to the number of objects you intend to spawn (10, in your case). Then, just do a check each frame to see if a second has passed, and also if the counter is above zero. If so, decrement the counter and spawn an object.

This has the added benefit of allowing you to seamlessly start spawning more objects simply by setting the counter to a higher number again.
B
2
S
1
G
2
Posts: 48
Reputation: 896

Post » Wed Jun 30, 2010 12:32 pm

How about something like this:

+ System: Is global variable 'EnemyBatch' Greater than 0
+ System: Every 1000 milliseconds
-> System: Create object Enemy on layer 1 at (random(DisplayWidth), random(DisplayHeight))
-> System: Subtract 1 from global variable 'EnemyBatch'

Works like this: as long as EnemyBatch is more than 0, it will repeat once every second. Spawn an enemy, decrement EnemyBatch. Tested and working nicely.
B
2
S
2
G
3
Posts: 105
Reputation: 1,510

Post » Wed Jun 30, 2010 8:53 pm

[quote="GMG":14urv5k7]How about something like this:

+ System: Is global variable 'EnemyBatch' Greater than 0
+ System: Every 1000 milliseconds
-> System: Create object Enemy on layer 1 at (random(DisplayWidth), random(DisplayHeight))
-> System: Subtract 1 from global variable 'EnemyBatch'

Works like this: as long as EnemyBatch is more than 0, it will repeat once every second. Spawn an enemy, decrement EnemyBatch. Tested and working nicely.[/quote:14urv5k7]

That's pretty much exactly what I said, isn't it? :evil:

Seriously though, that's more intuitive than the way I put it. I really should just stick to writing pseudocode instead of over-explaining :)
B
2
S
1
G
2
Posts: 48
Reputation: 896

Post » Wed Jun 30, 2010 9:22 pm

Awesome, thanks again for the help guys. I appreciate both explanations :)

-A
B
1
G
2
Posts: 4
Reputation: 633

Post » Thu Aug 19, 2010 11:00 am

I used a similar cooldown variable in a game of mine.

It's one of the first things I actually got working properly in Construct.
(I use 10 ms and multiples of 10 because of that warning that the milisecond rate isn't accurate
so cool_down = 100 means 1,000 ms.)

Trigger (eg Left mouse is down):
[subevent]
spaceship.value('cool_down') equals 0: fire BFG (or a loop subevent to fire 100)
Set spaceship.value('cool_down') to 10

Every 10 miliseconds
spaceship.value('cool_down') greater than 0: Subtract 1 from spaceship.value('cool_down')

If you hold the mouse button (or whichever condition you set), then each tick it checks the cool_down. If it is not zero, then nothing happens.
B
2
G
2
Posts: 42
Reputation: 734


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 1 guest