SOLVED keyRun, hSpeed, and vSpeed are mysteryously "undefined" in PlayerStateFree

D

DevilKap

Guest
The h and v speed are clearly defined in the player create event (seen below):
1602206652876.png
and keyRun is also clearly defined in the player step event (seen below):
1602206682684.png
However when I test my game, this happens:
1602206783522.png
When I re-define keyRun in PlayerStateFree, this happens:
1602206829269.png
1602206843792.png
 
D

DevilKap

Guest
Continued:
When I re-define hSpeed in PlayerStateFree, this happens (seen below):
1602207003900.png
1602207021876.png
So now, when I re-define vSpeed in PlayerStateFree, this happens:
1602207063582.png
The game actually launches! However, Sprinting, Dashing, etc. doesn't work anymore! please help
!
 

kburkhart84

Firehammer Games
I believe the issue has to do with script_execute(). Theoretically they left it in version 2.3 in working condition, but I don't think it works right. It seems to be executing the script, but possibly not the function as you have defined it.

1. Did you use the new syntax in 2.3 to define the function? If not, that may be the issue.

2. Is there any reason why you don't just call the function like below?

Code:
state = PlayerStateFree;
.....
//later in step
state();
// instead of script_execute(), just call it directly
Indeed, this is part of the whole point of the new functions/features in 2.3. script_execute really has pretty much no use case anymore since you can directly call functions now.
 

Gradius

Member
First error may be because you didn't initialise the keyItem variable in your create event.

For the second, the error being for 'object undefined' for a variable belonging to <unknown_object> looks to be key. It's not trying to access the instance variable of the object calling the script. So either the script is called elsewhere than the object, of you've got something like 'var hSpeed = 5' in the script, throwing GMS off. Testing myself, even nested script_execute causes no issues, so I can only think that the script is getting called from elsewhere or you've got a rogue 'var' turning it into a local variable instead of an instance variable.

Edit: Definitely hit the brush icon to clear your cache. Update GMS if you haven't done so lately too, then clear your cache again. It's possible that could be the result of weird undefined behaviour.
 
D

DevilKap

Guest
Solved, the problem was apparently:
GML:
//Activate key logic
if(keyActivate)
{
    //1. Check for an entity to activate
    //2. If there's nothing, or something without a script, do nothing
    //3. Otherwise, activate diologue
    //4. If the entity is an NPC, make it face towards the player
    
    var _activateX = lengthdir_x(10, direction);
    var _activateY = lengthdir_y(10, direction);

    activate = instance_position(x+_activateX, y+_activateY, pEntity);

    if (activate == noone || activate.entityActivateScript == -1)
    {
        state = PlayerStateFree;
    }
    else
    {
         ScriptExecuteArray(activate.entityActivateScript, activate.entityActivateArgs);
    }
    
    //NPC dir
    if (activate.entityNPC)
    {
        with (activate)
        {
            direction =point_direction(x,y,other.x,other.y);
        }
    }
Was done before the command for keyRun, somehow causing issues?
Could someone try to explain how putting this before the function for keyRun causes errors? It's clearly something to do with this in particular, because after I delete or move it around, it fixes only some of the issues.
 

TheouAegis

Member
Yeah, as has been said, you cannot write a script in GMS 2.3 the same way you would write a script in any prior version. The script must contain a function. if your script does not contain a function, game maker will look through the script at the start of the game and treat every single variable in that script as a global variable. It doesn't matter if the player object itself defines those same variables, game maker will treat the player's variables as instance variables and the variables references in the script as global variables. Since you define the instance variables and not the global variables, you get the error messages. If the script instead contains a function, then the function can use the instance variables. If you open the debugger and look at the variable listings while your game is running, you will see that the variables you mentioned in the scripts appear as either unassigned or global variables, in addition to the instance variables with the same names.
 
D

DevilKap

Guest
Yeah, as has been said, you cannot write a script in GMS 2.3 the same way you would write a script in any prior version. The script must contain a function. if your script does not contain a function, game maker will look through the script at the start of the game and treat every single variable in that script as a global variable. It doesn't matter if the player object itself defines those same variables, game maker will treat the player's variables as instance variables and the variables references in the script as global variables. Since you define the instance variables and not the global variables, you get the error messages. If the script instead contains a function, then the function can use the instance variables. If you open the debugger and look at the variable listings while your game is running, you will see that the variables you mentioned in the scripts appear as either unassigned or global variables, in addition to the instance variables with the same names.
English, please?
 

FrostyCat

Redemption Seeker
It simply means that in GMS 2.3+, you need to wrap the body of the function using the new function syntax.
GML:
function PlayerStateFree() {
    ...
}
Not doing this in GMS 2.3+ would make the code inside run at the beginning of the game.

See: GMS 2.3: New GML Features (Functions, Scripts, and Method Variables)
 
Top