How do I set a variable to only a certain number of my objects via the event sheet?

0 favourites
  • 8 posts
From the Asset Store
Very simple code without excess options (15 events for server and 11 events for client)
  • eg. I want 30% of my objects to have 1 as their variable and the remaining 70% to have 2.

  • Maybe something like this?

    1. Make a variable like "TotalObjects".

    2. Run a loop that makes TotalObjects increment by 1 for each existing object.

    3. Run another loop for every object, with another variable like "TemporaryTotalObjects" that is initially equal to TotalObjects.

    4. In this second loop, if TemporaryTotalObjects is more than 30% of TotalObjects, assign it to the value you want 70% of your objects to have. If TemporaryTotalObjects is less than or equal to 30%, give them the other variable.

    5. Decrement TemporaryTotalObjects each time the loop runs.

    So, for example, if you have ten objects:

    TotalObjects = 10

    TemporaryTotalObjects = 10 for Object #1, so its variable is 1.

    TemporaryTotalObjects = 9 for Object #2, so its variable is 1.

    TemporaryTotalObjects = 8 for Object #3, so its variable is 1.

    TemporaryTotalObjects = 7 for Object #4, so its variable is 1.

    TemporaryTotalObjects = 6 for Object #5, so its variable is 1.

    TemporaryTotalObjects = 5 for Object #6, so its variable is 1.

    TemporaryTotalObjects = 4 for Object #7, so its variable is 1.

    TemporaryTotalObjects = 3 for Object #8. TemporaryTotalObjects is now less than or equal to 30% of TotalObjects, so Object #8, #9, and #10 get 2 as their variable.

    I hope that makes sense. That's just off the top of my head.

  • Sprite set variable to (Sprite.IID<(Sprite.Count*0.30) ? 1 : 2)
    

    If you want to randomly distribute these values:

    For each Sprite Order by random(1)
     Sprite set variable to (loopindex<(Sprite.Count*0.30) ? 1 : 2)
    
  • Thanks, but can i use this to do more than only two variable? So for exemple can I make

    35% = 1 , 55% = 2 and the 10 last % = 3 ???

    I tried to figure it out by myself but it wasn't conclusive.

  • For each Sprite Order by random(1)
    
     loopindex<Sprite.count*0.35 : Sprite set variable 1
    
     Else
     loopindex<Sprite.count*0.90 : Sprite set variable 2
    
     Else : Sprite set variable 3
    
  • You already helped a lot but I still have an issue. I haven't used loops often and I am completly clueless with using loops and loops-index even tho I tried to figure it out by myself.

    So if it doesn't bother you too much, can you please explain how this work with more details:

    For each Sprite Order by random(1)
    loopindex<35 : Sprite set variable 1
    Else
    loopindex<55 : Sprite set variable 2
    Else : Sprite set variable 3

    -Thanks

  • I fixed some mistakes in my previous comment.

    Here is a demo:

    dropbox.com/scl/fi/uqgd4mpi4823mi0wtmvzd/randomColors.c3p

    Another and better option is to use the Advanced Random plugin. Create a probability table with three values, and then for each sprite assign a value from it. Sounds scary, but it's in fact pretty simple.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thank you again.

    About the Avanced Random plugin, It was what I was using at first but the result where never the same. In short, the number

    of sprite affected(for eg.set value to 1) were never constant.

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