• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML Need help with collisions and alarms

C

cobebear

Guest
Hi -

I've been having problems with how to collect resources with a collision event. What I want to happen is
the player collides with the tree to cut some wood. The wood appears in their inventory every five seconds. The problem is that the alarm that I'm using never goes down due to it resetting every step.

I remember this problem having an easy fix and it had something to do with setting the alarm to -1 and an if statement. However, my laptop is broken and I cannot access the previous projects that had it.

Any help would be much appreciated. I've been at this for a couple of hours but I can't seem to get it down.

Thanks!
 

nesrocks

Member
Personally I rarely use alarms. I usually go with a "counter" variable and increment it every step, then check it to know where things are.

With the alarm though, we'd need to see the code. You're probably setting the alarm every frame, when you should check if it isn't running or do something else to start it only once.
 

samspade

Member
Hi -

I've been having problems with how to collect resources with a collision event. What I want to happen is
the player collides with the tree to cut some wood. The wood appears in their inventory every five seconds. The problem is that the alarm that I'm using never goes down due to it resetting every step.

I remember this problem having an easy fix and it had something to do with setting the alarm to -1 and an if statement. However, my laptop is broken and I cannot access the previous projects that had it.

Any help would be much appreciated. I've been at this for a couple of hours but I can't seem to get it down.

Thanks!
Without seeing the code, I'd have to guess, but I think this would work:

Code:
//player creation code

timer = 0;
wood_count = 0;
add_wood_amount = // amount of wood that gets added

//player-tree collision code

if (timer <= 0) {
     wood_count += add_wood_amount
     timer = // whatever value you want to reset your timer to.
} else {
    timer -= 1;
}
 
C

cobebear

Guest
Without seeing the code, I'd have to guess, but I think this would work:

Code:
//player creation code

timer = 0;
wood_count = 0;
add_wood_amount = // amount of wood that gets added

//player-tree collision code

if (timer <= 0) {
     wood_count += add_wood_amount
     timer = // whatever value you want to reset your timer to.
} else {
    timer -= 1;
}
With a few changes to make the code work with the inv system, it worked! I think I was trying to be too complex when the answer was so simple.
Thank you very much, guys!
 
Top