Jump to content


Photo

How do I test if a variable exists?


  • Please log in to reply
20 replies to this topic

#1 Race Pro Kid

Race Pro Kid

    GMC Member

  • GMC Member
  • 23 posts
  • Version:GM8

Posted 02 May 2012 - 04:42 PM

I can't figure out how to let my game figure out if a variable exists, to minimize/erase crashes and bugs. If you help, please, no code. Just events.

Edited by Race Pro Kid, 02 May 2012 - 04:43 PM.

  • 0

#2 Jobo

Jobo

    No neurons left today

  • GMC Member
  • 2405 posts
  • Version:GM:Studio

Posted 02 May 2012 - 05:27 PM

variable_local_exists("var");
variable_global_exists("var");

This is bad for performance though. If you have any unused variables, you should detect them and get rid of them.

Edited by Jobo, 02 May 2012 - 05:27 PM.

  • 0

#3 mads2194

mads2194

    GMC Member

  • GMC Member
  • 368 posts
  • Version:Unknown

Posted 02 May 2012 - 06:38 PM

You should not use these kind of functions if you want to improve yourself. Initialize all variables at the beginning and you should be good to go. What are you trying to accomplish?
  • 0

#4 Race Pro Kid

Race Pro Kid

    GMC Member

  • GMC Member
  • 23 posts
  • Version:GM8

Posted 02 May 2012 - 06:46 PM

variable_local_exists("var");
variable_global_exists("var");

This is bad for performance though. If you have any unused variables, you should detect them and get rid of them.

Does this work with the "test variable" event, though? Or is it just code?

Edited by Race Pro Kid, 02 May 2012 - 06:46 PM.

  • 0

#5 Jobo

Jobo

    No neurons left today

  • GMC Member
  • 2405 posts
  • Version:GM:Studio

Posted 02 May 2012 - 07:20 PM

It's code. To be used in "Execute Code".
  • 0

#6 Race Pro Kid

Race Pro Kid

    GMC Member

  • GMC Member
  • 23 posts
  • Version:GM8

Posted 02 May 2012 - 07:40 PM

It's code. To be used in "Execute Code".

Heh heh... I'm not good with code... ehm, what do I do afterwards?
  • 0

#7 BattleRifle BR55

BattleRifle BR55

    Moo

  • GMC Member
  • 8247 posts
  • Version:GM7

Posted 02 May 2012 - 08:21 PM

I think you should take mads2194's advice - if you're doing this just because you don't want to keep track of what variables you're creating, then how are you supposed to keep track of anything you're doing? It will only lead to more problems down the line.
  • 0

#8 Race Pro Kid

Race Pro Kid

    GMC Member

  • GMC Member
  • 23 posts
  • Version:GM8

Posted 02 May 2012 - 08:24 PM

I think you should take mads2194's advice - if you're doing this just because you don't want to keep track of what variables you're creating, then how are you supposed to keep track of anything you're doing? It will only lead to more problems down the line.

In case a variable does not exist (is not set in the options or somewhere else), it will set a default value for the variable.
  • 0

#9 Jobo

Jobo

    No neurons left today

  • GMC Member
  • 2405 posts
  • Version:GM:Studio

Posted 02 May 2012 - 08:34 PM

In case a variable does not exist (is not set in the options or somewhere else), it will set a default value for the variable.

Never do this. This means your game will store unused memory, thus taking up more RAM when playing, and requires more performance.

Untick that option this instance, and get rid of all unused variables manually.
  • 0

#10 BattleRifle BR55

BattleRifle BR55

    Moo

  • GMC Member
  • 8247 posts
  • Version:GM7

Posted 02 May 2012 - 10:19 PM


I think you should take mads2194's advice - if you're doing this just because you don't want to keep track of what variables you're creating, then how are you supposed to keep track of anything you're doing? It will only lead to more problems down the line.

In case a variable does not exist (is not set in the options or somewhere else), it will set a default value for the variable.

That's a very poor fail safe. You should be able to know your engine and what it's doing, not just create a bunch of systems and leave them to their own demise. If you want to set it up so that variables receive a default value if they don't exist, why not cut out all that stuff and leave the bare basics in the Create event? Give the variables their default values there, and you have the obvious benefit of them existing from the get-go without any potential for missing variable errors.
  • 0

#11 Race Pro Kid

Race Pro Kid

    GMC Member

  • GMC Member
  • 23 posts
  • Version:GM8

Posted 02 May 2012 - 11:09 PM

In case a variable does not exist (is not set in the options or somewhere else), it will set a default value for the variable.

Never do this. This means your game will store unused memory, thus taking up more RAM when playing, and requires more performance.

Untick that option this instance, and get rid of all unused variables manually.

I don't want to get rid of unused variables, as I do not have any. Variables don't get set when the game starts and therefore do not exist when the game starts. I can't figure out how to make a default value for certain variables that I need. I can't put an invisible object in the menu that, on created, sets a variable because the menu is commonly accessed and the variable will get reset every time the menu is accessed. Same with the player.

If you can't understand that, I will just tell you that I am making a gun selector with variables as the guns; the variable being called "gunToShoot" and the values being 0 for a pistol, 1 for a shotgun, 2 for an SMG, and 3 for a chaingun. When space is activated in the game when the player is present, it will check for what the "gunToShoot" variable is. If it is 0, it will spawn a bullet, set an alarm to 10 steps (or 1/3 of a second) and tell when it can shoot again (the "can_Shoot" trick), with the same way for the other three guns.

EDIT: Okay, I think I figured out why everyone was misunderstanding. So it goes like this; when I want to start a new game, it resets all variables. until a variable is set, it doesn't exist. I need to select a gun in the options screen when I start a new game, or else the global variable the shooting depends on, "gunToShoot", will not exist. I need to, when the player is created, have a checker that can check if a variable exists. If it does not, it creates it with the default value. If it does, it leaves the existing value alone.

Edited by Race Pro Kid, 02 May 2012 - 11:16 PM.

  • 0

#12 Race Pro Kid

Race Pro Kid

    GMC Member

  • GMC Member
  • 23 posts
  • Version:GM8

Posted 03 May 2012 - 03:43 AM

Can I just get a straight answer? Please?

EDIT: So far (by myself) I figured out to make the executed code:

if variable_global_exists(gunToShoot) == false {
    variable_global_set(gunToShoot, 0)
}
But it gives the error that "gunToShoot" doesn't exist when I run the game. Isn't that the point of the code? Can someone help?

Edited by Race Pro Kid, 03 May 2012 - 03:59 AM.

  • 0

#13 creators124

creators124

    awesomeliciousmember

  • GMC Member
  • 866 posts
  • Version:GM8

Posted 03 May 2012 - 04:03 AM

why can't you create it at the beginning?
or do want the character to check if the variable is set to default value?
if you say yes to the second question:
-code:
if (!global.canshoot = -1 or not)
{
	global.canshoot = -1;
};

just seeing the canshoot is a global.
also my default is -1.

Edited by creators124, 03 May 2012 - 05:07 AM.

  • 0

#14 MonopolyKing

MonopolyKing

    GMC Member

  • New Member
  • 938 posts

Posted 03 May 2012 - 04:44 AM

Can I just get a straight answer? Please?

EDIT: So far (by myself) I figured out to make the executed code:

if variable_global_exists(gunToShoot) == false {
    variable_global_set(gunToShoot, 0)
}
But it gives the error that "gunToShoot" doesn't exist when I run the game. Isn't that the point of the code? Can someone help?



Put 'gunToShoot' in quotation marks ("") --- this should work:

if (!variable_global_exists("gunToShoot"))
{
   variable_global_set("gunToShoot",0);
}

/*---------------------------------------------------------------------------
This was the code I used to test whether or not the method worked, and it did:
------------------------------------------------------------------------------
if (!variable_global_exists("text"))
{
variable_global_set("text","Is this working");
}

draw_text(40,40,global.text);
*/

You can thereafter reference the variable by using 'global.gunToShoot'. Though, as everyone else stated, you should be cautious with using this method.
  • 0

#15 TheouAegis

TheouAegis

    GMC Member

  • GMC Member
  • 4725 posts
  • Version:GM8

Posted 03 May 2012 - 05:00 AM

Ok, gonna hijack this but stay on topic.

So in my game I'm keeping it open source and I also am lazy when placing instances in the room. So the thing is, some instances have unique variables set in the Creation Code for each instance. Each of these variables has a default value, but not all instances will use those default values obviously. So currently I have

if !variable_local_exists(drop) drop=obj_Heart;

Of course "drop" is a variable that needs to be set in the Instance Creation Code. I can't just put drop=-1 in the Create Event because then it will override the Instance Creation Code if I set drop there.

So what would be the proper way to handle that, then? Other than my original method before I learned about Instance Creation Code, which was to create a library script and have each value referenced in that script (I do this with stats for enemies, having the parent object reference various scripts to retrieve the various stats such as HP, damage, move speed, image speed, and such). That method was too tedious for anything beyond "universal" values, so I need something for defaulting Instance Creation Code variables.

Edited by TheouAegis, 03 May 2012 - 05:00 AM.

  • 0

#16 MonopolyKing

MonopolyKing

    GMC Member

  • New Member
  • 938 posts

Posted 03 May 2012 - 05:09 AM

Ok, gonna hijack this but stay on topic.

So in my game I'm keeping it open source and I also am lazy when placing instances in the room. So the thing is, some instances have unique variables set in the Creation Code for each instance. Each of these variables has a default value, but not all instances will use those default values obviously. So currently I have

if !variable_local_exists(drop) drop=obj_Heart;

Of course "drop" is a variable that needs to be set in the Instance Creation Code. I can't just put drop=-1 in the Create Event because then it will override the Instance Creation Code if I set drop there.

So what would be the proper way to handle that, then? Other than my original method before I learned about Instance Creation Code, which was to create a library script and have each value referenced in that script (I do this with stats for enemies, having the parent object reference various scripts to retrieve the various stats such as HP, damage, move speed, image speed, and such). That method was too tedious for anything beyond "universal" values, so I need something for defaulting Instance Creation Code variables.



I'd advise not to hijack a thread as others would prefer that you create your own (it's kind of disrespectful to the topic creator in my opinion also). As for your dilemma, each instance has its own unique ID which through proper manipulation will allow you to reference variables pertaining to only that instance.
  • 0

#17 TheouAegis

TheouAegis

    GMC Member

  • GMC Member
  • 4725 posts
  • Version:GM8

Posted 03 May 2012 - 05:48 AM

So then you're saying I would need to have an external object or script check the ID of each instance and set the variables there? Which is basically what I was doing before I started using Instance Creation Code...

And as I said, it's a hijack but it's not. This is nearly identical to the issue the OP is having. Now yes, the OP in this case can actually set the variable to -1 at the start of the game. Someone piped up and said anything can be done without using variable_local/global_exists() by setting the variables ahead of time. So in that regard my question pertained to this topic, since it required clarification on how you would go about setting variables unique to each instance during the room creation using object-based methods without the need to CTRL-click every instance and set its variables.

So it might be poor programming method to use variable_local_exists() but it seems a heck of a lot easier than what I'm seeing the proposed alternatives are unless I'm misunderstanding.
  • 0

#18 Race Pro Kid

Race Pro Kid

    GMC Member

  • GMC Member
  • 23 posts
  • Version:GM8

Posted 03 May 2012 - 01:54 PM


Can I just get a straight answer? Please?

EDIT: So far (by myself) I figured out to make the executed code:

if variable_global_exists(gunToShoot) == false {
    variable_global_set(gunToShoot, 0)
}
But it gives the error that "gunToShoot" doesn't exist when I run the game. Isn't that the point of the code? Can someone help?



Put 'gunToShoot' in quotation marks ("") --- this should work:

if (!variable_global_exists("gunToShoot"))
{
   variable_global_set("gunToShoot",0);
}

/*---------------------------------------------------------------------------
This was the code I used to test whether or not the method worked, and it did:
------------------------------------------------------------------------------
if (!variable_global_exists("text"))
{
variable_global_set("text","Is this working");
}

draw_text(40,40,global.text);
*/

You can thereafter reference the variable by using 'global.gunToShoot'. Though, as everyone else stated, you should be cautious with using this method.


Thanks! It works like a charm! :D

Ok, gonna hijack this but stay on topic.

So in my game I'm keeping it open source and I also am lazy when placing instances in the room. So the thing is, some instances have unique variables set in the Creation Code for each instance. Each of these variables has a default value, but not all instances will use those default values obviously. So currently I have

if !variable_local_exists(drop) drop=obj_Heart;

Of course "drop" is a variable that needs to be set in the Instance Creation Code. I can't just put drop=-1 in the Create Event because then it will override the Instance Creation Code if I set drop there.

So what would be the proper way to handle that, then? Other than my original method before I learned about Instance Creation Code, which was to create a library script and have each value referenced in that script (I do this with stats for enemies, having the parent object reference various scripts to retrieve the various stats such as HP, damage, move speed, image speed, and such). That method was too tedious for anything beyond "universal" values, so I need something for defaulting Instance Creation Code variables.


Sure, you can take over, lol.

Edited by Race Pro Kid, 03 May 2012 - 02:18 PM.

  • 0

#19 Jobo

Jobo

    No neurons left today

  • GMC Member
  • 2405 posts
  • Version:GM:Studio

Posted 03 May 2012 - 02:23 PM

To have variables initialized at game start-up or instance creation, declare the variable in the required object's Create event.

Example:

Create event:
my_local_variable = something;
global.my_global_variable = something;

  • 0

#20 Katuko

Katuko

    GMC Member

  • GMC Member
  • 4886 posts

Posted 03 May 2012 - 02:52 PM

Even after reading the reasoning I still can't see why it can't just be set to -1 or something. Checking if it exists like that is - if only a very minor case of it - a waste of time and processing power.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users