How can I set variables or perform functions based on specific criteria without being repetitive

0 favourites
  • 5 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • Hi, I am not sure if I am asking the question the best way, or if this answered and I can't find it or just don't understand...

    I've made a handful of small games with Construct 2 and can't seem to figure out how (unless it can't be done) to program functions or set variables dynamically based on objects that meet given criteria. (eg object clicked, or object in a specific position, etc). It seems like I have to re-write code over and over for individual, like objects.

    So for example, say I have multiple buttons and I want some actions to take place for each one that is clicked. Some of those actions may be the same for all (maybe a sound effect, or they open a menu or something) while others would be different (eg the specific action associated with that button).

    Conventionally, I would think do it this way:

    If any button was clicked:

    Determine which button it was;

    Perform some "you clicked any button action"

    Perform some other "you clicked any button action"

    Somewhere later in the code, I could have something like:

    Switch (id of button that was clicked before){

    case A:

    do something specific

    case B:

    do something else

    Case C:

    do something different


    Currently, It seems I have to go about it a different way, repeating nearly all the code for each button:

    If specific button A was clicked:

    Perform some "you clicked any button action"

    Perform some other "you clicked any button action"

    Do something specific

    If specific button B was clicked:

    Perform some "you clicked any button action"

    Perform some other "you clicked any button action"

    Do something else specific.

    If specific button C was clicked:

    Perform some "you clicked any button action"

    Perform some other "you clicked any button action"

    Do something else specifically different.

    (My above example isn't that dramatic, but note the lines of repetitive code)

    of course, I have "cheated" by creating global variables that are set when each button is clicked and then later perform action based on the value of that global variable, but that still feels like a work-around, rather than a proper solution.

    To use an actual example, I recently created a simple kids game where they have to drag words from the right column over pictures on the left. When the word is over the correct picture, something happens. I created the game and it works just fine, but I know I didn't go about it the most "code-efficient" way. (see it in action here:

    I made the "words" from a sprite-sheet with the thought that I could randomize the order of the words on game start. So, I created the word objects as sprites from a sprite sheet, made 6 instances of them and had each start on a different frame - It didn't work. All the buttons started on the same frame.

    I tried giving each button an instance variable and using that as an identifier but couldn't seem to figure out how to select by the instance variable.

    Secondly, when it came to dragging-and-dropping - when I dragged one, I was dragging all of them at the same time because it seems that I could only reference the draggable object by Object Name (not the instance variable).

    So the logic of the game as it stands, is that you drag the word-object on the right and when you you "drop" it within a specified distance from the position of the correct blank-rectangle-object on the left, it performs a bunch of actions (snaps the word-object into position, colors in the graphic, plays a sound effect, adds a point).

    My logic is fine, but I ended up creating multiple clones of the my word-objects, each with a different object name (deleted the instance vars) and multiple clones of the blank-box-objects. (I could have just used X/Y coords. without actual blank-box objects, but didn't think of that until afterwards.) Then I copied and pasted (editing the relative object names accordingly) each section of code that carries out my functions for each one of the words. This does not make for very efficient scalability.

    In other programming environments, I would have probably used some kind of loop, array, If/else or switch statement that would set/select all the variables regarding position, graphic manipulation, and sound file, based on which item I was dragging in the first place and then just fired off the "function" or lines of code that perform the actions using those variables. But I can't seem to do that in the Construct 2 environment.

    If this is a limitation of the Construct 2 environment, so be it, but if I am going about things the wrong way, any help and suggestions would be greatly appreciated!

    Thank you,


  • definitely not a limitation of C2! There are probably great number of ways to achieve what you want, so I thought I would take a quick crack at it...

    I used an array to randomise the order (without getting any repeats). Each answer needs to go to a specific image point on the background image. So, when the player drops an object, it checks how close it is to the desired image point.

    I did it as generic as possible, so it will work with any number of objects - just change the frames of the Objects sprite, and the global variables at the top, and the background image. (You have to set image points on the background image for where the answer boxes are and then use the ImagePosition global variable to tell which Objects frame goes to which image point).

  • Hi AllanR, thank you so much for your help / time!

    What you did is certainly what I am looking for (my EventSheet had 26 "events" on it so yours was, in fact slightly more efficient :-))

    Besides some of your logic being way more efficient / dynamic than mine, if you wouldn't mind, could you let me know if I am understanding this correctly as this is key to what I was asking:

    1. I see you used a FOR loop in the beginning to spawn new instances of the "Objects" in which you set their initial frame, position, comment and image-point instance variables you created. - This give me a much better understanding of how I can utilize instance-variables, thank you! (I wanted to use a loop to do some initial setup on my version, but since I was using multiple objects I couldn't - well, not without giving them object names ending in numbers and doing int->string conversions in my loop... seemed to complicated so I didn't bother).
    2. The "Objects on DragDrop drag start" is picking up any instance of "object."
    3. Then the object is Dropped, you are performing the actions of final positioning of the Object, adjusting the score and displaying the text all based on the object's Instance Variables???

    The last part is what I was the big one I was missing - my question here is: if I were writing Javascript, I would use "this." to reference an Object's properties... Does the script here "capture" (not sure if that's a good word) the "this." of the Object I was dragging just by dragging that specific object? If so, that is exactly what I have been missing. I was trying to figure out how to "tell" the script to use the properties of the specific object that was being clicked / dragged / touched without having to specifically code for that object.

    If I got that right, THANK YOU for enlightening me! (If I didn't, please let me know.)

    (I also didn't even know about the tokenat() function which will also come in handy for future use.)

    Again, thank you very much for taking your time to help. I really appreciate it!


  • glad to help.

    You are correct about the DradDrop events only affecting the instance being dragged. In C2/C3 understanding how instances are "Picked" is a very common stumbling block, but once you get your head around all the nuances, you can do some pretty amazing things!

    You can refer to the picked object as "self" to access the objects properties. Which comes in handy if you have several instances picked, you can say "Set Objects.x to self.X + 50" to make each one move relative to their current position...

    I use instance variables a lot! it keeps all the relevant data with the object. The alternative would be to look up data stored in an array or something, which can work just fine too. If you have multiple objects and want to sort/arrange them, you can put them all in a Family and give them a code stored in an instance variable to make them do what you want - rather than giving them a name with some code tacked on the end (although that can work fine too - just whatever makes the most sense to you).

    The DragDrop events are Triggers, which means they only get processed when an instance triggers those events. I used Touch rather than Mouse so that my example would work just as well on a phone or tablet. On touch devices you get multi-touch, so two people could be dragging around objects at the same time, and the event sheet will only apply the actions to the correct instance selected. Even if two objects were dropped at the exact same time, each one would trigger the Drop code and only apply the actions to the instance that caused the trigger.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Sorry I didn't reply sooner, but I just wanted to say thanks again for your help!

    I made that last "game" for my fiance who is an elementary school teacher doing "distance learning" activities with her students like so many others. I am in the process of making a few more interactive activities for her classes using Construct and what you showed me is making that process a bit easier for me than my first version :-). So thanks for helping educate an (early) middle-aged guy and for helping some little kids learn, too!


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