To global variable or not to global variable?

0 favourites
  • 5 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.
  • This question is for those with formal programming knowledge.

    I know that it is advisable to limit the number of global variable, that said I have a question on how to handle a situation.

    I have 10 turrets, each turret can be level 1-10.

    The game is multiplayer, and loads via ajax the opponents turrets and level.

    All good so far, but a level 1 turret fires a bullet with 5 damage per second (dps). A level 10 turret fires a bullet with 100 dps.

    Each Turret has to have the dps information loaded some how.

    My question revolves around the "On Created" condition (or another if someone knows a better way).

    Possible solutions:

    1. An Event sheet that has ALL of the turret information as global variables. This works fine and have used it before.

    2. Put all of the information in the turret (or a new sprite as a placeholder and keep things "clean"). (Use instance Variables)

    Anyway, just looking for a clean way of handling this, because in reality I have 100 turrets.

    Thanks in advance!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I like to use a Dictionary for things like this

    For example, the keys could be the level of the turret, and the value could be the DPS

    Keys | Values

    1 | 5

    2 | 15

    3 | 30

    4 | 50

    10 | 100

  • If one uses a dictionary, are you loading it via a json? At the start of a layer? Putting (loading) the dictionary information in an separate event sheet to keep it straight?

    I may be asking obvious questions, just never have found a good way of handling this.

    About to have 100 variables, and trying to have a good solution. Gonna be programming this part in the morning.

    10 building types x 10 levels dps, same for hps...so 200.

  • I would use instance variables on the turrets (and buildings and other unit types) so that every unit knows its level, dps, health, cooldown, time to next level, etc.

    then I would store all the values for the different levels in an array.

    I sometimes use a dictionary with the array to keep the code easier to read.

    so, the dictionary might look like the: turrets | 0, buildings | 1, tanks | 2, ...

    and then to see what a level 4 turret dps should be:

    dps = array.at(dictionary.get("turrets"), 4)

    the dictionary and array can be read from local storage, or read from a project file or from a database using AJAX.

  • Thanks for the input.

    Decided to load it all from a json via ajax. Allows for ease of writing it via a spreadsheet and a quick load on start.

    I'll just use a function to load sprite details (dps, hps, etc) on ajax complete (ie, once the player gets the base information from the server.)

    Thanks much!

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