Jump to content
Rebitaay

Is using global variables really a bad practice?

Recommended Posts

Hey all,

 

I've been learning scripting for the past 3 weeks and have been having a blast. I've create a lot of fun things for me and my friends.

Along my journey, I've heard the same thing over and over: Always use Private Variables because Global Variables are bad and should be avoided in most cases.

 

I kind of get why, since they can be modified from any script. But it's given me the mindset where I should never use global variables, ever.

Now, I'm working on a script that creates a respawn point, but it would be created in one script and deleted in another. To do this, as far as I know, I have to use a global variable. Is this bad? Should I just keep searching for a way to do it with a private variable?

Edited by rebitaay

Share this post


Link to post
Share on other sites

I think you're mixing up public and global variables. Public variables are "special" variables used to transmit the variable's value to all other machines with e.g. https://community.bistudio.com/wiki/publicVariable.

 

To answer your question, prefer private variables over global variables. Global variables are considered "bad" in programming but if you have to use a global variable it's fine. The world won't end. :)

 

EDIT: And to really answer your question, yes you'd probably use a global variable in your case.

Share this post


Link to post
Share on other sites
2 minutes ago, marceldev89 said:

I think you're mixing up public and global variables. Public variables are "special" variables used to transmit the variable's value to all other machines with e.g. https://community.bistudio.com/wiki/publicVariable.

 

To answer your question, prefer private variables over global variables. Global variables are considered "bad" in programming but if you have to use a global variable it's fine. The world won't end. :)

 

Yeah, my mistake. I've corrected the post and title. Thanks for your input.

Share this post


Link to post
Share on other sites

I recommend to prefix your global variables with your ofpec tag or an invented mission tag. That is to prevent other addons from writing over your global variable and vice versa.

 

An alternative to global variables, especially if there are plenty of them needed, is to make them local to an object with getVariable and setVariable. For example, the Nimitz scripts found in arma2 declare quite a few global variables in their scripts, like nimHang4, jdgNimArrestor, JDG_steamFX. I moved them into the 'jdg_carrier_spawner' object and now can use 'allVariables' to quickly read out the current state.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×