How do I fix my switch toggling system?

Get help using Construct 2

Post » Tue Aug 12, 2014 5:53 pm

Hey all,

I'm having a bit of trouble figuring out the logic for a little pressure switch system I've put into my game. The toggling works fine if only one switch is being stepped on at a time, but as I've started to implement extra characters etc, the results start to get a little crazy.

I've tried to comment the capx as clearly as possible, so hopefully you can get the gist of what I'm trying to achieve.

https://www.dropbox.com/s/v2dnvwv2jc1qo89/DoorTest.capx

Extra instances of objects seem to break it the most, namely the spawnable blocks. But if both players are standing on both switches, it also seems to wig out. I've also tried using families but couldn't seem to make a difference. I have a feeling I'm either using too many/not enough For Each loops, that's usually what trips me up with these kinds of things.

Any help is much appreciated!
B
6
S
2
G
1
Posts: 10
Reputation: 905

Post » Tue Aug 12, 2014 8:07 pm

why not use a boolean variable and toggle it when over the switch?
would probably be even easier when putting the doors in a container with the switch, but for now instead of the "for each door system compare" you should use system pick by comparison door : door.doorID = switch.switchID
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Tue Aug 12, 2014 8:43 pm

LittleStain wrote:why not use a boolean variable and toggle it when over the switch?
would probably be even easier when putting the doors in a container with the switch, but for now instead of the "for each door system compare" you should use system pick by comparison door : door.doorID = switch.switchID

Hmm, I've changed the for loops to the system compare but it still seems to be acting the same. I believe I tried a boolean method in an earlier attempt but it didn't seem to make much difference. I mean it's still switching between two states as it is.

Just noticed that if you create a block on top of a switch it will toggle it, but when destroyed the switch and door will not return to their original state. They just don't seem to work properly at all. Maybe something in the "not overlapping" events is not triggering, or I have it coded completely wrong? What's odd to me is that it works with the player characters one at a time, but breaks if two switches are pressed simultaneously.
B
6
S
2
G
1
Posts: 10
Reputation: 905

Post » Tue Aug 12, 2014 10:32 pm

Hi is this close to what you're after?

Edit: slight change to capx
doortest_codah.capx


without for loop
doortest_no_for_codah.capx


Edit: Comments.. Any reason the block wasn't in the 'heavy' family? And also why weren't you using the family instead of individual objects in a big 'or' block? The comment about 'each door's starting state' is a bit misleading because those events are continually evaluated, not just at the start.

Basically I started from scratch as the code looked way too complicated. I also used a boolean 'state' variable instead of the text one (personal preference, less error prone).

I keep coming back to one general design principal: when a change of state occurs, toggle a variable, then 'do all the state change stuff elsewhere'; i.e. in an event that checks for the state variable's value. Don't do things at the point where the state changes. It keeps things simple, clean and readable.
You do not have the required permissions to view the files attached to this post.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Tue Aug 12, 2014 11:16 pm

codah wrote:Hi is this close to what you're after?

Edit: slight change to capx
doortest_codah.capx


without for loop
doortest_no_for_codah.capx

Perfect, many thanks!

I actually had another variable on the door to determine whether it's default state was "open" or closed" at the start of layout, but I've added that in myself and it works just fine.

So it seems changing the toggle state to the switch instead of the door fixed it? Can you give any insight as to what was going wrong with my first attempt? Apart from all the unnecessary for loops ;)

EDIT: Just read your edit. I had gone through a previous attempt with families but thought that might have been clashing with something and reverted back to the individuals. Honestly I had gotten to the point where I wasn't sure which part was wrong and whether each change I made was breaking something else. I now realise I was over complicating things.
B
6
S
2
G
1
Posts: 10
Reputation: 905

Post » Wed Aug 13, 2014 12:43 am

ProceedToMemory wrote:
I actually had another variable on the door to determine whether it's default state was "open" or closed" at the start of layout, but I've added that in myself and it works just fine.

So it seems changing the toggle state to the switch instead of the door fixed it? Can you give any insight as to what was going wrong with my first attempt? Apart from all the unnecessary for loops ;)

EDIT: Just read your edit. I had gone through a previous attempt with families but thought that might have been clashing with something and reverted back to the individuals. Honestly I had gotten to the point where I wasn't sure which part was wrong and whether each change I made was breaking something else. I now realise I was over complicating things.


Yes I often start from scratch when I've 'tried' too many things as they all start to conflict with each other. Definitely the KISS principle applies to C2. And families can be a big help, but there are some gotchas as well. At first I thought you had the free version because your avatar doesn't show it (so I couldn't suggest families) but then I saw you already had one..
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596


Return to How do I....?

Who is online

Users browsing this forum: Baidu [Spider], Yahoo [Bot] and 26 guests