• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Execute Script With Objects Local Variables

For ease of code is there a command to make scripts execute in a way they read the executing objects variables as their own local?

I noticed when I swapped code from an object to a script I got this this because it's apparently trying to read locals for an undefined object instead of the object that called the script

2021-02-17 (2).png
 

FrostyCat

Redemption Seeker

kburkhart84

Firehammer Games
Functions as called on object scope already read variables you have created on objects. They also can add variables.

Code:
function create()
{
    myVariable = 0;
}

function step()
{
    //do something with myVariable
}
If an object has a create event that calls this create function, it will add an instance variable to that instance of the object(which ends up being all instances since they each will run the same code). If you put the step function there into the step event, it will have no problem accessing that same myVariable variable to do something.

You may have something confused honestly. Local variables are the ones that you create with the var keyword. They only exist in the code block they are created in. If you put one in a create event, it won't be usable in the step event either. Instance variables are the ones you create without that var keyword. They last through the lifetime of the instance, and can be accessed by any event of the object after it is created. The best practice is to init such variables in the create event.

FrostyCat also has a valid point. Many tutorials haven't been updated(though the manual shows it correctly). This error would indeed be caused by that.
 
Functions as called on object scope already read variables you have created on objects. They also can add variables.

Code:
function create()
{
    myVariable = 0;
}

function step()
{
    //do something with myVariable
}
If an object has a create event that calls this create function, it will add an instance variable to that instance of the object(which ends up being all instances since they each will run the same code). If you put the step function there into the step event, it will have no problem accessing that same myVariable variable to do something.

You may have something confused honestly. Local variables are the ones that you create with the var keyword. They only exist in the code block they are created in. If you put one in a create event, it won't be usable in the step event either. Instance variables are the ones you create without that var keyword. They last through the lifetime of the instance, and can be accessed by any event of the object after it is created. The best practice is to init such variables in the create event.

FrostyCat also has a valid point. Many tutorials haven't been updated(though the manual shows it correctly). This error would indeed be caused by that.

I am, I confused instance variables with locals.

I'm unfamiliar with those functions you're using and can't find them in the manual so I'm not 100% how to use them, nor is the manual Frost linked doing me much good at my current knowadge level with scripts.
 

kburkhart84

Firehammer Games
Those are functions that I made, just as an example. They don't exist unless you make them.

You didn't show us any code yet. If you show the actual code, I can show you where what Frosty's link applies to, I would need to see where you create the function, and where you call it.
 

kburkhart84

Firehammer Games
Since the script only does one thing I just used script_execute(movement_scp) instead of defining a function and calling it
1. And that is your problem. You need to learn the new syntax. Frosty's link has more info, as does the manual.

2. For future reference, stop posting pictures of your code, and instead use code tags and copy/paste the code. It is much easier for us to read, and it allows us to make quick changes to show you.

3. You need to convert your function to use the new function syntax. This means adding function movement_scp() to the top, and then surrounding the whole thing in brackets. This actually happens automatically when you create a new script resource, but you seem to have deleted that part without knowing what you were doing.

4. In the step event where you want to call the function, you simply call it like any other function, instead of using script_execute(). So you would simply put movement_scp() there instead.
 
1. And that is your problem. You need to learn the new syntax. Frosty's link has more info, as does the manual.

2. For future reference, stop posting pictures of your code, and instead use code tags and copy/paste the code. It is much easier for us to read, and it allows us to make quick changes to show you.

3. You need to convert your function to use the new function syntax. This means adding function movement_scp() to the top, and then surrounding the whole thing in brackets. This actually happens automatically when you create a new script resource, but you seem to have deleted that part without knowing what you were doing.

4. In the step event where you want to call the function, you simply call it like any other function, instead of using script_execute(). So you would simply put movement_scp() there instead.
That makes sense, Execute_script() still being included as a working function made me think the old way would work as long as there was only one "function" in the script. I'll have to define the script as a function with the proper call and brackets and switch to a function call. Thanks for clearing all that up for me.

Sorry about the images, I just assumed it would be quicker for a small thing. I'll copy the text next time.


Edit: defined the function and called it. Now it's all working. Thanks again.
 

kburkhart84

Firehammer Games
The execute_script() thing was kept around only since it has been pretty much replaced by just making functions and calling them directly. The new way pretty much replaces the use of execute_script(). The one place I think it was that you still need it, is if you are not making methods out of global functions but still want to call them from separate variables....but that is a very sparse use case that I'm sure doesn't apply to what you are doing.
 
Top