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

Get help using Construct 2

Post » Thu Feb 05, 2015 8:12 am

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?
B
16
S
7
Posts: 301
Reputation: 2,716

Post » Thu Feb 05, 2015 11:24 am

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.
B
44
S
11
G
2
Posts: 1,181
Reputation: 6,801

Post » Thu Feb 05, 2015 12:17 pm

@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?
B
16
S
7
Posts: 301
Reputation: 2,716

Post » Thu Feb 05, 2015 12:28 pm

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.
B
44
S
11
G
2
Posts: 1,181
Reputation: 6,801

Post » Thu Feb 05, 2015 12:49 pm

@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?
You do not have the required permissions to view the files attached to this post.
B
16
S
7
Posts: 301
Reputation: 2,716

Post » Thu Feb 05, 2015 12:55 pm

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 :D
B
44
S
11
G
2
Posts: 1,181
Reputation: 6,801

Post » Thu Feb 05, 2015 1:01 pm

@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.
B
16
S
7
Posts: 301
Reputation: 2,716

Post » Thu Feb 05, 2015 1:44 pm

sgn15 wrote:@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.

Image
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.

Image
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.
Image
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.
B
44
S
11
G
2
Posts: 1,181
Reputation: 6,801

Post » Thu Feb 05, 2015 1:56 pm

@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.
B
16
S
7
Posts: 301
Reputation: 2,716

Post » Thu Feb 05, 2015 2:03 pm

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. :D 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 :lol:
B
44
S
11
G
2
Posts: 1,181
Reputation: 6,801

Next

Return to How do I....?

Who is online

Users browsing this forum: bikachu, irwandwiyanto, omnipaws, tarek2 and 6 guests