How to access variables from another script WITHIN an object?

G

gacl

Guest
I have an object that uses ammo, obviously I cannot define the variable in the same step script as the bullet firing etc, as the ammo would be reset every time it looped. So, within an object how can I access a script in the create event from a script in the step event?

Something like:

object.create_script.variable += 3;

Stuff like that.

How do I access the variable though? I've already tried out just typing out the script...
 

FredFredrickson

Member
GMC Elder
Maybe I don't understand what you're trying to do, but why don't you just define the variable in the object's Create event and use the scripts to manipulate it?
 

RangerX

Member
In the create event of the object you declare the variable:

name = value;

In the scripts, you manipulate that variable like any other. If however you call that script in an object that doesn't have this variable, it will crash.
 

chorrus

Member
In the scripts, you manipulate that variable like any other. If however you call that script in an object that doesn't have this variable, it will crash.
But he can also modify that variable even if he called it from another object, use something like this.

with(obj_abc){//The object with the variable you want to access
var -= 1//The avriable and whatever you want to do with it
}
 
A

anomalous

Guest
obj_player = player instance that has ammo variable
ammo_count = 5; // in obj_player create event

From any other object you can access ammo_count using :
obj_player.ammo_count

Or if you want to work "inside" of that object use :
with obj_player
{
// do stuff here as though you were in obj_player, have access to all obj_player variables, etc.
}

If using with obj_player, and you want to access a variable back in the original instance you are working in, use other.<variable>
 
G

gacl

Guest
But he can also modify that variable even if he called it from another object, use something like this.

with(obj_abc){//The object with the variable you want to access
var -= 1//The avriable and whatever you want to do with it
}
Thank you, how can I compare variables like this?

E.G.

if (with obj_player {variable > 5})
{
do stuff
}

would this work, how would I go about doing so if not?
 
G

gacl

Guest
[
obj_player = player instance that has ammo variable
ammo_count = 5; // in obj_player create event

From any other object you can access ammo_count using :
obj_player.ammo_count

Or if you want to work "inside" of that object use :
with obj_player
{
// do stuff here as though you were in obj_player, have access to all obj_player variables, etc.
}

If using with obj_player, and you want to access a variable back in the original instance you are working in, use other.<variable>
I want to access the variable from within the same object, just from a different script within the object. I tried doing obj_player.variable however I got the same runner error of variable is not defined / does not exist. Am I doing something wrong?
 

FredFredrickson

Member
GMC Elder
Any variables you declare in the Create event are always with the object after that.

So in Create, you say:
Code:
ammo_count = 10;
Then, in the Draw event, you could do:
Code:
draw_text(10,10,string(ammo_count));
...which will draw the value of ammo_count in the player's draw event.

Is this along the lines of what you're trying to do?
 
G

gacl

Guest
Any variables you declare in the Create event are always with the object after that.

So in Create, you say:
Code:
ammo_count = 10;
Then, in the Draw event, you could do:
Code:
draw_text(10,10,string(ammo_count));
...which will draw the value of ammo_count in the player's draw event.

Is this along the lines of what you're trying to do?
Holy **** thanks man, the problem was in the create event I declared variables prefixed with the var keyword:

var beans = 5; //example

however if I remove the var it works. Thanks!
 

chorrus

Member
Thank you, how can I compare variables like this?

E.G.

if (with obj_player {variable > 5})
{
do stuff
}

would this work, how would I go about doing so if not?
That wouldn't work, it would be this:
with(obj_player){
if(variable>5){
//do stuff
}
}

Everything inside with(obj_player) works like if you had it in obj_player
 

FredFredrickson

Member
GMC Elder
Holy **** thanks man, the problem was in the create event I declared variables prefixed with the var keyword:

var beans = 5; //example

however if I remove the var it works. Thanks!
You're welcome.

You only use var when you want the variable to be temporary.

Don't forget that you can look up this sort of thing in the GM manual (click the little circle with the ? inside) if you're confused about what a function does.
 
Top