alarms(creating a power up)

jermaine

Member
im creating this power up thing where if my player comes in contact with it he'll speed up for a certain amount of time.
and...I believe alarms is something I will need in this and Im still having trouble learning how to use alarms even after watching some tutorials...
this is what I have right now.
GML:
if(place_meeting(x,y,player))
{
    alarm[0] = room_speed
    speedup = true
}
if speedup = true
{
    player.hsp = player.hsp*2
}
and in alarm 0 event:
speedup = false.
im pretty sure this is very inefficient or jus straight up dumb but I don really understand alarms lol.
all the tutorials ive seen so far show that the alarm is created and starts counting down in the create event.
but if it counts down in the create event then it wont actually work in this case since I want it to start counting down when I touch it.
 

Nidoking

Member
Why are you modifying player speed in the powerup instance? Why not have the player manage its own powerup state, set something that will double the speed, and set an alarm in the player to end the powerup at the right time? That's particularly important if you want to destroy the powerup once it's activated.
 

curato

Member
yeah that is likely your problem you should set the have the speed up and alarm be part of player object. If you have multiple speed up objects that will likely mess things up. Also you should store the desired speed seperate from the the hspd. If you say hspd = hspd * 2 ever time it is true your speed will go out of control.
 

jermaine

Member
ok I took ur guys advice and put it in the players code.
right now I used a collision event that activates when I collide with the thing and this is wut I wrote
alarm[1] = room_speed *10
hsp *= 10
in the alarm event its jus hsp /= 10
and well I made such drastic changes was to jus so that I could see the difference even if it was small.
and well now when I collide with it.
it sort of teleports me a very short distance and then my speed goes back to normal.
I think its pretty obvious that the problem is with alarm[1] = room_speed*10
but im not sure how to fix it
since I basically do not know anything about alarms lol
 

woods

Member
I think its pretty obvious that the problem is with alarm[1] = room_speed*10
alarm[1] = room_speed*10 //alarm 1 fires in 10 seconds

it would be easier for people to help if you posted your actual updated code ;o)

but im not sure how to fix it
since I basically do not know anything about alarms lol


alarms are pretty easy once you figure out how they work.. the hard part is getting your logic to flow properly BEFORE you start to code it ;o)

here is a quick example of using alarms
limiting a players rate of fire...

Code:
//obj_player create event
bullet_cooldown = room_speed * 2 //set this to whatever you want the delay to be.. room_speed*2 is 2 seconds

//obj_player step event
if (canShoot) {
    if (mouse_check_button_pressed(mb_left)) {
        canShoot = false;
        alarm[0] = bullet_cooldown;
        // Bullet creation + aim at mouse
        with instance_create(x, y, obj_bullet)
            direction = point_direction(other.x, other.y,  mouse_x, mouse_y); 
    }
}

//Alarm 0 Event:

canShoot = true;
all this happens in the player object so its all easy to find and change ;o)

set the bullet delay(cooldown timer) in the create event
activate the alarm in the step event
reset the alarm in the alarm event
 

jermaine

Member
ok...I adjusted it as you said.
now in the create event I have this.
cooldown = room_speed * 10

and in the step event I have this:
if(place_meeting(x,y,super))
{
hsp *= 2
alarm[1] = cooldown
}

and in alarm[1] event:
hsp /= 2

but right now its not speeding up at all its like nothing is happening
EXCEPT when the alarm ended I could feel the player slow down for jus a frame or something which must be the alarm ending.
although I didnt feel the speedup and it felt way longer then 10 seconds lol
 

jermaine

Member
erm well i press the two arrow keys right and left to move ig.
GML:
if move == 1
{
    hsp += acc
    if (hsp >= maxspeed) hsp = maxspeed
}
else if (hsp > 0)
{
    hsp -= acc
    if(hsp <= 0) hsp = 0
}

///move left
if move == -1
{
    hsp -= acc
    if (hsp <= -maxspeed) hsp = -maxspeed
}
else if (hsp < 0)
{
    hsp += acc
    if(hsp >= 0) hsp = 0
}
and in addition to that theres also this at another section of the step event
Code:
right = keyboard_check(vk_right)
left = keyboard_check(vk_left)
up = keyboard_check(vk_up)

///main actions

if (hsp != 0) image_xscale = sign(hsp) * sc
move = right - left;
x += hsp
y += vsp
 

Nidoking

Member
I don't think you want the activation of the powerup to affect hsp directly. It doesn't look like you're explicitly resetting hsp and vsp each step, which is what I would have expected, but you are applying the same changes regardless of the powerup state, and it doesn't appear that you're changing maxspeed when you're powered up, so the hsp value is still capped at the same value. You'd never see an increase beyond that.

Now, if you were to write your movement incorporating a check for a powerup variable, and when it's active, you use things that would make your instance move faster, then you'd only have one variable to turn on and off appropriately.

Also, I don't think you should apply hsp and vsp before you've handled your movement and collision checks. You're always going to be a step behind that way.
 

jermaine

Member
ok so i basically don't understand half of what you said but i'll try lol.
so first of all what do you mean buy resetting hsp and vsp each step?
you use things that would make your instance move faster, then you'd only have one variable to turn on and off appropriately.
and does that mean instead of changing the hsp, i should be changing the maxspeed and the acceleration when a certain variable is turned on?
because if not what else would i use to change the speed of the player/instance.

and last of all,
I don't think you should apply hsp and vsp before you've handled your movement and collision checks
does this mean like i should be putting my collision checks with walls and stuff infront of my movement?
 

Nidoking

Member
so first of all what do you mean buy resetting hsp and vsp each step?
Set: vb. To assign a value to something, such as a variable
Reset: vb. To set to an earlier or default value

and does that mean instead of changing the hsp, i should be changing the maxspeed and the acceleration when a certain variable is turned on?
because if not what else would i use to change the speed of the player/instance.
Work with me here. Think for just a moment. What happens if the maxspeed is, say, 5, and you set the speed to 10? What will the final, functional speed actually be? Because 10 is more than 5, and 5 is the highest value it can have. In short, if you don't change the maxspeed, then there's no change in functional speed.

does this mean like i should be putting my collision checks with walls and stuff infront of my movement?
I'm going to have to ask you to think for another moment. If there is a wall within hsp pixels in the direction of keyboard movement, then x += hsp is putting your instance inside that wall. You're also setting image_scale and x based on hsp before you set hsp based on acc and maxspeed. These things have to happen in a certain order. Do you read forum posts from bottom to top, or in some random order that makes sense? I would like to say you don't, but it's hard to tell at this point. Hopefully, you read top to bottom, the way English is designed to be read. Does it surprise you to learn that the program works that way too? If you use the value of hsp to calculate something, then change the value of hsp later, the earlier calculation is stale because changing hsp later doesn't change any of the things you used the old value to calculate. You should update hsp first, THEN use the new value of hsp to determine the things that hsp affects. Likewise, when you're walking, how often do you walk into walls because you're not paying attention? The more correct thing to do is first to check whether there is a wall, then, if there is no wall, walk as far in that direction as you've decided to. If there is a wall, you will need to know how far you can move without hitting it, and move that far. Or ask "Can I move just one pixel?" and if so, do it, and then repeat until you can't go any farther. The way you've written it, you're first ramming right into the wall, then checking to see whether you're currently embedded in a wall, and finally asking "Oh, wait, how far was I supposed to go?"

In short, always wait until you've determined the correct value for a variable before using it for anything each step.
 
Top