Global variable or instance variable

0 favourites
  • 3 posts
From the Asset Store
Globals 2.0
$3.99 USD
Globals 2.0 stores and group variables. You can also load and save data (variables) from/to JSON files.
  • Curious as to what is the better practice for storing variables like Experience and Level. My game has a single Hero, and right now I am storing Experience and Level on the Hero, but wondering if I should instead store it as a global variable? Just wondering what the recommendations would be and why?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • If you know you will only have one sprite ever that uses variables with those names, then I guess making them global wouldn't be a problem, however...I think it's always best to use instance variables 99% of the time when dealing with sprites. One reason would be that you won't later have to convert variables from global to instance when you run into a conflict on down the road, so it could save you a lot of time.

    Another reason for me is that it makes more sense to me when it reads like English. I could forget what global variable goes with what. But if something says Hero.Experience, it's easier to remember and understand.

    I do use "global" variables, but they are still instance variables. For everyone using the Free Edition, each global variable takes away from your 100 allowed events. So I created an array called Global. I don't use it as an array, but simply a place to put variables I want to use as global. So, they read as Global.Level, Global.CurrentScore, Global.NumberOfBalloons, etc. It is so helpful to read the word 'Global' before the variable, that I have continued doing this even after buying Construct 2.

    I only have 4 true global variables in my game, and they are called True, False, Player, and Computer. These never change their value and allow me to use all Number variables throughout the game, so I can use "boolean" variables in equations.

  • In general, if your project is going to get large I'd highly recommend that you use static local variables within groups as opposed to global variables. The global variable drop-down gets very cluttered very quickly if you are overusing them.

    My player's stats are stored this way, and I access them using getter functions.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)