• 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 power ups.

DataBass

Member
Hey its me again.
Im trying to figure out a way to make power ups that affect the character for as long as the game goes on. so think kirby air ride.
Get a heart hp goes up, pick up a boot and speed goes up, jump height changes with wing power up etc.

at first i was thinking that i could just add an amount of that stat to the already established code

example:
when you collide with the speed power up, destroy the power up and add a number to the horizontal speed
when you collide with the jump power up, destroy the power up and add a number to the vertical speed

the problem is im not sure how exactly i would write this out in code if it is the answer im looking for
 

SoapSud39

Member
It's not too difficult. As long as your stats are variables it's really easy to change them. It'd be a different story if they were constants, but they're clearly not from your other post.

It could be something as simple as
GML:
//player object, collision code with power-up object
hp++; //or whatever stat variable
with (other) {
    instance_destroy();
}
If you have max health or speeds, be sure to change that, as well as any other variables that might affect movement (acceleration, jump speed, etc.) and maybe actual values (current health, mp if you have that, etc.)

If you want to change certain amounts based on the power-up, you could do something like hp += other.hp_gain;.

There are probably many ways to do it, but this is a pretty simple method that works.
 

DataBass

Member
okay that works but only when moving to the right. also what if i want to increase it by the amount i choose.
 
Last edited:

SoapSud39

Member
If you're doing a horizontal speed power-up, don't just change the horizontal speed variable, but rather change the max horizontal speed variable. This should change the speed in both directions if you only have one max speed variable, since that is independent of the pos/neg value you multiply it by in your step event. If this isn't what you're doing maybe post your code and we can help you better.


If you have different objects for each power-up, write how much you want the value to increase by.
e.g. for a health power-up you might have hp_gain = 5; and then in the collision code for the health power-up you can increase hp by 'other.hp_gain' (this is what I meant with my example above).

If you want to have all the power-up values in one object then you can do a series of if statements or switch/case statements to get the power-up type and increase accordingly: e.g.
GML:
switch (other.powerup_type) {
    case "health": hp += 5; break;
    case "speed": max_speed += 2; break;
    case "jump": jump_speed += 2; break;
}
where you would establish the variable 'powerup_type' in the power-up object e.g. powerup_type = "health";
(it would be better to do these as enums rather than strings)
 

DataBass

Member
before i send the code. im curious if you're saying i should just make a script for all the items and then set their properties so i can use them where ever i want or if im over complicating things
 

SoapSud39

Member
Not so much a script but more an object. What I mean is one option is to make separate object assets for each power-up, and when the player collides with an instance of the object, it'll gain stats based on the attributes of that object, which you establish in its code. Another option would be to make a singular power-up object, and then when you create an instance you can set its 'powerup_type' to be a random one (either in the create event or by using with (instance_create_layer(...) {...}) and then when the player collides with an instance it can get the 'powerup_type' and increase stats accordingly.

Of course, there are many methods, but these were the suggestions I was getting at.
 

DataBass

Member
i got the speed up to work. it was because of the other code that i needed help in that it didnt wok but after i removed the hsp line it worked. so for vertical speed (vsp) would i have to make a max of that in order to have the jump power get stronger because right now the powerup just sends me slightly up in the air

code for jumping here:
GML:
if(key_jump && jump_current > 0)
{
    vsp = -6;
    jump_current--;
}
if(place_meeting(x, y + vsp, oWall))
{
    while(!place_meeting(x, y + sign(vsp), oWall))
    {
        y += sign(vsp);
    }
    if(vsp > 0)
    {
        jump_current = jump_number;
    }
    vsp = 0;
}
y += vsp;

if (vsp < 0) and (!key_jump_held) vsp = max(vsp,-2.8)
Create code here
GML:
hsp = 0;
vsp = 0;
grv = 0.22;
walksp = 6;
max_hsp = 7;
acceleration = 0.57;
frc = 0.25;
controller = 0;


jump_number = 1; //How many jumps the player can make
jump_current = 0; //How many jumps the player has remaining
 

SoapSud39

Member
So right now your jump code is vsp = -6;, but what you can do is declare a 'jump_speed' variable in create jump_speed = -6; and then for you rjump code use vsp += jump_speed;. Then for your jump power-up it would be as simple as setting 'jump_speed' to a bigger number like jump_speed = -8;.
 
Top