GML I can't figure out the alarm system

I

Ibrahim Ahmed Sharif

Guest
Ok so the thing is I'm trying to make a game where you swing the sword around with the mouse, like this:
but I don't want the enemy to get hurt all the time, so I've made a global variable called collisionMask that determines if the enemy can be hurt. I'm trying to do so that every second it turns the collisionMask to 1 (true) but it doesn't work for some reason. Please help.

Weapon create code:
alarm[0] = 1*room_speed​

Weapon step code:
if place_meeting(x, y, obj_enemyParent) && global.collisionMask == 1
{
global.collisionMask = 0;
alarm[0] = 2*room_speed;
}​

Alarm 0 event:
global.collisionMask = 1;​

Enemy step code:
if place_meeting(x, y, obj_playerWeapon) && global.collisionMask == 1
{
hitSound = choose(orcHitSound1, orcHitSound2)
audio_play_sound(hitSound, 10, 0);
instance_create_layer(x, y, "Instances_1", obj_blood);
}​
 

FrostyCat

Redemption Seeker
You are creating a race condition between the weapon and the enemy, where the enemy's Step check can't run if the weapon gets it first. In addition, I don't see any code for deducting health in here.

I recommend that you move the enemy hit code into a User Event (found under Other > User Events, say you use 15):
Code:
hitSound = choose(orcHitSound1, orcHitSound2);
audio_play_sound(hitSound, 10, 0);
instance_create_layer(x, y, "Instances_1", obj_blood);
/* Remember to add code for deducting health here */
Then call that from the weapon's code:
Code:
var inst_enemyParent = instance_place(x, y, obj_enemyParent);
if (inst_enemyParent != noone && global.collisionMask == 1)
{
    with (inst_enemyParent) event_user(15);
    global.collisionMask = 0;
    alarm[0] = 2*room_speed;
}
 
I

Ibrahim Ahmed Sharif

Guest
You are creating a race condition between the weapon and the enemy, where the enemy's Step check can't run if the weapon gets it first. In addition, I don't see any code for deducting health in here.
I don't know how it works, but it works. So thanks!
 
Top