SOLVED Problem witch Frogs

xNYARLx

Member
Hello. I make for my child cute frogs who follow them but sometimes i have this error:

ERROR in
action number 1
of Other Event: Animation End
for object obj_env_frog_walk_to_player:

Variable obj_env_frog_walk_to_player.xspeed(100005, -2147483648) not set before reading it.
at gml_Script_move (line 7) - if (place_meeting(x + xspeed, y, collision_object)) {
############################################################################################
gml_Script_move (line 7)
gml_Object_obj_env_frog_walk_to_player_Other_7 (line 8) - move(obj_solid);



There Code, you can use it when u want to your game its no problem:

FROG CODE

GML:
CREATE
/// @description Initialize Frog

//Set frog to be still
image_speed = 0;
image_index = 0;

sight = 70;
max_speed = 2;
acceleration = 1.5;
gravity_acceleration = 0.5;

enum frog2 {
    idle,
    jump
}

state = frog2.idle;

---------------------------------------------------------------------------------------------------------------
DESTROY
/// @description Destroy the frog
event_inherited();

---------------------------------------------------------------------------------------------------------------
STEP
/// @description Execute the state

switch (state) {
    #region Idle State
    case frog2.idle:
        if (instance_exists(obj_player)) {
            var dis = distance_to_object(obj_player);
            if (dis < sight and alarm[0] <= 0) {
                image_speed = 0.5;
            
                if (obj_player.x != x) {
                    image_xscale = sign(obj_player.x - x);
                }
            }
        }
    break;
    #endregion
    #region Jump State
    case frog2.jump:
        image_index = image_number - 1;
    
        if (!place_meeting(x, y + 1, obj_solid)) {
            yspeed += gravity_acceleration;
        }
        else {
            yspeed = 0;
        
            apply_friction(acceleration);
        
            if(xspeed == 0 and yspeed == 0) {
                state = frog2.idle;
                alarm[0] = 15;
                image_speed = 0;
                image_index = 0;
            }
        }
        if (xspeed != 0) {
            image_xscale = sign(xspeed);
        }
        move(obj_solid);
    break;
    #endregion
}


---------------------------------------------------------------------------------------------------------------
ALARM 0
/// @description Just needs to be here


---------------------------------------------------------------------------------------------------------------
ANIMATION END
/// @description Jump
if (state == frog2.idle) {
    if (instance_exists(obj_player)) {
        xspeed = sign(obj_player.x - x) * max_speed;
        yspeed = -abs(xspeed * 2);
    }

    move(obj_solid);
    state = frog2.jump;
}
 

TsukaYuriko

☄️
Forum Staff
Moderator
You are not declaring xspeed, so it doesn't exist by the time you want to use it. Declare it in the Create event.
 

xNYARLx

Member
In general, this frog works perfectly, it does everything it has to do, but sometimes I get this error, from the beginning of the previous post.
yspeed to?
 

TsukaYuriko

☄️
Forum Staff
Moderator
If you get the same error with two different variables which are used in the exact same way, I see no need to spell out what to do with the second one after you just resolved the issue with the first one... ;)
 

xNYARLx

Member
Just asked for posterity hehe. Ok I'm testing :)

CREATE
GML:
/// @description Initialize Frog

//Set frog to be still
image_speed = 0;
image_index = 0;

xspeed = 0;
yspeed = 0;
sight = 70;
max_speed = 2;
acceleration = 1.5;
gravity_acceleration = 0.5;

enum frog2 {
    idle,
    jump
}

state = frog2.idle;
 
Last edited:

xNYARLx

Member
So far it works fine, but as I wrote, the error only pops up sometimes. I will test longer. If something was wrong, I will write it here. Thank you for your help :)
 

TsukaYuriko

☄️
Forum Staff
Moderator
It only popped up sometimes because there is a path in your code where an assignment to the variable (= declaration, if it's the first time) happens before you read from it. This is why you should declare variables in the Create event - to make sure they are declared no matter what.
 
Top