Loop Trouble

Get help using Construct 2

Post » Wed Feb 12, 2014 2:50 pm

[Solved]

Hey guys please help me i tryed using a loop today for the first time trying to get it to cycle through from 0 to a varible then as each number going through it would activate other events but for some reason i cant get my loop to work i really need help with this please

The loop can be found in the office events under photo group

Roger
darkrealos2014-02-12 17:21:29
B
16
S
5
Posts: 139
Reputation: 1,652

Post » Wed Feb 12, 2014 3:25 pm

I have looked at it, and its not easy to see what you are actually trying to achieve with it, some explanation would make it easier :)

Anyway as im not really sure what its suppose to do, I might be way off in what you are trying to do, in that case you can just ignore my post.

Just as a tips, its rarely a good idea to make a loop run every tick. But make it run in a function or on an event call, as it will otherwise run 60 times a second multiplied with the loop, so that loop you have added, even though it goes to max 5, can actually need up to 300 "checks" per seconds. And since there are no condition for it for whether it should run or not, it run every cycle.

Just on a side note, its a good idea to name them, especially if you use nested loops, so you are sure you are using the correct loopindex, should you need it.

The reason I think its not working is because you add 1 to "PhotoPlacement" but since the max times it should run is set at "On start of layout" the photoplacement will always be this number to start with, this might be ok?.

The reason for that is because the loop will run from 0 to PhotoRandomizer before it moves on. So if PhotoRandomizer on "Start of layout" is 3 then PhotoPlacement will also be 3. Not sure if you change it later on, but if not, then each time you run the loop, PhotoPlacement will keep counting up with 3 each tick. So at second tick Photoplacement is actually 6, so if you take into account that it runs 60 times multiplied by the loop every second, then after a second Photoplacement is actually 180, if im not mistaken :D


From what I can see, you don't actually need a loop in this case, you could just "Set Photoplacement = PhotoRandomizer" unless you change it some where else.
nimos1002014-02-12 15:28:15
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,838

Post » Wed Feb 12, 2014 3:40 pm

A hint is to use the Browser.Log action to pump debug statements to the console. This lets you see what's going on (press F12 to see console). You'll see immediately that the For loop runs constantly, as nimos100 mentioned.
ImageImageImage
B
71
S
21
G
230
Posts: 3,688
Reputation: 128,852

Post » Wed Feb 12, 2014 4:18 pm

Hey tyvm umm yeah i used the debug and saw it running constantly what i was trying to achieve im still got loads to learn about construct but im gunna have 10 random photos maybe more later on and i want it to pick between 1 to 5 random photos then when i click on an object it shows those random photos you get my drift? so what i was aiming for/thinking is that the for loop would run the events along side it selecting the a random photo then storing it but at the looks of what your saying im completely going the wrong way about it got any pointers

TYVM for any help i appreciate loads
B
16
S
5
Posts: 139
Reputation: 1,652

Post » Wed Feb 12, 2014 5:22 pm

TYVM for your time managed to find another way around it :) i love construct 2 there's always a way to work a way around it :)
B
16
S
5
Posts: 139
Reputation: 1,652

Post » Wed Feb 12, 2014 5:28 pm

No worries trust me I have had my share of problems and still do, think that's the same for everyone :D

A way you could do it, is to make a sprite that hold the images that you would like to randomly choose from, so each frame holds a photo.

This have some benefits to making them as separate sprites, its easy to randomly select between them, and you can add as many photos to it as you please very easy, without having to add any new code.

However if you plan on using animations, you shouldn't do it like this.

Since you know that you at max can get 5 photos to show and to keep it simple, you can just add 5 variables preferable to the object that handles them. If you don't have an object but just use globals you can add them there.

However just a tip, sometimes it can be a good idea to make a placeholder object in form of a 32x32 sprite which only purpose is just to keep track of something for you, instead of adding everything as globals. As the list of variables can get very long, and you cant use Booleans with globals. So having some small sprites outside the layout to help you keep track of things, wont hurt performance, but will make things a lot easier.

Anyway :)

These 5 variable you just add the frame number of the photos that should be shown.

You can do that by randomly selecting a frame number:

Assume that the object holding you photos are called Photoholder.


Set Photo_1 = int(random(0, Photoholder.FrameCount - 1)
Set Photo_2 = int(random(0, Photoholder.FrameCount - 1)
Set Photo_3 = int(random(0, Photoholder.FrameCount - 1)
Set Photo_4 = int(random(0, Photoholder.FrameCount - 1)
Set Photo_5 = int(random(0, Photoholder.FrameCount - 1)

You let it start at 0 since frames always start at 0.
The reason you subtract one from FrameCount is due to the same reason. As it counts the actual number of frames, so if you have 10 photos it return 10. However since you want to store the frame number, and it starts at 0 you just subtract 1 from FrameCount.
Doing it like this also means that if you decide that you would actually like to use 15 photos instead of 10, you can just add them and the code will include them automatically.

When you want to show them again, you just read these values. And if there are no value it should just be ignored.

Since you know that the value will always be greater than 0 if there are a photo stored, you can just check to see if that's the case, and if so it should show the image.

If photo_1 > than 0 Show photo
If photo_2 > than 0 Show photo
If photo_3 > than 0 Show photo
If photo_4 > than 0 Show photo
If photo_5 > than 0 Show photo

So you can either just give them a default value of -1 to start with and every time you want to generate new ones you just start by setting all of them to -1, before setting the photo that should be stored.

You don't have to use a loop for this, but if you want to do it, then you could do it like this:

For 0 to int(random(1,5))

If Photo_1 = -1 then Set Photo_1 = int(random(0, Photoholder.FrameCount - 1)
If Photo_2 = -1 then Set Photo_2 = int(random(0, Photoholder.FrameCount - 1)
...
..
Etc.

That should do the trick
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,838

Post » Thu Feb 13, 2014 12:53 am

Hey Sir sounds like the type of of thing im looking for and i appreciate you spending the time to try help me learn construct 2 some more but im rather confused do you reckon you could show me via capx file? be even better if u can add it to the one i already done

I really really appreciate the time your spending on me you have my many thanks
B
16
S
5
Posts: 139
Reputation: 1,652

Post » Thu Feb 13, 2014 12:24 pm

Here is an example, I just made it so when you press the button it will show up to 3 random photos on the screen, the photos are chosen from a list of 5 photos, but you can add as many images as you want.

CAPX:
Choose random photonimos1002014-02-13 12:32:17
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,838

Post » Thu Feb 13, 2014 3:53 pm

TYVM buddy gonna check it out now
B
16
S
5
Posts: 139
Reputation: 1,652

Post » Thu Feb 13, 2014 3:58 pm

ahhh thats cool but very confusing i dont quite understand the code but tyvm for taking your time to help ill keep hold of it :)
B
16
S
5
Posts: 139
Reputation: 1,652

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 12 guests