How do I pick an instance created on runtime ?

0 favourites
From the Asset Store
Awardkit
$5 USD
Create custom awards and achievement or use the many templates and create your own.
  • I know my question is pretty basic, but for the life of me, I can't find any tutorial that explains clearly what I'm looking for, even though it seems it's quite a basic thing to set up ! Lots of forum message boards saying "just create an instance variable to keep track of the UID of instances created" without any more details, and that isn't very helpful to me...

    Here is a more detailed breakdown of my problem :

    I'd like to be able to differentiate, on runtime, between 4 different instances of an object created at runtime. So that when one of those is selected, the code knows which one was selected and acts accordingly.

    To be more specific :

    Here is the function that is called : it creates a button that gives the player a dialogue choice (and the parameters allow me to specify the position of each button as well as their text input) :

    Then in the event, I'm calling this function as many times as I need dialogue options for the player (here, 2 options) :

    But I can't then have the rest of the code follow accordingly to what button the played clicked, since I don't know how to pick those dynamicaly.

    I tried a lot of things with UID and all, but I must be doing it wrong since it never works (plus every tutorial I come across isn't tailored to my needs obviously, so it never works when I'm trying to translate it to my project).

    You can discard the crossed out section of the code, which is precisely one of my attempts, with an instance variable on the button object, to keep track of the instances' UID (it failed).

    Thanks in advance for your help !!

  • If it is based on which one the player clicks, you can use the condition 'on object clicked' under the Mouse object conditions.

  • If it is based on which one the player clicks, you can use the condition 'on object clicked' under the Mouse object conditions.

    Hye !

    yes, I know about that condition ; my main problem is to identify WHICH object is clicked, since there's only one object, but many instances of it. Each instance should not have the same result ; hence my need to be able to identify every instance individually on runtime.

  • For example, lionz, I actually came across a forum board where you said the following to a user :

    You have to do something a little extra for this, you would store the instance selected, for example the UID, in a local variable with your 'on selected' event. Then as a sub event you 'pick all' instances and set opacity 100, then again as a sub event you pick the instance by UID, the variable value you stored, and set it to 70.

    This is because when you use on selected event it narrows the selected instances to just 1, you have to open them up again with pick all to set everything back to default, then return to the original one instance to pick that and set it to 70. Hopefully that makes sense.

    I believe this is exactly what I need to be able to do ; but I don't know how to actually proceed to achieve the emphasized part !

  • Looks like you’re creating the four objects in specific locations. You could compare the x and y to pick specific ones. You could give the object an instance variable and set the variable to something unique when you create it.

    Start of layout
    — create sprite at 100,100
    — sprite: set var to “left”
    — create sprite at 200,100
    — sprite: set var to “right”
    
    On clicked on sprite
    — sprite: var= “left”
    — — do something
    — sprite: var= “right”
    — — do something else
  • > If it is based on which one the player clicks, you can use the condition 'on object clicked' under the Mouse object conditions.

    Hye !

    yes, I know about that condition ; my main problem is to identify WHICH object is clicked, since there's only one object, but many instances of it. Each instance should not have the same result ; hence my need to be able to identify every instance individually on runtime.

    'On object clicked' picks the instance that you clicked, then when you run actions it will be on only the instance that was clicked.

  • I see ; I will try what you propose ! R0J0hound.

    lionz

    My understanding is, "On object clicked" should be accompagnied with another condition that specify WHICH instance was clicked, isn't it ? Because each instance has a specific causal effect, so the rest of the code must evolve accordingly to which specific instance was clicked.

    Am I being clear ?

  • Looks like you’re creating the four objects in specific locations. You could compare the x and y to pick specific ones. You could give the object an instance variable and set the variable to something unique when you create it.

    Start of layout
    — create sprite at 100,100
    — sprite: set var to “left”
    — create sprite at 200,100
    — sprite: set var to “right”
    
    On clicked on sprite
    — sprite: var= “left”
    — — do something
    — sprite: var= “right”
    — — do something else

    Okay, after thinking about it, I don't think it's that simple, but maybe I'm missing something.

    Remember I'm creating an object (a sprite) two times, but everytime, it's a different instance of the same object.

    So the var will always be set to what the last instance created will have set as a var.

    Like, in this example where 3 instances of a unique sprite is created :

    create Sprite 1

    set var=1

    create Sprite 2

    set var=2

    create Sprite 3

    set var = 3

    In the end, the var will always be "3", right ?

    So whichever sprite I click on, the var being set on 3, it will always lead to whatever is programmed to happen when var=3

    And I would love to be able to use the position (which is a function parameter, named "position", that specify the position of the instance created) but I cannot use a function's parameter as a condition. I cannot add as a condition to "on object clicked", something like "pick the instance which parameter is position = XX".

    I do understand the answer lies in using an instance variable, it really seems to be the easiest way to go about it, but I still don't understand how to set this properly.

  • [

    R0J0hound

    After messing with your idea a little more... It looks like it's working !!

    Here is the function (and the setting up of the var according to the position of each instance, line 34 & 35) :

    And here is the code for the "on object clicked" :

    It looks like it's working for now ! I will go a little deeper with it and see if it holds.

    Thank you for your time !

  • I see ; I will try what you propose ! R0J0hound.

    lionz

    My understanding is, "On object clicked" should be accompagnied with another condition that specify WHICH instance was clicked, isn't it ? Because each instance has a specific causal effect, so the rest of the code must evolve accordingly to which specific instance was clicked.

    Am I being clear ?

    Nope it just picks the one that was clicked. The actions next to it only apply to that one instance.

  • > I see ; I will try what you propose ! R0J0hound.

    >

    > lionz

    > My understanding is, "On object clicked" should be accompagnied with another condition that specify WHICH instance was clicked, isn't it ? Because each instance has a specific causal effect, so the rest of the code must evolve accordingly to which specific instance was clicked.

    > Am I being clear ?

    Nope it just picks the one that was clicked. The actions next to it only apply to that one instance.

    I'm not sure I follow (sorry if I'm being dense XD)

    In that scenario :

    Create object "Sprite 1"

    Create another instance of Sprite 1 (but with different X,Y attributes given by the function's parameter)

    Create another instance of Sprite 1(but with different X,Y attributes given by the function's parameter)

    Create another instance of Sprite 1(but with different X,Y attributes given by the function's parameter)

    On Sprite 1 clicked => do action

    Every instance of sprite 1 created has to have a different consequence.

    It seems to me that only having as a condition "on object clicked" (here, "sprite 1" being the specified object) will not automatically inform the game which action to follow up with.

    I'm looking for a way to inform, in the code, what happens when a particular instance is clicked - where to go from there.

    In my experience, it doesn't work on its own. I tried the solution of having a instance variable (attached to the "sprite 1" object) evolve according to the instance clicked, and it does work, once coupled with "on object clicked".

  • Yeah you can pick them out with different instance variable values if they do different things. Depends what exactly they are used for. If you want to display text you can set the text in an instance variable on the object itself and say on object clicked > display text object.text

  • Yeah you can pick them out with different instance variable values if they do different things. Depends what exactly they are used for. If you want to display text you can set the text in an instance variable on the object itself and say on object clicked > display text object.text

    Yes ! That's actually what my question was about originally : HOW does one set up instance variables so that we can identify each one at runtime ? R0J0hound's solution took me closer to my goal and might be the solution I was looking for, but it created new problems also.

    So I'm looking for an example of how would one simply get an instance's UID when it's created, and use it afterward (preferrably by using an instance variable).

    When an instance is created, how to know its UID in runtime ? And how to use that information again in the code (how to keep it in memory, and point to it later, for example) ?

    (about your example about text : I actually use the function properties to inform the text on text object created, which in turn points to an array's coordinate, where all my text is. But I would still be curious as to how you would set that example with text up, since it might answer my more general question !)

  • If you create a sprite called blueSprite, you can load its UID into variable whoAmI with

    whoAmI=blueSprite.uid.

    If you want to use that specific sprite again later you can get it with

    pick blueSprite by UID whoAmI.

    I hope this helps.

    yours

    winkr7

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • If you create a sprite called blueSprite, you can load its UID into variable whoAmI with

    whoAmI=blueSprite.uid.

    If you want to use that specific sprite again later you can get it with

    pick blueSprite by UID whoAmI.

    I hope this helps.

    yours

    winkr7

    Hye !

    Thanks for the walkthrough.

    Would it work the same if there are 4 instances created at the same time ? How would the variable identify which is which ?

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