How do I Make in game Shop/upgrades without Global Variables

0 favourites
  • 11 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 there, I have been struggling with this for some time. Im working on a game project where you can upgrade your ship and weapons stats in shop than continue to another level.

    Something like in Tyrian (

    shop screen is in 2:10) Top Down shooter, but bit more complex. I want to change statistics of each weapon and each ship. Ship statistics would be max speed, agility, HP, Shield points etc. Weapons would have its own stats.

    Here is the catch, I want to make it somehow without using lot of global variables (maybe 4 to 6 maximum). Is it even possible? If not, how many of global variables I should use.

    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
  • Absolutely possible. You can store the data in either an Array or a Dictionary object (I prefer Arrays so I can just reference a spreadsheet of my own). You would store the bass stat for each as 0,1 0,2 0,3 and so on. Then the upgrade number in 1,1 1,2 1,3 etc. Then I would set the "result" in 2,1 as Array.At(0,1)+Array.At(1,1) so then you can reference all three numbers in your UI to make things even clearer for your users.

  • Thank you for your reply C-7, Im kind of scared of using arrays, it is much easier to pick global variable by its name like "WeaponSpread" and than using it instead of coordinates in array to localize the right value.

    I am afraid it would be very confusing if I add more and more statistics, if you know what I mean. Is there a way to somehow name the value and pick it by the name, instead of coordinates ?

    I tried to use dictionary just for taking the values from layout A to layout B and I failed. Can someone make a simple .Capx of how I can do that or link to some tutorial?

    Arrays and Dictionaries are pretty vast topic, so I would love to know where to start.

  • I don't like posting links to the manual, but the Dictionary object would do precisely what you are looking for. You'd use something like Dictionary>Add "shield" and set it to 50 or whatever. Then you can reference that value at any time Dictionary.Get("shield") instead of your global variable call.

    The manual: https://www.scirra.com/manual/140/dictionary

  • Thanks, I've already been in the dictionary documentation, looks like I did something wrong in my project. I'll look in to it deeper, I hope I will make it work this time

  • WebStorage is also an option, especially if you want data to persist even after game closes (for example, save game progress)

    https://www.scirra.com/manual/120/webstorage

  • Hi thx for your responses guys, I bumped in to a problem, it seems like Dictionaries arent able to store instance variables of some object as values. Am I right? How can I store value as an instance variable of some object than transfer the value from instance variable to a another layout.

    How can I do it? Thanks in advance for any reasonable reply !

    Capx below

  • You are doing it right but, you forgot to create an object "cage" on the LO_RabbitsNumber layout. So when you assign the "Rabbits" value of the dictionary to the instance variable of cage you are assigning that value to nothing, because there no object to assign that value to.

    Also, it would be better to increase, decrease the value of items when the + or - buttons are clicked, not after going to another layout.

  • I don't have much to offer but a helpful tip would to be to add webstorage like russpuppy said. If this is going to be a mobile application then you should webstorage to save the players purchases. I would hate it if I purchased something from a store and then when I came back to play again, I would have to start over.

  • Im making progress it works now, thx donelwero! I feel little dumb to not being able make it on my own.

    So, another questions is better to use webstorage or dictionary?

    Should I use webstorage only? It looks like a better idea if I want to store these values after the program restarts. Or is there some profit of using Dictionary and than save the values to webstorage?

    Working Capx below.

  • There is an alternative method. If you make your object Cage a global object then it won't get destroyed between layouts... You could store your ammo/weapon data inside its instance variables, which you might find easier to visualise in the event system because they are all drop down items. Just keep it invisible / on layer 0 / no collisions etc so it doesn't interfere with the game during runtime. I've just started using global objects with variables for things like score, energy, player mode FSM and the game FSM - I find it much easier to work with in C2 than the dictionary object.

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