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

Changing other's variables

E

Emberex

Guest
I have an enemy that follows a grid to a "target" unless the aggro is up enough, then it will chase the player. I made a weapon to hit the enemy and I want it to stop them from moving. This is what I tried:
GML:
zNear = instance_nearest(x,y,oZombie);
if(place_meeting(x,y,oZombie)){
    with(zNear){
        speed = 0;
    }
    instance_destroy();
}
But that isn't stopping them, they completely ignore it. What is the reason? here is the code to chase the targets/player:
Code:
    if(zAggro <= 50){
    if(instance_exists(oTarget)){
          if(mp_grid_path(global.grid, path, x,y, pPosX,pPosY,1)){
            path_start(path, zSpeed, path_action_stop,false);
        }
    }
    ///Checking for player if target does not exist
    } else if (zAggro > 50){
        if (instance_exists(oPlayer)){
            if(mp_grid_path(global.grid, path, x,y, oPlayer.x,oPlayer.y,1)){
            path_start(path, zSpeed, path_action_stop,false);
    }
    }
    }
 
You should switch up to using a state machine. Have a chase state, an idle state and a "stunned" state or whatever the pause is when hit. It'll make the code a lot cleaner. What's probably happening is that you are stopping the zombie for a single frame (if that) then the movement code takes back over and makes it chase. If you use states, you can have a timer count down before it switches out of the stunned state and starts moving again.
 
E

Emberex

Guest
You should switch up to using a state machine. Have a chase state, an idle state and a "stunned" state or whatever the pause is when hit. It'll make the code a lot cleaner. What's probably happening is that you are stopping the zombie for a single frame (if that) then the movement code takes back over and makes it chase. If you use states, you can have a timer count down before it switches out of the stunned state and starts moving again.
I will probably mess up the entire code if I try that. I don't know what would go where... Super new to this
 

Joe Ellis

Member
I think the first part should be like this:

GML:
zNear = instance_nearest(x,y,oZombie);
if(place_meeting(x,y,zNear)){
zNear.speed = 0;
instance_destroy();
}
 

Nidoking

Member
Why do people use instance_nearest and place_meeting together?

instance_place literally returns the instance ID you're looking for, or noone if you're not touching an instance.
 
N

NoFontNL

Guest
I don't copy code. I have been writing this myself and looking up what I need to. Maybe you should ask questions before judging. I am still learning and trying.
So here you are saying you are still learning and trying.

I will probably mess up the entire code if I try that. I don't know what would go where... Super new to this
And here you say you don't want to try, because it will 'probably' mess up your code.

You could copy your project files. Open the copy and try stuff out. Or even create an empty project if your game is not that complex yet. There are lots of tutorials on state machines on the internet. You can also use a backup tool such as Github desktop to save your work to the cloud and undo changes if you messed up.
 

Yal

šŸ§ *penguin noises*
GMC Elder
I have an enemy that follows a grid to a "target" unless the aggro is up enough, then it will chase the player. I made a weapon to hit the enemy and I want it to stop them from moving. This is what I tried:
GML:
zNear = instance_nearest(x,y,oZombie);
if(place_meeting(x,y,oZombie)){
    with(zNear){
        speed = 0;
    }
    instance_destroy();
}
But that isn't stopping them, they completely ignore it. What is the reason? here is the code to chase the targets/player:
Code:
    if(zAggro <= 50){
    if(instance_exists(oTarget)){
          if(mp_grid_path(global.grid, path, x,y, pPosX,pPosY,1)){
            path_start(path, zSpeed, path_action_stop,false);
        }
    }
    ///Checking for player if target does not exist
    } else if (zAggro > 50){
        if (instance_exists(oPlayer)){
            if(mp_grid_path(global.grid, path, x,y, oPlayer.x,oPlayer.y,1)){
            path_start(path, zSpeed, path_action_stop,false);
    }
    }
    }
When moving on a path, instances are locked to the path (meaning, even if you move them around they will move back onto the path the next movement tick) and use path_speed instead of the normal speed/direction system. You will need to use a state machine approach with the following states:
  • Following the path
  • Being knocked back (stops following the path temporarily)
  • Move back to the path (when being knocked down, remember the last path position you were at; move back to that point in this state using, say, linear interpolation between current and remembered position)
The reason we want a "move back to path" state is so the enemy won't just instantly teleport back to the path (which looks ugly)
 
E

Emberex

Guest
When moving on a path, instances are locked to the path (meaning, even if you move them around they will move back onto the path the next movement tick) and use path_speed instead of the normal speed/direction system. You will need to use a state machine approach with the following states:
  • Following the path
  • Being knocked back (stops following the path temporarily)
  • Move back to the path (when being knocked down, remember the last path position you were at; move back to that point in this state using, say, linear interpolation between current and remembered position)
The reason we want a "move back to path" state is so the enemy won't just instantly teleport back to the path (which looks ugly)
Thank you :D
 
E

Emberex

Guest
So here you are saying you are still learning and trying.


And here you say you don't want to try, because it will 'probably' mess up your code.

You could copy your project files. Open the copy and try stuff out. Or even create an empty project if your game is not that complex yet. There are lots of tutorials on state machines on the internet. You can also use a backup tool such as Github desktop to save your work to the cloud and undo changes if you messed up.
I never said I wasn't going to try it, read the reply again. I said I will probably mess it up, but I have made a backup and am attempting it now.
 
Top