• 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!

Cleaner way to collect different types of coins instead of several collision events?

I have 10 different types of currency. When the player interacts with a certain type of currency he is given a random amount of money within a given range, depending on what the currency is. Currently I have 10 different Collision events (in oPlayer) that all look incredibly similar. All currency objects are children of the parent object Collectable_parent.
Collision with oCoin:
with(other)
{
audio_play_sound(Coin_collect_SFX, 1, false)
instance_destroy();
}
money += irandom_range(1, 10);

Collision with oRuby:
with(other)
{
audio_play_sound(Coin_collect_SFX, 1, false)
instance_destroy();
}
money += irandom_range(25, 100);

Etc. Etc.

Is there a cleaner way to say
with(other)
{
audio_play_sound(Coin_collect_SFX, 1, false)
instance_destroy();
}
and if (other) is a coin, then: money += irandom_range(1, 10);
if (other) is a ruby, then: money += irandom_range(25, 100);
and so on, in a Collectable_parent Collision event? I've tried playing around with it, but it keeps kicking back errors. Any suggestions are appreciated. Thanks so much.
 

TsukaYuriko

☄️
Forum Staff
Moderator
Use a parent. Give it a variable - let's call it value - which you override in the children's Create events to be whatever the value of the coin is. Collide with the parent, and add the value of that variable to the player's coins.
 

Nidoking

Member
Another reasonable option is to define a function in the child objects that does whatever you want done. In this example, that might be a getMoney() function that returns an integer value, and each coin would define its value accordingly, but for more flexibility in the future, you might call it collectMe() and make it do everything you want done. That way, you could have an item that gives extra lives, or random bonuses of any kind, or damages all enemies on the screen... whatever makes sense to you.
 
In the oPlayer's Collectable_parent Collision event I have:
with(other)
{
audio_play_sound(Coin_collect_SFX, 1, false)
instance_destroy();
}
money += value(other)
In each child object's Create Event I have:
value = irandom_range(1, 10); //the range is different for each one.

However, it still is not working. What am I doing wrong with my code? Thanks.
 
Top