• 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!

Legacy GM [Solved] Setting a global variable?

X

XirmiX

Guest
Strangely, when I set up a global variable in one object's create event and then make an if statement in create event of another object checking whether that variable is true/false or some number, it gives me an error saying "the variable isn't set"... What the heck? Can I not check for global variables in create events?
 
L

Lars Karlsson

Guest
Two things to make sure of:

1. When you want to get your global, let's say: global.health. You have to get it by global.health not by health.
2. Make sure that the object declaring the global is being instantiated before the one calling it.
 
If you are putting the objects down in the room editor, then make sure the object that defines global.health comes first in the room's instance order.

Go to the room settings and click on the "Instance Order" button. There's also a button on the top of the room editor window, but the icon is hard to describe. ( three rectangles with a red check box next to one of them).
 

jazzzar

Member
calling a global variable in another object that the one it was declared with requires the object calling that variable to be created after the one declaring it (i made it too complicated)
simple steps :
the object that declares the global variable is the first one in the room (or before the one using it)
than the object using it
 

TheouAegis

Member
You should consider making a blank, empty room and putting it at the top of your rooms list so it's the first room the game goes to. Click on SETTING in the Room tab and click Creation Code. Declare all your global variables there, then put room_goto_next() to continue on with the rest of the game.

An additional benefit of doing it this way is when you start doing your own game restart functions, you can "restart" in the global variable room OR in the main menu, depending on how your game is set up. So some things you may want to only create/declare once won't get created/declared over and over.
 
X

XirmiX

Guest
Okay, I solved the issue with the global variable not being declared, but wtf is this 💩💩💩💩:
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Create Event
for object obj_garage_functions:

Unable to find any instance for object index '2'
at gml_Object_obj_garage_functions_CreateEvent_1 (line 18) - obj_ricochet.sprite_index = spr_ricochet_M3;
############################################################################################
The number 2?! Why does the program want the number 2?! Does it seriously need to take a dump that badly?

But seriously though, why does it ask for number 2?

The Create Event code for the object this error is happening in is as follows:
Code:
///Garage settings
//Default starting settings
global.hull_equipped = viking;
global.turret_equipped = ricochet;
global.paint_equipped = green;
global.upgrade_equipped = M3;


//Paint green
if global.paint_equipped == green
{
    if global.hull_equipped == viking
    {
        obj_hull.sprite_index = spr_viking_M3;
    }
    if global.turret_equipped == ricochet
    {
        obj_ricochet.sprite_index = spr_ricochet_M3;
    }
}
 
it's saying no instance of object index 2 (which is the third object down in your resource tree (remember they start at zero)). Given the context, object index 2 must be "obj_ricochet".

OR, obj_ricochet is a variable which containts the real number 2, and because the variable is being used as if it were an object index, no instance of that object index exists.
 
C

ConsolCWBY

Guest
Look at this
If you notice, it returns a real number. That is why it says: Unable to find any instance for object index '2'
Does obj_ricochet exist when this code runs? If not, that's why it would throw this error.
 
X

XirmiX

Guest
Look at this
If you notice, it returns a real number. That is why it says: Unable to find any instance for object index '2'
Does obj_ricochet exist when this code runs? If not, that's why it would throw this error.
Okay, but why does the object have to have been in the room already for this? Why does it require me to have it in the room for changing the sprite_index? This doesn't make any sense! And I can't have the object be already created beforehand, because it is created only afterwards by a code within obj_hull and obj_garage_functions throws in the global variables to which obj_ricochet and obj_hull respond to, to which the obj_garage_functions itself then applies sprite_indexes to... Do I seriously need another whole separate object for the variables or the if statements I've currently put in one object that is obj_garage_functions?
 
There are a few properties of "objects" (as opposed to instances) that you can change while the game is running...

sprite_index happens to be one of them, but you need to use this function:

object_set_sprite( index, spr );

http://docs.yoyogames.com/source/dadiospice/002_reference/objects and instances/objects/object_set_sprite.html

Look at this page for a list of the other properites that can be changed on an object level:

http://docs.yoyogames.com/source/dadiospice/002_reference/objects and instances/objects/index.html

Apparently, if you change an object's properites, any instances of that object that already exist in the room may or may not be changed in the same way. The manual suggests that you shouldn't modify object properites if any instance of that object already exists.
 
X

XirmiX

Guest
There are a few properties of "objects" (as opposed to instances) that you can change while the game is running...

sprite_index happens to be one of them, but you need to use this function:

object_set_sprite( index, spr );

http://docs.yoyogames.com/source/dadiospice/002_reference/objects and instances/objects/object_set_sprite.html

Look at this page for a list of the other properites that can be changed on an object level:

http://docs.yoyogames.com/source/dadiospice/002_reference/objects and instances/objects/index.html

Apparently, if you change an object's properites, any instances of that object that already exist in the room may or may not be changed in the same way. The manual suggests that you shouldn't modify object properites if any instance of that object already exists.
Okay... Not sure if this will help with my error in any way, but good to know.
 
My understanding was that you wanted to change the sprite_index of a whole object.

But maybe you are actually just trying to change the sprite_index of a newly (or soon to be) created instance?
 
X

XirmiX

Guest
My understanding was that you wanted to change the sprite_index of a whole object.

But maybe you are actually just trying to change the sprite_index of a newly (or soon to be) created instance?
Yeah, soon to be created. But I changed the code so that the instance gets created without the code help of obj_hull, so at this point it's whatever, things working out well now, so thanks for any help that anyone provided. Though, it would be great if you could tell me how to change sprite_index for an object/instance that's soon to be created if you can, in case I come back to that at any point or anyone else finds this and they need some help :p
 
Code:
var _sprite = spr_ricochet_M3;
with instance_create(0,0,obj_ricochet)
{
    sprite_index = _sprite;
}
Or:

Code:
var _sprite = spr_ricochet_M3;
var _ricochet = instance_create(0,0,obj_ricochet);
_ricochet.sprite_index = _sprite;
Or, if the newly created instance needs to know the value of _sprite during its create event ,(for example, because it needs to make calculations based on the size of the sprite), you can assign the sprite index to a global variable prior to creating the instance, and then reference that variable from inside of the create event.

I'm assuming that the value of "_sprite" can be variable, otherwise you might as well assign "spr_ricochet_M3" directly to "sprite_index", without using an intermediate variable.
 
Top