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

SOLVED Need some clarification.

flyinian

Member
Declaring a local variable once and using it multiple times throughout the same event for entirely different things....Could this cause bugs and such?


Example:

GML:
if(something)
{
var house;

house = object1;

//code that uses the new value of "house".
};

/// Lots of other code not using local variable "house".

if(something entirely different)
{
house = ds_grid_get(something, 1,1);

//code that uses the new value of "house".
}

/// Lots of other code not using local variable "house".

if(something totally different)
{
house = 32;

//code that uses the new value of "house".
}


My guess is that using the local variable like this is a recipe for disaster. Which is probably why my code isn't working as intended anymore.

The local variable "house" would need to be the same value throughout the entirety of the event to avoid unwanted outcomes, right?
 
just out of curiosity, why would you want to use a local variable over and over again but for different things inside the same object/event?
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
The local variable "house" would need to be the same value throughout the entirety of the event to avoid unwanted outcomes, right?
Yes. This is exactly how it should be done... There are some times where I would consider it acceptable to reuse a local variable, like if you have various loops in an event you could declare "i" once and then use it in all of them, but really, you want to keep local variables unique, and it'll help with readability, in my opinion, as you can name each one clearly for the task it's intended for.
 

flyinian

Member
Yes. This is exactly how it should be done... There are some times where I would consider it acceptable to reuse a local variable, like if you have various loops in an event you could declare "i" once and then use it in all of them, but really, you want to keep local variables unique, and it'll help with readability, in my opinion, as you can name each one clearly for the task it's intended for.
Okay, Thank You.
 
Top