Loop Trouble

0 favourites
  • 13 posts
From the Asset Store
For Adventure, Action, Heroes, Sci-fi and Action-RPG games.
  • [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

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

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

  • 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

  • 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 :)

  • 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

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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

  • 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 photo

  • TYVM buddy gonna check it out now

  • 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 :)

  • Here is an explanation :)

    1. When you press the button, it will reset the 3 variables that holds the photos. To make sure that they don't hold photos already. Also I destroy all PhotoPlaceholder objects, as I create 3 new ones that should hold the photos that are displayed on the screen. This is to clean up so there are not several objects overlapping each other.

    2. Since I just destroyed all PlaceHolders I need something to copy the frames from, so I make 1 PlaceHolder to copy from.

    3. Then I select how many photos should be displayed, 1 to 3, for each of these, it will check to see if there are already a photo stored in the photo variable if that's the case it will check the next variable and so on.

    4. Then it check each variable to see if there are an photo stored. If that's the case it will create an image on the screen. And it will do that for each variable.

  • I think i get it now the only thing confusing me is that FOR loop i just cant seem to understand it

    also im so grateful for all the help i know this may sound rude after all you've done but can i message you if i ever need help?

  • Sure, however my visit to the forum changes quite a lot, sometimes I check it a lot and other times not for several weeks so if I don't answer you know why :D

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)