Do I need to use instance variables or global?

0 favourites
  • 7 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.
  • Hi,

    I'm developing a multiplayer sci-fi game with minimal graphics, in which most game data is shown as text display information and numbers. For example instead of seeing a ship moving through space, the player sees a readout of ship speed on the screen.

    When I first started this project a few months back, I stored variables like ship speed, engine power and so on in global variables, which change over time.

    This morning I was wondering if this is in fact a mistake, since in my game a player will have the option to buy and own more than one ship. How would I then save the ship stats from the global variables for each ship, as they will be different.

    Then I thought, is it a better idea to create an off-screen object which represents the ship, then store the ship stats inside instance variables for the object? The player can own as many ships as he wants and each (invisible) object will contain its own saved stats. This will allow me to decrease the number of global variables in my game by 50% as the way its developing there are a lot of variables going into a single ship which affects the readouts the player will be seeing on the screen. Hopefully this will also reduce the cpu usage?

    The same question also applies to things like ship systems for examples weapon fire; their power; rounds and so forth....would I need to create weapon objects too to store these variables, or global? Right now everything in my game is global lol...

    What do you think?

    Is there a limit yo the number of instance variables an object can carry? I need about 50-100 for each ship (maybe more or less) as I'm making a very complex game.

    Thanks for your help.

  • Check this link:

    There are a few good points why you shouldn't use many global variables in your games.

    You can use that Globals plugin, or any other "dummy" object to store instance variables, for example a small invisible sprite. As far as I know, there is no limit for a number of instance variables.

    Or instead of instance variables you can use a dictionary. Create a separate instance of the dictionary for each ship and store as many keys as you need.

  • I would try to arrange the events in groups and use lokal group variables. For me that is much more clearer and lokal variables are some times the better choice over instance or global variables.

  • Check this link:

    There are a few good points why you shouldn't use many global variables in your games.

    You can use that Globals plugin, or any other "dummy" object to store instance variables, for example a small invisible sprite. As far as I know, there is no limit for a number of instance variables.

    Or instead of instance variables you can use a dictionary. Create a separate instance of the dictionary for each ship and store as many keys as you need.

    Thanks dop2000. Your response is great as always and puts me in a direction to learn more about the programme.

    I might play around with the dictionary vs invisible sprite in a simple construct 2 programme to see how they work, and ease of functionality for storing lots of variables. I'm someone who likes things to be organised well so we'll see how it works haven't used a dictionary yet...

  • I would try to arrange the events in groups and use lokal group variables. For me that is much more clearer and lokal variables are some times the better choice over instance or global variables.

    What is a local variable? I think I made one by accident a while back when I tried to put a global variable inside a group.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • What is a local variable? I think I made one by accident a while back when I tried to put a global variable inside a group.

    That is how you make a local variable. The scope of the local variable is only for this group. The benefit is if you make an event and use a variable you will only see the globals and the lokal variables from this group. It's all about clarity.

  • I would try to arrange the events in groups and use lokal group variables. For me that is much more clearer and lokal variables are some times the better choice over instance or global variables.

    Since he needs to store variables for several ship instances, neither local nor global variables is a good choice here.

    Instance variables or a dictionary is a way to go.

    If I had to store about 100 parameters for each ship, I would probably use a dictionary ("Ship").

    Add an instance variable ID on the dictionary. Create several instances of Ship with IDs =1, 2, 3 etc.

    Then pick Ship dictionary by ID and set/get its keys.

    One big advantage of the dictionary is that you can loop through its keys. Which you can't do with instance variables.

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