only some enemies are moving?

jermaine

Member
right....so I just fiddled around with some of the coding in my enemy's code and I am well aware I am still considered very new to coding as most of the code is just taken from YouTube videos lol.
anyways I have this bunch of code and I don't know where it went wrong so im jus gonna put it all here.
GML:
hsp = dir * movespeed
vsp += grav;
if (hsp != 0) image_xscale = sign(hsp) * sc
if (place_meeting(x,y,player))
{
    game_restart()
}
var enemyvar = instance_place(x,y,enemy)

with (enemyvar)
{
switch(states)
{
    case states.stationary:
        hsp = 0
        if (abs(x - player.x)<64)
        {
            states = states.chase
        }
        states = states.chase
        break;
    
    case states.chase:
        #region hsp vsp collision
        if (place_meeting(x+hsp,y,wall))
        {
            while(!place_meeting(x+sign(hsp),y,wall))
            {
            x += sign(hsp)
            }
            hsp = 0;
    
            dir *= -1;
        }

        //vertical
        if (place_meeting(x,y+vsp,wall))
        {
            while(!place_meeting(x,y+sign(vsp),wall))
            {
            y += sign(vsp)
            }
            vsp = 0;
        }
        x += hsp;
        y += vsp;
        #endregion
    break;
}
}
I suspect its around the area where the state of the enemy is still in stationary.
so right now what's happening is that I have several enemies in the room and all of em r spread out.
but now when I go close to them.they don't move at all.they are stuck in stationary state or something??idk
then only two enemies start moving and idk if this is related but they are very close to each other.
they were basically placed next to each other.
 

jermaine

Member
so that is suppose to make it...apparently only affect that one instance.because it before this, it was basically moving all at once when it detected I was 64 pixels near it as seen in the stationary event.instead of only the one that detected the player moving, all of em moved.
regarding what I actually wrote in that code.i don't REALLY understand it....it was jus a line of code from a previous thread I posted that I thought could solve the problem.
 

Nidoking

Member
In that case, I'm going to suggest that you take the time to try to understand what you're doing. Watch the videos again with the intention of understanding what they're doing, why, and how. Read the sections in the Manual that describe the functions and logical constructs that you're using. Never, ever, ever copy something you found on the Internet unless you can explain what it does, how, and why you expect it to fix your problem. "I don't know what I'm doing lol" is not a good methodology for anything.
 

muki

Member
I feel like the enums might be a problem. They are global scope, so it's possible all your enemies are sharing the same states. In each instance you are trying to set 'states' to something based on the local conditions of the instance. If you're doing this for every enemy in the room, it won't work because they're sort of all sharing the same mind, in a sense.

I'm not super awake though so I might be wrong. I don't use enums for states personally, specifically because they are global. That means I also don't have much experience with them, so maybe someone else can chime in.

And yeah, don't "self find" instances in this way. You don't need to, and if anything it'll make things complicated and buggy. Even if that isn't the source of the original problem.
 
Last edited:

chamaeleon

Member
I feel like the enums might be a problem. They are global scope, so it's possible all your enemies are sharing the same states. In each instance you are trying to set 'states' to something based on the local conditions of the instance. If you're doing this for every enemy in the room, it won't work because they're sort of all sharing the same mind, in a sense.

I'm not super awake though so I might be wrong. I don't use enums for states personally, specifically because they are global. That means I also don't have much experience with them, so maybe someone else can chime in.

And yeah, don't "self find" instances in this way. You don't need to, and if anything it'll make things complicated and buggy. Even if that isn't the source of the original problem.
Saying that you don't use enums because they are global is like saying I don't use numbers like 1,2, 3, because they are global. Enums are just pretty names for numbers. It is the variable you assign them to that matters and how you use it and what its scope is. If the variable is global state may be shared. If the variable is an instance variable the state is not shared, regardless of what kind of enums are used and regardless of whether they have overlapping ranges of values, and only the object code handling of these values matter. Same object, same code, but different data will not lead to shared state unless that is desired and implemented as such.
 

muki

Member
Saying that you don't use enums because they are global is like saying I don't use numbers like 1,2, 3, because they are global. Enums are just pretty names for numbers. It is the variable you assign them to that matters and how you use it and what its scope is. If the variable is global state may be shared. If the variable is an instance variable the state is not shared, regardless of what kind of enums are used and regardless of whether they have overlapping ranges of values, and only the object code handling of these values matter. Same object, same code, but different data will not lead to shared state unless that is desired and implemented as such.
Thanks for the correction and clarification. Enums aren't really my thing.

OP: I have no idea. Disregard my previous message.
 
Last edited:

jermaine

Member
In that case, I'm going to suggest that you take the time to try to understand what you're doing. Watch the videos again with the intention of understanding what they're doing, why, and how. Read the sections in the Manual that describe the functions and logical constructs that you're using. Never, ever, ever copy something you found on the Internet unless you can explain what it does, how, and why you expect it to fix your problem. "I don't know what I'm doing lol" is not a good methodology for anything.
ok sure!
but there are two lines of code where I took it from the forum and I still don't really understand it...I understand it somewhat...but I don't think its enough for me to actually fix it..
I looked it up on the manual but I still don't understand.
its the two lines where first, I use "abs"
and second is where I well, find self using var.

could you maybe enlighten me on those two?
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Moderator
abs() simply means that the number returned will always be positive. So:
GML:
a = -100;
show_debug_message(a);
show_debug_message(abs(a);
This code would show:

-100
100

var is for defining LOCAL variables. A local variable is a "throw away" variable that only exists for the duration of the event or the function it was declared in. They are incredibly useful for calculating intermediate values that are only required for a specific calculation in a single event or function, as well as a number of other things (for example, they are no scoped to the instance or struct using them, which means they are a good way to transfer values between items).

Hope that helps. :)
 

woods

Member
they are stuck in stationary state or something??idk

draw event of obj_enemy add some debug text
something like...
Code:
draw_self();
draw_text(x, y-32, "state:  " +string(states));

with your distance check for the player, you should see the state drawn over the enemy change when they get close enough
if (abs(x - player.x)<64)
{
states = states.chase


//simple debug blips like this can often tell you alot about what is and isnt going on..
// ex: is your state even firing or when certain variables are being manipulated, etc..
//; can deff help reduce the "or somethings" and "IDKs" ;o)
 

jermaine

Member
well I tried the debug thing and yea I guess I was correct.the enemies didn't change state, they remained in stationary except for those two enemies.
abs() simply means that the number returned will always be positive. So:
GML:
a = -100;
show_debug_message(a);
show_debug_message(abs(a);
This code would show:

-100
100
well could u explain how this helps me detect the player when its near like in my code? what does turning it to positive do here.
var is for defining LOCAL variables. A local variable is a "throw away" variable that only exists for the duration of the event or the function it was declared in. They are incredibly useful for calculating intermediate values that are only required for a specific calculation in a single event or function, as well as a number of other things (for example, they are no scoped to the instance or struct using them, which means they are a good way to transfer values between items).
ye....no I got as far as to understand its a local variable and from then I don understand a single thing.
 

Nidoking

Member
could you maybe enlighten me on those two?
I will attempt, but you are showing a remarkable inability to understand basic concepts in the replies above. You need a more thorough understanding of the most basic programming concepts, which you are not going to get in the Programming section of a forum. This might be a case for a "Programming For Dummies" type book, and if it sounds like an insult, I promise I'm not the one who came up with that title. That's the title someone who hadn't read "Choosing Good Book Titles For Dummies" decided would be a good title for a series of books on various subjects. But let's try.

its the two lines where first, I use "abs"
You are looking at a distance. Distances cannot be negative. If the difference in coordinates between two points is a negative number, then the distance between them is the absolute value. Basically, it saves you from having to figure out which order to do the subtraction in.

and second is where I well, find self using var.
This is the simple, very basic thing you don't understand about objects and instances. You don't need to find "self". self is you. self is exactly what you are when you are not something else. It would be like me telling you to go ask somebody where you are. You know where you are, because you're there. If you want, say, the x value of self... it's x. Just x. You don't need an instance id to look at your own variables. You just look at your own variables without dot notation. You don't need to use a with to put you in the scope of self, because you are always in the scope of self. I can't explain the line to you because it does nothing meaningful and is wrong, bad, and useless.
 

jermaine

Member
hm ok...ur right about me not understanding anything tho lol considered im literally 14 and have been coding for like what 3 months?
I think... I have got the gist of the abs thing...
the var function well. if I didn't do that line of code in my game however...
I don't remember wut it did b4 I put that line fo code but now all the enemies are moving at once even if im not near them...
NEW PROBLEM THO. I THINK I realised the problem.so I have seen this for a few days now and I suspected this was the problem to this but I don really know and I STILL don't know cuz I can't find it on the internet anywhere.

but basically in the line of "abs" code. that "if" function right before that is grey instead of orange.and so is another "while" function but that isn't the most important right now.so im thinking since its grey is that why its just...not doing that line of code?
and I also realised that those two enemies that move only move when they are basically sticking to each other like I said on the first comment I made.and they move only when I add the "var" self finding code.if it wasn't there, it would be the same and everyone would just move even when im not near.
 

woods

Member
maybe back it up a bit, and take a few baby steps to get your process down.. start with this in your enemy object... get your states changing working proper and then add to it.. one thing at a time.. framework and logic flow are important

see if you can get your enemy to change state when you get close enough.. dont worry about moving them until you nail this part down ;o)

something like..
Code:
//create event
state = "stationary";
Code:
//step event
switch(state)
{ 
case "stationary":

if (abs(x - player.x) < 64)
        {
            state = "chase";
        }
       break;

case "chase":
if (abs(x - player.x) >= 64)   
      {
           state = "stationary";
      }
      break;
}
Code:
// draw event

draw_self();
draw_text(x, y-32, "state:  " +string(state));
 

Nidoking

Member
and I also realised that those two enemies that move only move when they are basically sticking to each other like I said on the first comment I made.and they move only when I add the "var" self finding code.if it wasn't there, it would be the same and everyone would just move even when im not near.
It sounds to me like you're using a "with" inappropriately, or you're using an object name in dot notation. But more to the point, you're making the classic mistake that prevents poor programmers from becoming good programmers, which I call the "closer fallacy". You're thinking that, when you make a particular change, the behavior is closer to what you want than it was before you made the change, and that therefore, the change is beneficial and you're closer to the solution to your problem. Get this thinking out of your head, because it is your worst enemy. Pretend you're trying to get through a maze, and you're in a dead-end hallway. Moving closer to the exit of the maze directly is just running you right into a dead end, and if you keep going that way, you'll find an insurmountable wall between you and where you want to be. The only way to progress from that point is to take steps that appear to lead away from the goal, but will put you onto the right track. I'm not going to say that there's no such thing as "closer" and "farther" from the correct answer in programming, but it's not as simple to judge as you seem to think it is. You don't appear to understand "if" or "while", and you show no indication that you understand why you don't ever need to teach an instance what "self" is. I think you would really benefit from stepping away from the code for a while, writing out in simple, logical steps what you want to happen, and then working to translate that into code. Maybe make some really simple games based on the tutorials, like clown clickers or something, so you learn these very basic concepts that you don't understand. You won't be making the games you want to without that. Don't ever take success as a positive result unless you can describe WHY something you've written does what you want it to do - getting the right answer once will only help you solve future problems if you understand how you got there. If you're 14, you've got plenty of time to learn these things, but you have to actually learn them. No shortcuts.
 

jermaine

Member
see if you can get your enemy to change state when you get close enough.. dont worry about moving them until you nail this part down ;o)
ok ill try that.

If you're 14, you've got plenty of time to learn these things, but you have to actually learn them. No shortcuts.
im not gonna reply the whole msg but yea ok I get wut u mean thanks.
The variable is the same as the enum. GM doesn't throw an error for that?
im not sure where your talking about but yea I guess it doesn't?
Also, this just sets the state to states.chase every time.
ye I saw that and changed it.
 

jermaine

Member
ok so thru some random investigation of this thing. im pretty sure the problem is with the gravity I wrote or something related to the y movement.
so right now I figured out how to make the enemy move.but it has to be placed exactly on the ground in the room customiser for it to move.
if it is 1 pixel above the ground. when I go close to it, it jus disappears,i expermiented a bit and found that if it is exactly 5 pixels above the ground. then it won't dissappear, instead, it will flash to the ground and continue moving normally.im not sure if there is another specific number of pixels of the ground and it'll just disappear again but I assume 5 pixels off the ground or directly on the ground would work.so thats wut makes me assume that the y movement is the problem. and im still terrible at making character physics so could u guys help me see wuts the problem?I tried making the y movement myself./
GML:
vsp += grav;
        //vertical
        if (place_meeting(x,y+vsp,wall))
        {
            while(!place_meeting(x,y+sign(vsp),wall))
            {
            y += sign(vsp)
            }
            vsp = 0;
        }
    y += vsp;
in the create event, grab = 0.2
vsp = 0.
one extra question regarding "abs" function.
in my code does it affect the y axis in any way, or does it just detect x movement.
if it does affect. how?

EDIT: so I figured out something new and well I don think its the pixels height problem anymore but I still think my y movement needs some fixing if im not wrong.so since I don't know the real problem Ill jus say wut I have observed from several runs of the game.
wut I said earlier about 5 pixels still affects if im not wrong. but there seems to be a limit after 5 pixels too. but thats not the only thing.
so I observed that in my room I have two enemies, so right, if im moving horizontally or not moving at all, and kill the first enemy, when ig et close to the second it just disappears, and yes it is on the ground.but sometimes, if I move and jump and kill the enemy, the second one doesn't disappear, now what I jus said may not be the key cuz the thing where the enemy doesn't disappear?its kinda inconsistent so yea I don't really know wuts going on at this point.and when it disappears it kinda just dies cuz the draw thing earlier that shows wut state it is in, it also disappears with it.but that also only happens sometimes, sometimes when it disappears,i can see that it goes downward into the floor which leads back to the y movement problem. this is so complicated😩
 
Last edited:

Nocturne

Friendly Tyrant
Forum Staff
Admin
Moderator
this is so complicated😩
Yes it is! I'm making a platform game right now and my player movement code is 1000+ lines... 😅 This is my first platform game too, even though I've been using GM for 15 years nearly. This kind of game is actually one of the toughest to make, so if you're just starting out it's no surprise that you sometimes get frustrated. Just remember that making games should be FUN and if it's not, then there's no reason why you can't change projects! You can take a break and try to make a smaller, simpler game, which will teach you new things, and then come back to this later when you have more knowledge... 😁
 

woods

Member
one extra question regarding "abs" function.
in my code does it affect the y axis in any way, or does it just detect x movement.
if it does affect. how?
This function returns the absolute value of the input argument, so if it's a positive value then it will remain the same, but if it's negative it will be multiplied by -1 to make it positive.


from what ive seen so far you are only using the "abs" function once to check for distance from enemy to player.. using the X value only

from your original post where you use the "abs" function..
Code:
if (abs(x - player.x)<64)
        {
            states = states.chase
        }
i have a hunch that you might be looking for this function instead..
This function calculates the distance from the edge of the bounding box of the calling instance to the nearest edge of the nearest instance of the object specified.


ive been lurking here off and on since 2016 and the most important thing ive learned is that i havnt learned everything ;o)
the most that ive put out over all this time is a handful of birthday card / minigame type things and hundreds of half projects that ive learned how to make a certain function or game mechanic work.
i am nowhere near ready to even think about "my epic game" that i want to make yet.. i have a long ways to go before im ready for that. but every day, i learn something new..
and if i can apply it to a future project.. BONUS



btw..
if this shet was easy.. everyone would be doing it ;o)
 

TheouAegis

Member
im not sure where your talking about but yea I guess it doesn't?
What I was referring to is you have a variable states, but you also have an enumerator states. GM is not supposed to allow this, it's supposed to throw an error. The reason is because GM has no way of knowing when states.walking is referring to an enum and when it's referring to an instance variable walking inside the instance whose ID is referenced by states. So even if GM is not showing error messages, I would not be surprised if that is still causing issues. You should change either the variable or the enum.
 

jermaine

Member
btw..
if this shet was easy.. everyone would be doing it ;o)
ye ig ur right...

@TheouAegis I assume you mean that in the create event I should change it to state = states.stationary??
If thats what you mean then instead of not giving me and error...it gives me an error actually.
it says that at line 11 which says " case states.stationary: " it will say enemy.state variable not set before reading it.
but when I do states = states.stationary it doesn't give me a error but ye there are problems with the game I guess...
sorry for the late reply it was exam season
 

TheouAegis

Member
ye ig ur right...

@TheouAegis I assume you mean that in the create event I should change it to state = states.stationary??
If thats what you mean then instead of not giving me and error...it gives me an error actually.
it says that at line 11 which says " case states.stationary: " it will say enemy.state variable not set before reading it.
but when I do states = states.stationary it doesn't give me a error but ye there are problems with the game I guess...
sorry for the late reply it was exam season
I mean any place you have states = or switch states, change it to just state.

Did you never have states = states.stationary in the Create Event, which should now be state = states.stationary‽ That would mean you never declared your variables, which is what causes an unset variable error.
 
Top