SOLVED Struggling With Calling Scripts/Functions

O

Optimistic Ori

Guest
Hi, I'm new to working with scripts, as I've been mainly watching tutorials when doing something I don't know how to do, and I have come across a problem that is driving me mad.

All I have seen people say to do is to basically put all the code and wrap it in a function. I understand that part, not too difficult, just a little copy and paste.
But what I don't get is how to call it. Everytime I try to call the function like I've seen people do, I get this:

And I don't get why it does this! It feels like I'm doing everything right, and it's just giving me a middle finger.
The code I place in the function works when I just run it in the Step event, but not when I try to call the function while it's in a script.
 
Last edited by a moderator:

Zephni

Member
In my case I am using the 2.3 style function scripts but it seems the object create event is still happening before the script's function definition is available? Is that possible?
 

samspade

Member
Hi, I'm new to working with scripts, as I've been mainly watching tutorials when doing something I don't know how to do, and I have come across a problem that is driving me mad.

All I have seen people say to do is to basically put all the code and wrap it in a function. I understand that part, not too difficult, just a little copy and paste.
But what I don't get is how to call it. Everytime I try to call the function like I've seen people do, I get this:

And I don't get why it does this! It feels like I'm doing everything right, and it's just giving me a middle finger.
The code I place in the function works when I just run it in the Step event, but not when I try to call the function while it's in a script.
Can someone tell me what it is I'm doing wrong? If the code I'm using needs to be seen, I'll share it.
We would need to see how and where you are declaring your function. You should post the entire functions code and where it is located.

In my case I am using the 2.3 style function scripts but it seems the object create event is still happening before the script's function definition is available? Is that possible?
This should probably be its own thread as it is its own question and not responding to the OP, but the following up comment is still the same, you should post the function and where it is being put that's the best way to answer questions like these.
 

Mr Magnus

Viking King
Did you actually remember to wrap it in a function? As in literally do

GML:
///Awesomescript.gml

function AwesomeScript(){
        ///Your great code here
}
 
O

Optimistic Ori

Guest
Thank you for directing me there but I investigated that one before posting here and had no luck.

Did you actually remember to wrap it in a function? As in literally do

GML:
///Awesomescript.gml

function AwesomeScript(){
        ///Your great code here
}
Yes. I've wrapped it in a function exactly how you've shown.

GML:
function PlayerState(){
    var _move = key_right - key_left;

    hzSpeed = _move * moveSpeed; //Walking
    vtSpeed = vtSpeed + grav; //Gravity
    if key_jump and place_meeting(x, y + 1, objWall) {vtSpeed = -8} //Jump

    //Horizontal Collision====================
    if (place_meeting(x + hzSpeed, y, objWall))
    {
        while (!place_meeting( x + sign(hzSpeed), y, objWall))
        {
            x = x + sign(hzSpeed);  
        }
   
        hzSpeed = 0;
    }

    x = x + hzSpeed;

    //Vertical Collision======================
    if (place_meeting(x, y + vtSpeed, objWall))
    {
        while (!place_meeting(x, y + sign(vtSpeed), objWall))
        {
            y = y + sign(vtSpeed);  
        }
   
        vtSpeed = 0;
    }

    y = y + vtSpeed;


    //Making Attacks
    if (key_click)
    {
        instance_create_layer( x, y, "Walls", objAttack);
   
   
    }

    //Animation
    if (hzSpeed != 0) image_xscale = sign(hzSpeed);

}

And this here is how I have tried to call the script/function (however I should refer to it) in my object's step event.

GML:
/// @description Controls

//Buttons=================================
key_left = keyboard_check(ord("A")); //A key means left
key_right = keyboard_check(ord("D")); //D key means right
key_jump = keyboard_check_pressed(vk_space); //Spacebar is jump
key_click = mouse_check_button_pressed(mb_left); //Attack



PlayerState();

Let me know if I need to share anything else.
 

samspade

Member
A couple questions:
  • Where are you declaring the function (where is the code wrapped in the function actually located)?
  • What object is running that step event?
  • Since you are neither passing or returning arguments, the code in your function should work if you just copy and pasted it in place of the function call, have you verified that it does?
 

quattj

Member
Where have you declared the function? Is it inside the player object? If so, I don't think that will work. If it is in its own standalone script/function then it should work. I don't see anything inherently wrong with the code, so my best guess is the actual location where you've declared the function.
 
O

Optimistic Ori

Guest
A couple questions:
  • Where are you declaring the function (where is the code wrapped in the function actually located)?
  • What object is running that step event?
  • Since you are neither passing or returning arguments, the code in your function should work if you just copy and pasted it in place of the function call, have you verified that it does?
1. I don't really get that first question. I've got the code wrapped in a function inside a script, and I am attempting
to declare it inside my player object's step event.
2. Yes, the code works outside the script when I had originally had everything typed up. Everything you see written
inside the function was simply cut and pasted. I have since cut and pasted back into the step event, and it still works fine,
just not when I try to call the function from the script.

Where have you declared the function? Is it inside the player object? If so, I don't think that will work. If it is in its own standalone script/function then it should work. I don't see anything inherently wrong with the code, so my best guess is the actual location where you've declared the function.
I am indeed declaring the function inside the player object. If that's the problem, then that makes no sense to me. How am I supposed to declare the function if not in the player object?
 

samspade

Member
1. I don't really get that first question. I've got the code wrapped in a function inside a script, and I am attempting
to declare it inside my player object's step event.
I'll use the terms create and use instead. Creating (declaring) a function is where you put the function PlayerState() {//code} and using (calling) the function is where you put PlayerState();. Where you create/make/declare a function determines a lot about its use. If you create it in a script asset it has certain properties and if you create it inside an object it has different properties. This is why where you create it matters. Your answer to my question makes it sound like you have created it inside a script asset. But your answer to quattj's question makes it sound like you created it in the player object. Which one is correct? If you created it inside of a script asset, what is the name of the script asset (hopefully it is something different than the function itself). If you created it inside of the player object, which event did you create it in?
 
O

Optimistic Ori

Guest
I'll use the terms create and use instead. Creating (declaring) a function is where you put the function PlayerState() {//code} and using (calling) the function is where you put PlayerState();. Where you create/make/declare a function determines a lot about its use. If you create it in a script asset it has certain properties and if you create it inside an object it has different properties. This is why where you create it matters. Your answer to my question makes it sound like you have created it inside a script asset. But your answer to quattj's question makes it sound like you created it in the player object. Which one is correct? If you created it inside of a script asset, what is the name of the script asset (hopefully it is something different than the function itself). If you created it inside of the player object, which event did you create it in?
My bad, I didn't understand the terminology.
I declared the function in a script asset and I'm trying to call the function in my player object. Does the name of the script asset affect things?
Because they have the same name, the declared function and the script asset.
 

samspade

Member
My bad, I didn't understand the terminology.
I declared the function in a script asset and I'm trying to call the function in my player object. Does the name of the script asset affect things?
Because they have the same name, the declared function and the script asset.
Try changing the name of the script asset to something else (e.g. keep your script function, the actual function itself, named the same, but change the name of the script asset, the thing you create in the IDE to something else like player_script_functions). I think it is supposed to work for conversion purposes from 2.2 to 2.3 but it's bad practice otherwise. It's worth testing as otherwise it seems like you are doing things correctly.
 
O

Optimistic Ori

Guest
Try changing the name of the script asset to something else (e.g. keep your script function, the actual function itself, named the same, but change the name of the script asset, the thing you create in the IDE to something else like player_script_functions). I think it is supposed to work for conversion purposes from 2.2 to 2.3 but it's bad practice otherwise. It's worth testing as otherwise it seems like you are doing things correctly.
Good idea but it did not work. I left the script asset named the default "Script1" and named the function "pMovement."
Calling the "pMovement" in my object resulted in the same error message.
 

Mr Magnus

Viking King
Could you by chance copy and paste the entire script and step event here? As you describe it everything does sound correct.
 
O

Optimistic Ori

Guest
Could you by chance copy and paste the entire script and step event here? As you describe it everything does sound correct.
Sure thing.
GML:
// Script assets have changed for v2.3.0 see
// https://help.yoyogames.com/hc/en-us/articles/360005277377 for more information
function pMovement(){
    //Getting Movement and Establishing Gravity
var _move = key_right - key_left;

hsp = _move * moveSpeed;
vsp = vsp + grav;


//Horizontal Collision and Movement
if (place_meeting(x + hsp, y, objWall))
{
    while (!place_meeting(x + sign(hsp), y, objWall))
    {
        x = x + sign(hsp);   
    }
    hsp = 0;
}

x = x + hsp;

//Vertical Collision and Movement
if (place_meeting(x, y + vsp, objWall))
{
    while (!place_meeting(x, y+sign(vsp), objWall))
    {
        y = y + sign(vsp);
    }
    vsp = 0;
}

y = y + vsp;

GML:
/// @description Controls

//Buttons=================================
key_left = keyboard_check(ord("A")); //A key means left
key_right = keyboard_check(ord("D")); //D key means right
key_jump = keyboard_check_pressed(vk_space); //Spacebar is jump
key_click = mouse_check_button_pressed(mb_left); //Attack

//Getting Movement and Establishing Gravity
var _move = key_right - key_left;

hsp = _move * moveSpeed;
vsp = vsp + grav;


//Horizontal Collision and Movement
if (place_meeting(x + hsp, y, objWall))
{
    while (!place_meeting(x + sign(hsp), y, objWall))
    {
        x = x + sign(hsp);   
    }
    hsp = 0;
}

x = x + hsp;

//Vertical Collision and Movement
if (place_meeting(x, y + vsp, objWall))
{
    while (!place_meeting(x, y+sign(vsp), objWall))
    {
        y = y + sign(vsp);
    }
    vsp = 0;
}

y = y + vsp;

if (place_meeting( x, y + 1, objWall) && key_jump) vsp = -7;

GML:
/// @description Controls

//Buttons=================================
key_left = keyboard_check(ord("A")); //A key means left
key_right = keyboard_check(ord("D")); //D key means right
key_jump = keyboard_check_pressed(vk_space); //Spacebar is jump
key_click = mouse_check_button_pressed(mb_left); //Attack

pMovement();
 
O

Optimistic Ori

Guest
Did you forget to copy a final curly brace at the end of the script? Or is it perhaps missing from the script?
No, the required curly braces are there. It'd give me an error in the text editor if something was amiss.

If you guys could actually see what I am doing, would that help? Like maybe posting a video? Maybe one of y'as
can find out the step I'm missing.
 

chamaeleon

Member
I can't see what you are doing wrong. I can get the same exact error message if I misspell either the name of the function call or the name of the function, but if they match, the function is called as expected. I am trying to think of a way you could have some kind of textual change taking place, but I haven't been able to come up with a #macro definition that doesn't cause a compile error instead of this exact message. In any case, does PlayerState occur in another way in your code? Unless it's a brand new identifier you have just started using for this function, perhaps you have some legacy use that interferes (just can't think of how, on the top of my head).
 
O

Optimistic Ori

Guest
In any case, does PlayerState occur in another way in your code? Unless it's a brand new identifier you have just started using for this function, perhaps you have some legacy use that interferes (just can't think of how, on the top of my head).
Are you asking if I'm calling the function somewhere else besides the step event? If so, then no. This is the only place I am calling the function.
I felt like it might help to narrow it down, so I recorded myself migrating the code into a script function.
 

chamaeleon

Member
Are you asking if I'm calling the function somewhere else besides the step event? If so, then no. This is the only place I am calling the function.
I felt like it might help to narrow it down, so I recorded myself migrating the code into a script function.
No, I was rather asking if the symbol PlayerState has been used in some other way at all in the project, before you made the function and the call of it.

Edit: Watched the video. Nothing apparent wrong with the use of functions. If it was me, I'd create a blank project, add an object, add a script, add a function in the script, and call the function from the create event, and see if it works at all, as a baseline.
 
O

Optimistic Ori

Guest
No, I was rather asking if the symbol PlayerState has been used in some other way at all in the project, before you made the function and the call of it.
Gotcha. Another good thought, but no. Previously I did have PLAYERSTATE as the name of an enum, but I have since changed the name of the function to pMovement, so I do not think that is the error.

Edit: I have tried what you said and calling the function in the create event worked.
Edit 2: I have also tried calling the function in my original game in the create event, it resulted in the same error as before, only it said something went wrong in the CREATE event rather than the STEP event.
 
Last edited by a moderator:
O

Optimistic Ori

Guest
I have attempted using a smaller portion of the code and tried calling it in my step event and resulted in the same error. Is there something I have to declare in the "Create" event? Based on the error message, I'm thinking that GM thinks I'm trying to call a variable that hasn't been given a value.​
 

chamaeleon

Member
The only thing required is to write the function like you do in a script, whose name does not matter. Calling the function does not require any extra definitions or declarations. If you simply cannot get a trivial project to work this way, I would recommend uninstalling and reinstalling gms completely and see if that helps.
 
O

Optimistic Ori

Guest
The only thing required is to write the function like you do in a script, whose name does not matter. Calling the function does not require any extra definitions or declarations. If you simply cannot get a trivial project to work this way, I would recommend uninstalling and reinstalling gms completely and see if that helps.
No dice. However, I did notice something when trying to declare a function. When creating a Script asset, the function that gets loaded has the same name. But when I change the name of the function, I get the little "Caution" error symbol on the left that says "variable [name i chose] only referenced once." I don't get that error when I leave the function's name the same as the script. It doesn't work either way, but could that have something to do with it?
 

chamaeleon

Member
No dice. However, I did notice something when trying to declare a function. When creating a Script asset, the function that gets loaded has the same name. But when I change the name of the function, I get the little "Caution" error symbol on the left that says "variable [name i chose] only referenced once." I don't get that error when I leave the function's name the same as the script. It doesn't work either way, but could that have something to do with it?
Doubtful. It will have that warning marker to indicate that the function is not called anywhere. That is normal until you write code that calls it. You may need to file a proper support request with YYG.
 

Tyg

Member
Ya, you have to make sure the declared function name is the same as the script name
I noticed that the duplicate function is a little buggy and sometimes the duplicated script code shows in DnD although its not the actual code that is run
whenever i duplicate a function i exit and reload the program, its like i know thats not the code i wrote and it sometimes crosses into other previously made functions
it is just visual and doesnt contain that code, i was hoping that they would fix that in this last update but it still does it
it scares me that my functions were overwritten so i bail and reload and the code shows up fine
 
O

Optimistic Ori

Guest
It appears I may have found the solution. I manually copied all of the code I had used into another new project, and now I can call the function just fine, even with a different name than the title of the script. I am guessing the file I had a problem with was corrupted somehow.
Kind of an irritating way to discover things, but it works now so whatevs. Thanks for your time lads, y'all had some really good ideas.
 
Top