Choose with exceptions/Dependent Events

0 favourites
From the Asset Store
Very simple code without excess options (15 events for server and 11 events for client)
  • I'd like to be able to choose a number from a dynamic list of numbers, I think in probability it's called a dependent event.

    I have three sprites (or one sprite and two instances of another sprite to save resources), each sprite needs to choose a number but cannot choose from one already chosen.

    For example the list is 0,1,2,3,4

    Sprite instances x y and z each choose in order.

    X picks 0, so y gets 1,2,3,4, it chooses 3. Now z can choose from 1,2,4 and chooses 4.

    so we end with

    x 0, y 3, z 4.   

    then these are destroyed by going off screen and the new instances can choose from 0,1,2,3,4,

    How would I go about doing this?

    I've been working on this problem for a few days now and tried using random trees but it just ends up being indecisive and I can't exit the loop because there is always a duplicate. I'd am looking for a way to set up such a dynamic list.

  • Try this example. It's basically the same issue, just adapt it to your needs.

  • Thank you very much! I got it working so long as the objects already exist, but my goal is to spawn new sprites, pick frames, then destroy after an interval. it seems that there is something not taken into account when new objects are created because they are always on the same frame, maybe the iids don't reset? How would one take newly created objects into account?

  • Working with IID is problematic. It represents an index, starting from zero, of all currently existing instances. If an instance is deleted the IID is arranged new, as well as if instances are added, in a way that the IIDs always range from 0 to # of instances - 1

    You could either work with UID which keeps its value, or with an instance variable, to gain better control over the sprites.

  • So I would need to create a function or loop to re-tally all the uids or instance variables to represent the newly created sprites each time they are created and then run the shuffle function that has been edited to look for

    Pick Sprite where Sprite.uid/instance var = loopindex?

  • That sounds complicated. <img src="smileys/smiley4.gif" border="0" align="middle" />

    The array in the example can be filled with everything, not just a raising number. If you fill it with the UIDs of the sprites/instances in question, and then randomly choose an array index like in the example, it will return the UID.

    Pick Sprite where Sprite.UID = array(r)

    If I have some time tomorrow, I'll make an enhanced example.

  • That would be very good of you, I greatly appreciate it. Handling multiple instances has been my biggest obstacle ever since I started construct, I feel like once I cross this hurdle things will get much easier.

    So you are suggesting that the shuffle/choose function takes account of the newly created sprite UIDs each time its run and then picks for them? I'm playing with that idea now trying to figure out the order of operations.

    Just a little background:

    ultimately i'm going to choose one of those three sprites each shuffle and prevent it from choosing the same frame more than three consecutive times, and remove certain frames from being chosen after they've been called more than 5 times. But I'll figure that out once this "shuffle" is down.

    It's essentially an educational game and the player is meant to choose the correct answer, the spite placement is also random(got it covered) so I was wondering if it would be easier to use two sprites, essentially two dummies and a dedicated answer for the shuffle function?

  • I just read your post. I already did an example now, but it is more general. On the other hand, you might learn more from it. This example is pretty simple. There's one sprite instance. Whenever you click on any one instance a new instance is created. When you click on the button, three instances will be destroyed. You can mix creation/destroying as you like. The algorithm behind it should be quite obvious. If not please ask.

    It just confuses me that you talk of sprites, instances and frames in a way that it all seems to be the same for you. Since those are terms for specific things in C2, I'm not quite sure exactly how the game mechanic will be.

    random_unique_selection.capx

  • deleted due to trouble adding capx links

  • Thank you again for your examples! I will be more careful when I speak of sprites, instances and frames in the future :D

    I wanted to know if I was on the right track. I think it's a problem with picking the instances and adding them to the array.

    In the first example it will generate random frames but they repeat still have duplicates.

    first

    in the second I tried adding UIDs to the array but wasn't sure how to pick them.

    blockuids.capx

  • I'm sorry, your capx' were done with r145, which is too buggy regarding audio for me to use. If you don't want to recreate them with r144.2 or r139, I propose to wait until a more reliable version is out. <img src="smileys/smiley6.gif" border="0" align="middle" />

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Haha, that's ironic. I upgraded to 145 to access your random destroy example.

    Here is an r139 version. I wanted to know if I was on the right track. I think the problem is with picking the newly created instances. I wasn't sure how to handle it, nor am I certain about the order of operations going on there.

    instancepickconfusion.capx

  • Here is the corrected version. The mean thing is: it's done with r144.2 (I don't want to reinstall different versions while I'm developing a project)

    There were a lot of issues, too many to explain them all. Most of them were already addressed in my last example. You might want to have a closer look at it and this new one. For example, you added an array to your project but the initial size was kept at (10,1,1). With already 10 cells filled with the value 0 you will never get any other frame than 0 ;)

    Try to look very closely at the projects. All there is exists for a good reason <img src="smileys/smiley14.gif" border="0" align="middle">

    Oh, and last: If all you want is having 3 instances that have different animation frames that change on some event, you don't need to destroy and recreate the instances. Just shuffling the frames would be sufficient then.

    instancepickingNOconfusion.capx

  • Thank you again, I continue to play with it but I feel like you might be babying me at this point.

    So one more question, what needs to be done to pick a random instance of the three new instances?

    <img src="http://i39.tinypic.com/358pero.png" border="0" />

    Do I need to have their UID as part of the array when they are created and pick randomly from one of them? Setting up an XY array where x is the frame and y is the UID? much like the random destroy but with a two dimensional array

    Is it a parameters issue? Where I need to return values in order to pick from those three?

    and about the destroy bit, they will be given bullet later and go off screen, creating 3 more. Though I guess they could just be moved back and bullet across infinity after changing frames and picking a new "answer" block

  • You don't need anything to pick a random instance, just that exact event you are using.

    The problem here is that C2 seems to work the same way CC worked. That means if you create an instance it is only accessible in the same event, where it is created. Any other events can only access it one tick later. The instance is created at the end of the current tick, so when you tell C2 to choose a random instance, there are no instances to choose from.

    If you give it at least one tick time, it works. For example, make the random pick event a subevent just like this:

    + (empty) -> System Wait 0.1 seconds

    ++ Pick a random ...

    how long you have to wait is dependant on the framerate. With 60 fps one tick needs about 17 ms. If it also is a stable framerate, then waiting 0.02 seconds would be enough.

    You could also store the current tickcount to a variable in the function, and then outside the function check if tickcount = variable + 1, and if so pick a random instance.

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