Legacy GM How do I put multiple actions on one object?

River

Member
Hi guys, currently I am using GMS 1.4, I'm still working on a survival project right now. I am performing a monkey with the following actions: if it is near the banana it will run to get the banana to eat, if it is close to the main character it will follow the main character, and if it is from 7pm (19h) to 5am It will sleep and then wake up. The code I am currently doing is basically like below, but is it not working while sleeping? which means from 7pm to 5am she still can't sleep, I don't understand what's wrong, hope someone can help me. Thank you.
Create events:
GML:
Sleeping=0; state = STATE_IDLE; Eating=0;
Step Events:

Code:
if instance_exists(oBanana) and Sleeping =0 /// monkey will follow the banana
{
    if distance_to_object(oBanana) <=150
        {
            state = STATE_FOLLOWEAT;
            Eating=1;
        }else
        {
            state = STATE_IDLE;
            Eating=0;
        }
}else if (instance_exists(objPlayerCharacter)) and Eating=0 and Sleeping=0 /// monkey will follow the main character
{
     if (point_distance(x, y, objPlayerCharacter.x, objPlayerCharacter.y) <= 150)
           {
               state = STATE_FOLLOWPLAYER;
         }else if (state == STATE_FOLLOWPLAYER)
         {
                state = STATE_IDLE;
         }
}else if Sleeping=0
{
    if ( oMapController.hour >= 19 && oMapController.hour <= 23) ) || ( oMapController.hour >= 0 && oMapController.hour <= 5) ) /// monkey will sleep
                {
                    state = STATE_SLEEP;
                    Sleeping=1;          
               }else
               {
                      state = STATE_IDLE;
                      Sleeping=0;
               }

}
 
Last edited:

Coded Games

Member
All your conditions have Sleeping=0. So if sleeping is 1 all the conditions will be false and nothing will happen. Also, to make your code infinitely easier to understand please use true/false for Boolean values instead of 1/0. No need to be a leet hacker person using straight binary.

Edit: There is actually a lot more wrong here. I don’t think this is valid syntax: 19 <= oMapController.hour <=23
it would have to be
if (oMapController.hour >= 19 && oMapController.hour <= 23)

This line is missing a bracket: if (point_distance(x, y, objPlayerCharacter.x, objPlayerCharacter.y) <= 150

There shouldn’t be a space here: instance_exists (oBanana)
 
Last edited:
You're organizing your actions by priority, wich is the right thing to do, but when dealing with such a complex behaviour structure, don't try to implement all at once. It's good practice to focus on a single action, see if it's working properly, then move to the next one.

I think you're having problems with the sleeping state because you're trying to check 2 conditions inside 1. Instead of
GML:
if (19 <= oMapController.hour <= 23) || //......
You should check as
GML:
if (oMapcontroller.hour >= 19 && oMapController.hour <= 23) || //.....
I never seen conditions structured like the way you did, so I really don't even know if they should work.
 

Nidoking

Member
You need to think through your logic from top to bottom.

Code:
Code:
if instance_exists (oBanana) and Sleeping =0 /// monkey will follow the banana

}else if (instance_exists(objPlayerCharacter)) and Eating=0 and Sleeping=0 /// monkey will follow the main character

}else if Sleeping=0

}
So, if there is a banana, the monkey will always go to the banana, regardless of the time. If not, if there's a player instance (and surely this will always be the case), the monkey will follow the player. So it looks like the monkey can only sleep if there is no banana, and it's eating (what it would be eating if there's no banana is anyone's guess), AND there's no player.

Also, the conditions are incorrect as stated above.
 

River

Member
All your conditions have Sleeping=0. So if sleeping is 1 all the conditions will be false and nothing will happen. Also, to make your code infinitely easier to understand please use true/false for Boolean values instead of 1/0. No need to be a leet hacker person using straight binary.

Edit: There is actually a lot more wrong here. I don’t think this is valid syntax: 19 <= oMapController.hour <=23
it would have to be
if (oMapController.hour >= 19 && oMapController.hour <= 23)

This line is missing a bracket: if (point_distance(x, y, objPlayerCharacter.x, objPlayerCharacter.y) <= 150

There shouldn’t be a space here: instance_exists (oBanana)
Sorry for posting the wrong code, I corrected everything as you said but it still doesn't work as I expected. This means it works properly with actions near bananas and near the main character but it only malfunctions when sleeping. That is from 7pm to 5am, the monkey doesn't sleep and the Sleeping is always zero but not 1. I don't understand what's wrong with it not sleeping at 19pm?
 

River

Member
You need to think through your logic from top to bottom.



So, if there is a banana, the monkey will always go to the banana, regardless of the time. If not, if there's a player instance (and surely this will always be the case), the monkey will follow the player. So it looks like the monkey can only sleep if there is no banana, and it's eating (what it would be eating if there's no banana is anyone's guess), AND there's no player.

Also, the conditions are incorrect as stated above.
I know the problem is that the monkey's sleep-time activity because the monkey works properly when it's near the banana and near the character itself is chasing the right target but it's only wrong from 7pm to 5am. then it still does not sleep and now Sleeping is always 0 but not equal to 1? I consider this code structure is very reasonable whether with banana or no monkey banana still function properly just wrong in terms of sleep activity.
 
I know the problem is that the monkey's sleep-time activity because the monkey works properly when it's near the banana and near the character itself is chasing the right target but it's only wrong from 7pm to 5am. then it still does not sleep and now Sleeping is always 0 but not equal to 1? I consider this code structure is very reasonable whether with banana or no monkey banana still function properly just wrong in terms of sleep activity.
As Nidoking said, you need to check your logic from highest to lower priority. The way you did it, the conditions alltogether make it impossible for the monkey to change it's state.
Consider that after each "else" statement, you're assuming that the previous "if" statement returned false.
 

River

Member
You need to think through your logic from top to bottom.



So, if there is a banana, the monkey will always go to the banana, regardless of the time. If not, if there's a player instance (and surely this will always be the case), the monkey will follow the player. So it looks like the monkey can only sleep if there is no banana, and it's eating (what it would be eating if there's no banana is anyone's guess), AND there's no player.

Also, the conditions are incorrect as stated above.
it is not correct, that's why I have set it: if instance_exists (oBanana) and Sleeping = 0 because if the monkey sleeps, even if there exists a banana it still has to sleep because at the time of sleep (from 7pm to 5pm I have set the condition Sleeping = 1).
 

River

Member
As Nidoking said, you need to check your logic from highest to lower priority. The way you did it, the conditions alltogether make it impossible for the monkey to change it's state.
Consider that after each "else" statement, you're assuming that the previous "if" statement returned false.
Thank you. Yes, I'm still thinking about it and I still don't understand what I should do, I know my logic is wrong but the problem is I don't know where it goes wrong and how I have to fix it. Sorry, because I'm bad at coding and thinking badly about logic.
 
Try to take it easy, you'll get there, believe me. You at least know some code and functions, I started using only Drag and Drop 🤣
Take it step by step... try adding a "/*" after the first condition to set all others as a comment and to be ignored from the compile.
Check if it's working, then move to the next... if the next isn't working, thats the one you should try to fix

EDIT: Or you should reconsider your priority order. I would put the sleep state at the beginning, since it's the one that requires the most conditions and shouldn't be interrupted by any other states.
 

Nidoking

Member
it is not correct, that's why I have set it: if instance_exists (oBanana) and Sleeping = 0 because if the monkey sleeps, even if there exists a banana it still has to sleep because at the time of sleep (from 7pm to 5pm I have set the condition Sleeping = 1).
But Sleeping is not equal to zero, because you're never getting to the part that would set it to one. So that condition has no effect. If there is a banana and the monkey is not asleep, then the monkey is going to the banana regardless of what time it is. You are not checking the time. You are only checking if there is a banana. If you want the monkey to sleep during certain times, you need to ensure that that check happens regardless of what else is happening.
 
Top