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: http://jasonlipset.com/testmatch)
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,
J