How do I keep the global variables'/array's values?

0 favourites
  • 13 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.
  • In my game, after I close the game and open it again, the global variables reset to their initial values and arrays reset (to blank).

    I never used the "reset global variables" action in any event.

    Is this how it really works in C2?

  • Every time you restart the game all variable will, by default be as they were set, by you in C2. If you want to keep them the same after you change them during runtime, you have to make a functionality that can load the changed settings. There are some different ways to do it, as I recall, so think the best you can do, is to start searching the manual and learn about the different ways and see what suits you best.

    Try to check save/load from file and see if that's what you need.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • nimos100

    ok thanks. I found dictionary and webstorage. I chose dictionary.

    Do I need to use "add key" action before using "Set key" action? or only "Set key" action?

  • Add key will as I remember either set a key if it already exist or it will create one for you with the value chosen. Where as set key will just set a already existing key, and if there are none it will do nothing. So if want to "fill" your dictionary you just need to use Add key and in theory you can simply use it for every thing, you don't really have to use the set key at all.

  • nimos100

    thanks. I have few questions:

    1. Can I use a key like this?

    "GameSave" & str(GameSave_Text.Array_X) & str(GameSave_Text.Array_Y)

    this is intended to add keys like

    GameSave00

    GameSave01

    GameSave02

    GameSave03

    GameSave10

    GameSave11

    GameSave12

    GameSave13

    and so on until GameSave43 (because my array size is 5,4,1)

    then set the values to their corresponding cells by retrieving from both the array and text objects.

    I used the first event in the pic (already working) for showing the values of array (GameSav_Array) into each text (GameSave_Text)'s instance variables called Array_X and Array_Y.

    Should the second event in the pic work?

    2. And since this is supposedly my solution for maintaining global variables's values even after closing, I can't really confirm if this all works without exporting it first before testing? or preview is enough to tell if it works?

  • But if you want to make save games, there are a build in action in C2 that you can use. Its in the system -> Save and Load. It will save the game in its full state.

    If you want to store for instant display settings, controls etc. I would personally just make a file that you read each time the game starts. And save any changes to this.

    A lot of games today will have a config file, where they store varies settings. so it would be like that.

    Regarding your examples, I cant tell if that will work. You will just have to test it

  • nimos100

    Well, I really did not try the game load and save of C2 (after trying the example file) because it's sort of cheating for the players to always go to exact same state XD

    I want the player to at least start the highest level where he/she last played and which hasn't been cleared yet.

    About the config file, so what do I have to use? You did not specify in your first reply so I search by myself and I found webstorage and Dictionary. Please do specify if you have another better method in mind. Much appreciated. Thanks.

  • nimos100

    I want the player to at least start the highest level where he/she last played and which hasn't been cleared yet.

    About the config file, so what do I have to use? You did not specify in your first reply so I search by myself and I found webstorage and Dictionary. Please do specify if you have another better method in mind. Much appreciated. Thanks.

    Not 100% sure what you mean, if the player load an old game, they will continue from where they left off, ain't that what you want?

    Anyway here is a very simple example of how to save to a file, that I made some time ago, for another post I think.

    It just store some text in a file.

    I don't have an example from the load data. Except from Dragonhelm, so it might seem a bit crazy.

    In the game all dungeons are random generated, and to make it so players can re-enter them, I store them in files, so I can load them in again. So the load function in the screenshot, read the file. Which would look like this for the tiles of the dungeon.

    Each number then represent a certain tile, and it will read through the file, until it reach an "" (Blank space) and from this it can rebuild the dungoen.

    What you can do, is to save the settings to a file, and then read in each setting you need and update it in the game, at the "Start of layout", as I have done in my game.

    Don't get scared off by my load example. It doesn't have to be like that. Just add the NodeWebKit functionality. But for some reason the help document for it, seems to just link to where you can download it. which is not very useful when you want to learn it I guess

    But maybe there is a tutorial that will tell you how to load files simply. Otherwise let me know, and I can try to make something simple to show it.

  • nimos100

    What I want to do is this:

    Say you unlocked up to level 3 and now playing level 4, when you close the game and open again, level 1-3 are still unlocked and you can start level 4 again.

    Based on the example file of C2 for game load/save, I thought you can save the "state", meaning, if you are in a level with long layout with a boss at end of layout, you can save the state just before fighting the boss. So if you die, just load the state again (you won't have to start the level, you're just back at that point (and place) before fighting the boss). This is what I don't want in my game. I would like the player to start at the very start of the same level again. That's why I save a global variable for levels and other stuff. If I misunderstood how C2's game save works, I apologize.

    Ok. I get the general idea of your examples. Don't worry. You helped me a lot with those examples.

    But I have a question:

    If I save like readable words or numbers in a notepad file, then players can easily just edit the notepad file's content ? then next time opens the game, the game would load those values? This is also about preventing players from another way of cheating I suppose.

  • Well the easiest way, is to just make it so the player can't save during the level. But only after or before a level. Wouldn't that solve your problem? So make the save button in the menu where they choose next level or maybe make it do it automatically.

    But yeah in my example with Dragonhelm you could cheat, but I don't have a problem with that in the games I make, if people want to cheat they can cheat as much as they like. I don't make competitive games anyway

  • nimos100

    If I save into a notepad file content like this:

    Level: 12

    Player: John

    Controls:

    jump - list key code

    right - list key code

    and so on

    How do I tell C2 to retrieve the text in line 2 after "Player: "

    I already added node webkit but I did not see an expression for something that specific, only readfile and clipboardtext

    or do I need to use Regex?

  • I wouldn't just store everything in a file, it would make it unnecessary complicated I think.

    So only data that you can control. Meaning you know output and input so its easier to read it.

    That way you can make a function that can parse the data after you have read it from the file. Here is an example that someone made of a simple parser, that I think you could use to create it. But still I would suggest that you make it so the player can only save and load between levels. And only display setting, controls etc are stored in a config file. I wouldn't store level and player name and so on.

    Anyway here is the tutorial (Simple parser):

    https://www.scirra.com/tutorials/757/simple-text-adventure-example

  • TRY THIS:

    For Saving:

    Use the save action whenever you want

    For Loading:

    Set the first layout to the one that will load data, and make it load the game on start of layout or go to a specific layout if it fails to load (when there's no save) like this:

    [attachment=0:a70fgryn][/attachment:a70fgryn]

    Then, it will bring you to the last place you have saved, but we don't want that. So set this condition on every event sheet:

    [attachment=1:a70fgryn][/attachment:a70fgryn]

    Place that condition everywhere but not in the destination layout (for example Startscreen), or it will loop.

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