0 Favourites

How do I fix my switch toggling system?

  • 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!

  • 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

  • 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.

  • Hi is this close to what you're after?

    Edit: slight change to capx

    [attachment=1:2tb70m1r][/attachment:2tb70m1r]

    without for loop

    [attachment=0:2tb70m1r][/attachment:2tb70m1r]

    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.

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • Hi is this close to what you're after?

    Edit: slight change to capx

    [attachment=1:lyho8d1d][/attachment:lyho8d1d]

    without for loop

    [attachment=0:lyho8d1d][/attachment:lyho8d1d]

    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.

  • 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..

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)
Similar Topics Posts Views Last Post
Unread hot topic
234 61,232
DARKSETH's avatar
DARKSETH
Unread hot topic
175 32,641
clifton11221's avatar
clifton11221
Unread hot topic
152 19,233
Magistross's avatar
Magistross