How do I use Local Variables properly

Get help using Construct 2

Post » Sat Mar 08, 2014 3:30 pm

Hi there,

I have some issues with local variables.

My counter2 variable always gets zero again after running the loop once. I have tried 3 ways. Once with a global variable (worked). After it I tried the two ways in my Screenshots (Didn't work)

counterScirra.PNG


and

counterScirra1.PNG


Thanks for your Help :)
You do not have the required permissions to view the files attached to this post.
B
5
Posts: 27
Reputation: 277

Post » Sat Mar 08, 2014 3:38 pm

This is how local variable works, everytime you run the loop the variable is reinitialized.
B
39
S
15
G
5
Posts: 295
Reputation: 6,089

Post » Sat Mar 08, 2014 3:43 pm

But why, if I use a local variable in c# i make a method with a variable if i declare the variable not inside the loop but inside the function it won't be set to zero or what ever again

private int xy()
{

int a = 0;

for(int i = 0; i < 10; i++)
{
a++;
}
return a;

}

the return value would be 9 and not 0. Whats the difference?
B
5
Posts: 27
Reputation: 277

Post » Sat Mar 08, 2014 6:12 pm

That first example should work. What's getting appended to the text?
B
55
S
29
G
19
Posts: 1,520
Reputation: 25,650

Post » Sat Mar 08, 2014 11:06 pm

In both examples the text gets 111111111
B
5
Posts: 27
Reputation: 277

Post » Sun Mar 09, 2014 12:18 pm

Is there any workaround expect a global variable?
B
5
Posts: 27
Reputation: 277

Post » Sun Mar 09, 2014 12:33 pm

@beni92 - did you use the debugger and step through slowly to see it in action?
B
33
S
7
G
8
Posts: 312
Reputation: 8,528

Post » Sun Mar 09, 2014 1:05 pm

Can't set a breakpoint, it always says "cannot set breaktpoint here .."


looping.PNG


I also tried it outside of the function, still not working.

Thats why i have the Text, and the Text gets 1111111111

thanks for your help :)
You do not have the required permissions to view the files attached to this post.
B
5
Posts: 27
Reputation: 277

Post » Sun Mar 09, 2014 2:05 pm

@beni92 - I'm just curious, what happens if you take out the OR condition and just have Type = 0. What do you get for counter2?
B
33
S
7
G
8
Posts: 312
Reputation: 8,528

Post » Sun Mar 09, 2014 4:30 pm

thanks for your respond, but still only 1111111. If i do it with a global variable it works, but i donot want to have many global vals, and i would have to reset them every time. I donot like this kind of code. Why a global if a local would be enough :/
B
5
Posts: 27
Reputation: 277

Next

Return to How do I....?

Who is online

Users browsing this forum: Artcadev, dop2000 and 32 guests