limit the creation of objects by type of instance.

0 favourites
  • 6 posts
From the Asset Store
Build your own spaceship with the various components in the pack
  • Howdy Y'all, I'm here again asking for some wisdom.

    What I'm trying trying to achieve is make several instances of a object. Then limit how many of that type of object there can be. So for example, lets say I spawn five objects, in three seperate rows and assign a random color to each (red,blue,yellow). I want to make it that there can never be more than three of the same color. So, for example:

    red, red, blue, yellow, yellow

    yellow, blue, blue, red, blue

    red, blue, red, yellow, yellow

    so in the above, I know there are 5 of each color and want it so that no matter how many times I run the code, there will always be 5 just in different spots.

    I attached a capx and have tried adding to a global variable but that didn't work out and for each is not exactly doing it. Any help would be greatly appreciated.

    app.box.com/s/b0n9w15me7iulddtlaisox90ed10v44v

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • This would be my approach to it, though there are probably many ways to do it :

    dropbox.com/s/vuvyzca319jct0l/ArcadeStacker_edited.c3p

  • This would be my approach to it, though there are probably many ways to do it :

    https://www.dropbox.com/s/vuvyzca319jct0l/ArcadeStacker_edited.c3p?dl=0

    is that a Construct 3 file? I don't have construct 3, only 2.

    **EDIT**

    So i figured out how to open it in c3 free edition and I can't seem to make heads or tails. I can see you made a function, but not sure how that function works (pardon the pun?)

  • It chooses a colour then checks if the limit was reached for that colour, if not then it assigns that colour. If it hit the limit then it runs the function again, it'll keep running the function until it chooses a colour that hasn't hit the limit. The block at the top where it sets variable to 99 stops the function from looping forever when you create more orb objects than the limit so at that point it will just keep creating the grey default ones, for example in a scenario where you said no more than 3 can appear which would be 12 orbs and you decide to spawn 16 orbs, the last 4 will be grey.

  • It chooses a colour then checks if the limit was reached for that colour, if not then it assigns that colour. If it hit the limit then it runs the function again, it'll keep running the function until it chooses a colour that hasn't hit the limit. The block at the top where it sets variable to 99 stops the function from looping forever when you create more orb objects than the limit so at that point it will just keep creating the grey default ones, for example in a scenario where you said no more than 3 can appear which would be 12 orbs and you decide to spawn 16 orbs, the last 4 will be grey.

    Yeah, I see that now. I had to really buckle down and even wrote it out on paper to understand it. It's a very simple and elegant solution, well to me atleast. Bravo my guy. So in theory, if I add another color, I would just extend the code in two ways:

    1. Add another variable for the corresponding color.

    2. Add another condition in the function for that color.

    I am curious though, is this bit of code needed:

    -> Browser: Log in console: "setting "&Orb_Base.UID&" to red"

    I would assume no, but I wanted to ask first.

  • If you duplicate the block for another colour I think it should work yes. The logging is not required, this is for testing purposes and debugging. With that I could see in console when colours were assigned and when it ran the function.

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