How do I create new instances with a unique variable set (must validate existing instances first)?

  • hi,

    I am trying to create an easy situation where a game can have four unique objects, created as instances of the same object. every instance will have a variable set, to a unique number (1-4).

    What is the easiest way of validating the existing instances when setting the value to the newly created one, so it will be unique.

    Since instances can be deleted it is not possible to validate the index or UIDs.

    Do I need to store the variables to a dictonary or an array. Or is it possible to use a For Each -loop while creating the new instrance, to get the variable from the existing ones?

    See attached example (in the example I use choose(1, 2, 3, 4) to set the variable to the new instance, which of course will not check for uniqueness.

    (The example is just a test, I need to function in a multiplayer game, so every player will have a unique color, players will be able to leave and join, but always 1-4 players. The animations is just a different color)

  • You could run the choose and then check if that object.color=choose exists and if so run the choose again. You could have an array with 1,2,3,4 at x and pick a random x from 0 to array.width which chooses the number at random and then deletes it from the array so it cannot be chosen again i.e. it picks 3 you are left with 1,2,4 in the array for next time.

  • Thanks.

    haha, I cannot get it to work properly. Sounds like an easy thing to do.

    I am trying with an array, to set all the values in a array at index=i For Each Sprite.... with local variables.. also tried while loop.

    I would have guessed that it would be easy when I used numbers, since then I could just try to set it to 1, then step it +1 until no sprites exists with that variable. then assign it. But this was to hard for me on a sunday. :P

    Any more ideas, with event example :)

  • Thanks.

    haha, I cannot get it to work properly. Sounds like an easy thing to do.

    I am trying with an array, to set all the values in a array at index=i For Each Sprite.... with local variables.. also tried while loop.

    I would have guessed that it would be easy when I used numbers, since then I could just try to set it to 1, then step it +1 until no sprites exists with that variable. then assign it. But this was to hard for me on a sunday. :P

    Any more ideas, with event example :)

    https://www.dropbox.com/s/ab9f1q4kardngif/No%20Repeats.capx?dl=0

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Here is an array example and how I usually do this, although I do tend to be using arrays at the time for other things :) dropbox.com/s/r1wgrjx5o75kar1/pickarray.c3p

  • Thank you both!

    (It was a little bit harder, since it was for a multiplayer game. First create Array > initialize the room and fully fetch all the other users from server > remove their variable from the Array > create your own instance > send yourself to the others.. So had to be some "Waits" and re-shuffles :)

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