Specifying object instances.

For questions about using Classic.

Post » Wed Jun 20, 2012 4:54 pm

Is there a way to make one instance of an object control a specific instance of another? Like a switch that opens a door. Or do I have to make each switch and door a separate object with separate events?

Thank you.
Posts: 18
Reputation: 336

Post » Wed Jun 20, 2012 5:23 pm

Apart from the family trick (that I never really got^^) you can always work with variables:

+ MouseKeyboard: Mouse is over Sprite
+ System: Sprite ('pid') Equal to 2
-> System: Set global variable 'toOpen' to 1

+ System: Is global variable 'toOpen' Different to 0
++ Sprite: Value 'pid' Equal to global('toOpen')
--> Sprite: do whatever needed to open the door
++ empty condition
--> System: Set global variable 'toOpen' to 0

One sprite with several instances. They all share a private variable named "pid", which is set to 1 for the first instance, 2 for the second, and so on. If you setup the pid wisely, you can simplify the first event. For example you could give all switches the lower numbers, and all doors numbers + 10 (or whatever many doors you have) So a switch with pid 2 belongs to door with pid 12, 3 belongs to 13, etc.
Then the first event would read:

+ MouseKeyboard: Mouse is over Sprite
+ System: Sprite ('pid') Lower or equal 10
-> System: Set global variable 'toOpen' to Sprite ('pid') + 10
Posts: 1,821
Reputation: 8,279

Post » Wed Jun 20, 2012 5:27 pm

You can have private variables for the Switch and Door objects (one of each, then place lots of them in the layouts) to pair them.

Then compare this in the events Eg:
For each Door
-Subevent> Switch value "DoorNum" = Door.value("SwitchNum")

You can then use different animation frames for different door and switch images and change this in their properties, or use a variable again to set it in-game.

Edit: Ah, just saw Tulamide's post. Nice solution!

Double edit: Oops, I just realized containers and an "On" variable on the Switch will probably work here too! That's probably the easiest method, just have Switch in the container of Door and then:

For each Door (or Switch, shouldn't matter which is looped through)
-Subevent> Switch value "On" = 1, open door
-Subevent> Switch value "On" = -1, close door

Then, when the player uses a switch, just change its value of "On" to "On * -1"Jayjay2012-06-20 17:36:32
"Construct 4 lets YOU make advanced games! (but not play them)" Construct Classic - Examples Kit
Posts: 2,120
Reputation: 16,493

Post » Sat Jun 23, 2012 7:39 pm

Thanks a lot for the tips guys, I it goin now.
Posts: 18
Reputation: 336

Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 0 guests