Reset Global Variables at the End of the Loop

Get help using Construct 2

Post » Tue Jul 26, 2016 11:46 pm

Hi, all.

How would I reset the variables used in a single spawn of some sprites?

Basically I have

Repeat 49 times
System block1_spawn (variable) does not equal 49 - make some blocks using h and y values

this spawns a horizontal line of blocks for me

I want to repeat the process/use the group again in another location (since it spawns only once because of the does not equal condition).

I would need to reset the variables **AFTER*** my block1_spawn variable reaches 49 in this instance.

I checked videos, tutorials, loopindices, system conditions, Googled it too, but I can't find this anywhere.

I am very new to programming. Could you provide an actual Construct 2 coding example?
B
40
S
20
G
18
Posts: 270
Reputation: 13,022

Post » Tue Jul 26, 2016 11:59 pm

You could have a sub event in the loop that compares the loopindex to see if it has hit 49 yet - then call your action to reset your variable.

System -> compare two values
[Main loop is here]
---[loopindex = 49] (or 48 if you've used a 0 based beginning to the loop)
------ set global.variable to 0

~Sol
Tired of crappy file hosts that are crappy? Get DROPBOX - https://db.tt/uwjysXJF
Moderator
B
84
S
34
G
40
Posts: 3,032
Reputation: 30,499


Post » Wed Jul 27, 2016 12:15 am

I'm having a hell of a time finding loopindex to access. Found a post about some other peeps complaining that loopindex is name of the loop, not intuitive at all in Construct 2? Can't figure out how to code/access the code for this.
B
40
S
20
G
18
Posts: 270
Reputation: 13,022

Post » Wed Jul 27, 2016 12:22 am

It's an expression, so it will need to be used in something like "compare two values" or "is between values" which are system conditions.

loopindex will reference the current loop being run, so if it's a sub event to your loop (or used as an additional condition or action expression) then it will not need any reference to which loop.

In your condition simply go to System -> compare two values and in the top box just type in loopindex and then set "is equal" and in the bottom box put 48 or 49 depending on your loop starting position. :)

~Sol
Tired of crappy file hosts that are crappy? Get DROPBOX - https://db.tt/uwjysXJF
Moderator
B
84
S
34
G
40
Posts: 3,032
Reputation: 30,499

Post » Wed Jul 27, 2016 12:50 am

"In your condition simply go to System -> compare two values and in the top box just type in loopindex and then set "is equal" and in the bottom box put 48 or 49 depending on your loop starting position. :)"

That was very helpful (would've been Mt. Everest for me; believe it or not nothing comes up on You Tube under "Construct 2" and "loop index").

Unfortunately code is still not working for me. I tried it with group activation and reactivation and just doing the set to variables (manual plugin).

I guess since I'm reusing and resetting the same variables, I would need to run the groups in sequence? How would I do that? Basically tryin to spawn 2 horizontal lines of blocks in sequence (only once as a first layout).

Only way I found (by Googling) is as a sub-event, but it will not let me make the whole 2nd group (basically a clone of the first for a new line w/ offsets) a sub-event of the first.

Also, I don't think the code is working as intended because I still need it to spawn a block at a value of 49 and reset AFTER. I could be wrong.
B
40
S
20
G
18
Posts: 270
Reputation: 13,022

Post » Wed Jul 27, 2016 1:21 am

Ok, turns out the sequence thing is easy just by incrementing and making a new variable.

Also, loopindex needed to be 48 like you said :) (forgot about the 0 thing).

So it is spawning 2 lines for me now, but not all the way to the edge of the layout, like it did when I was just using the first spawning group for 1 horizontal line.

Will play with it to figure it out.

Thanks so much for the help.
B
40
S
20
G
18
Posts: 270
Reputation: 13,022

Post » Wed Jul 27, 2016 1:56 am

Ok, it's absolutely bizarre.

The 1 line spawns 49 blocks, but it's not what the code is supposed to do because of some built in chance.

But the 2 lines don't spawn all the way to the edge of the screen (same cloned group) because the code is working as intended, so it's an easy fix.

I thought the repeat x times (without a loopindex condition to end) was creating an invisible infinite loop, but it's not based on the number of spawned blocks checked with a text variable, just 49.

So it seems to be a bug, but the code works correctly in the multiple spawn line version, which is what I need.

If anybody cares, the code started working correctly only after the loopindex, variable resets, and #oflinesspawned were added (now does the same thing for 1 group or 2).

Otherwise it was counting a non-spawn because of chance as if it didn't happen at all and going to the next possible spawn (the internal loop index in the repeat) was not updating at all.

This doesn't seem to be explained anywhere in the documentation as far as I can tell.

-----
Ok, I got this. Which is a bit ahead of myself :)

It's not a bug. The way I had it set-up with the variable checking for whether to execute the repeat or not, it wasn't updating the variable for the condition check because I had the variable incrementation done on only 1 out of 3 possibilities, so it could go up to 49.

What it fixed it was the variable reset on actual loop index check (it could no longer keep going up to satisfy the condition), thus doing the odds properly.

I learned a lot :)

(Sorry for the multiple posts.)
B
40
S
20
G
18
Posts: 270
Reputation: 13,022

Post » Wed Jul 27, 2016 4:47 am

Did you get it working?

Learning how to do it is the best way! A lot of people ask for capx to be made for them, so it's refreshing to see someone actually working through it.

If you're still stuck, post your capx here and I will take a look at it ;)

~Sol
Tired of crappy file hosts that are crappy? Get DROPBOX - https://db.tt/uwjysXJF
Moderator
B
84
S
34
G
40
Posts: 3,032
Reputation: 30,499

Post » Thu Jul 28, 2016 1:58 am

Yes got it to work perfectly!

Thanx so much!

Owe ya.

On another note the -system-repeat- seems to have trouble with ELSE statements if you're doing a lot of different probabilities, but no problem with OR blocks.

I may not be understanding the ELSE logic properly, but from what I read it should've been working.

Eh, doesn't matter.

OR works.

Thanks again.
B
40
S
20
G
18
Posts: 270
Reputation: 13,022


Return to How do I....?

Who is online

Users browsing this forum: dop2000, irina and 8 guests