How do I use variables and layouts?

Just started using Construct 2? Post your questions here

Post » Wed Jul 23, 2014 2:06 am

i have for example a global number called box with the value 1 declared in layout1.

If i set the box variable with the value 2, i can set to 2 and use a webstorage as set local key to "2".

Then i change to the layout 2 and on start of layout 2 i set box to webstorage.localvalue("2").

The thing is when i change to layout1 i need to also set the variable box to webstorage.localvalue("2") to load it because when i change to layout 1 is declared as 1.

The question is... i need to load the variable in every layout all the time?

hope y can explain the question.
B
31
S
13
G
6
Posts: 355
Reputation: 5,058

Post » Wed Jul 23, 2014 2:23 pm

let me reformulate the question:

if i have a global variable and change the value i need to load with webstorage in every layout?
B
31
S
13
G
6
Posts: 355
Reputation: 5,058

Post » Wed Jul 23, 2014 2:32 pm

global variables keep their values when changing layouts, only closing the game, use the reset global variables action or setting them yourself will change their values.
Game design is all about decomposing the core of your game so it becomes simple instructions.
B
53
S
22
G
18
Posts: 2,122
Reputation: 17,123

Post » Wed Jul 23, 2014 5:29 pm

This means if i change the value of the variable i need save it in webstorage and then need to webstorage load in every layout right?
B
31
S
13
G
6
Posts: 355
Reputation: 5,058

Post » Wed Jul 23, 2014 9:27 pm

You only need to save the global variable if you are going to quit the game and restart..
Global means that it is accessable from every layout, so when you set it in layout one, what you set will be available in layout two..
If however you quit the game this info is lost..

No need to save to webstorage unless you reload the entire game..
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Thu Jul 24, 2014 1:09 am

If im correct if i have a variable with the value 1 and change it to the value 2 and change the layer the variable is automatically set to 1 again.... mmmmm... now i think why... is because on start of the second layout all the variables are reseted....
B
31
S
13
G
6
Posts: 355
Reputation: 5,058

Post » Thu Jul 24, 2014 1:44 am

@katzin, hi...

If the global variable (GV) is resetting to the default value between layouts, then you must be telling it to do so somewhere in your code.

GV's are accessible at any time from any layout. So if GV is set to 1 in layout1, it will still be 1 when you go to layout2. Make sure you are not resetting it when the layout starts.

Can you provide simple capx...
If it can be done, someone on the web will show you how to do it!

CDASI Games Mentality Break Splat-a-bug FlapFleet Challenge
B
35
S
13
G
7
Posts: 363
Reputation: 7,283

Post » Thu Jul 24, 2014 3:55 pm

I found what is the problem:

Im taking one of the templates that when the start of layout all the variables are reseted.

This means in layout 1 i change the variable and when i changed to layer 2 is changed to the original value because of the "on start of layout reset all the variables".

So the solution for me was in layout 1 save the value with webstorage and in layout 2 after the reset load the value saved before.
B
31
S
13
G
6
Posts: 355
Reputation: 5,058


Return to Beginner's Questions

Who is online

Users browsing this forum: No registered users and 3 guests