Dooley was faster than me
But if it's ok, I would like to share my 2 cents too.
Here is a commented code (you can simply run it to see how it works):
var player_health = 100;
action health_taker()
{
while(!player){ wait(1); }
int touching = false;
var touching_counter = 0;
while(my) // never create endless loops !
{
if(vec_dist(player.x, my.x) < 200) // if we are close enough?
{
if(touching == false) // and we yet can do damage to player ?
{
player_health -= 10; // take 10 health points from player
touching = true; // make sure that if statement is going to be false in next loop
// so we wouldn't take any more health points !
}
// if you need to take player's health, let's say 10 points each second
// you can reset 'touching' to false each 1 second
touching_counter += time_frame / 16;
if(touching_counter >= 1)
{
touching = false;
touching_counter -= 1;
}
}
else // far enough ?
{
if(touching == true) // and we previosly touched player ?
{
touching = false; // reset touching switch
}
}
wait(1);
}
}
action player_controller()
{
player = my;
while(my)
{
if(player_health <= 0)
{
break;
}
wait(1);
}
ptr_remove(my);
}
void main()
{
fps_max = 60;
warn_level = 6;
level_load("");
ent_create(CUBE_MDL, vector(128, 32, 0), health_taker);
ent_create(SPHERE_MDL, vector(128, -32, 0), player_controller);
while(!key_esc)
{
DEBUG_VAR(player_health, 0);
wait(1);
}
sys_exit(NULL);
}
There are few thing that I would like to mention.
Firstly this code (same as your original one) doesn't take into account 'touching' between 'health_taker' entity and 'player' entity. It only checks if they are close enough to eachother. To check for touching, I would recommend looking into events. F.e. EVENT_IMPACT or EVENT_ENTITY. But checking for distance might be better way (just keep the distance small enough, so enemies f.e. won't cause damage though walls).
Secondly I would recommend not to use 'player' pointer at all. Because if player pointer will be invalid (f.e. after removing him with safe_remove macros), your 'health_taker' entities will crash/cause errors. As as simpliest workaround, I would use global vector, which will be changed to player's position at the end of the player's main loop (where you move the player) and then, in 'health_taker' action, I would check for distance between that global vector and health_taker's position.
Best regards!