Sequential Switches Scripting Guide

Discussion in 'Halo and Forge Discussion' started by Psychoduck, Feb 22, 2016.

By Psychoduck on Feb 22, 2016 at 4:46 PM
  1. Psychoduck

    Psychoduck Spartan II
    343 Industries Cartographer Forge Critic Senior Member

    Messages:
    1,528
    Likes Received:
    428
    SEQUENTIAL.jpg

    tutorial-sequence-switches.jpg

    This guide will accompany my latest video tutorial on sequential switches. The topic is somewhat complex, so I wanted to provide supplemental material in text form.


    Purpose:
    Sequential switches allow you to create an "and gate" (one which requires multiple conditions to be met before performing an action) where the required conditions must be completed in a specified order.

    Applications:
    This scripting will be best used in puzzles where a player can be required to complete multiple challenges in a specific order. Mini games and even linear progressive infection maps could make use of this as well. I would recommend placing the items which provide feedback on players' progress through the sequence (the cones, in this case) above the door they are trying to open rather than above the switches themselves. Of course, opening a door does not need to be the end goal. Any action can be attached to the activation of the seventh power channel which is the result of completing the sequence.

    Limitations:
    For this setup, I have used three ordered switches. The scripting can be extended to include additional switches, but the amount of scripts required to do so will grow exponentially with each additional switch. For a series of three, approximately 35 scripts are required.

    Logic:
    I've laid out all of the logic at work here below. You can see the exact scripts this has translated into in the next section, but this should provide a clearer picture of exactly how this works using simple if, then statements. First thing's first, we need to set each of our first three power channels off. Channels begin in a null state, but logic further down the chain will require them to start in the off state. From here, interacting with each switch should turn its respective channel on.

    If: match start, then: alpha off
    If: match start, then: bravo off
    If: match start, then: charlie off

    If: interact with switch 1, then: alpha on
    If: interact with switch 2, then: bravo on
    If: interact with switch 3, then: charlie on
    Next, we need to walk through the sequence and determine what should happen when each switch is turned on at the correct time. We'll step through it in order and this will affect our secondary group of power channels. Once these channels are all on, meaning the sequence is complete, we'll turn on our last power channel which will activate the outcome of the whole sequence (opening the door, in this case).

    If: alpha on, and: bravo off, and: charlie off, then: delta on
    If: alpha on, and: bravo on, and: charlie off, then: echo on
    If: alpha on, and: bravo on, and: charlie on, then: foxtrot on

    If: delta on, and: echo on, and: foxtrot on, then: golf on
    Now we've got the outcome for the sequence being completed properly down. We need to reset everything if any switch is flipped out of order. We need to account for every possible way the switches could be flipped in the wrong order.

    If: alpha off, and: bravo on, then: delta off
    If: alpha off, and: charlie on, then: echo off
    If: bravo off, and: charlie on, then: foxtrot off
    If: delta off, and: echo on, then: echo off
    If: echo off, and: foxtrot on, then: foxtrot off
    If: delta off, then alpha off, and: bravo off, and: charlie off
    If: echo off, then alpha off, and: bravo off, and: charlie off
    If: foxtrot off, then alpha off, and: bravo off, and: charlie off​

    The last thing we need is feedback for the player. In this case, we're associating a cone with each switch and changing its color upon activation. If these three objects are physically placed in the right order, they can guide players through the sequence and show their progress.

    If: alpha on, then: color change green on object 1
    If: alpha off, then: color change orange on object 1
    If: bravo on, then: color change green on object 2
    If: bravo off, then: color change orange on object 2
    If: charlie on, then: color change green on object 3
    If: charlie off, then: color change orange on object 3
    Scripts:
    You can see all the scripts this logic translates into below. With the exceptions of the interaction with the switches and the color changing on the cones, everything is crammed into three script brains. It's a bit jumbled and harder to follow here which is why I laid everything out above.

    Switches:
    sequential-switches-1.jpg
    Brain 1:
    sequential-switches-2.jpg
    Brain 2:
    sequential-switches-3.jpg
    Brain 3:
    sequential-switches-4.jpg
    And there you have it! Feel free to download Sequential Switches from my files to take a look in forge if that helps you. I hope some of you find this useful or at least interesting, and I look forward to seeing what you come up with!
     
    #1 Psychoduck, Feb 22, 2016
    Last edited: Feb 23, 2016
Tags: this article has not been tagged

Comments

Discussion in 'Halo and Forge Discussion' started by Psychoduck, Feb 22, 2016.

    1. Max Extra
      Max Extra
      feels like my digital electronics class all over agian good stuff! you can do all sorts of things with these state machines.
    2. Architex
      Architex
      I know what you mean, it's amazing how much the logic design courses I've taken have helped with all this scripting.
      Last edited: Feb 24, 2016
      Yumudas Beegbut likes this.
    3. Pat Sounds
      Pat Sounds
      Thanks for putting this together! I definitely needed help in this area.
    4. buddhacrane
      buddhacrane
      Uh oh, here comes Buddha "The Optimiser" Crane again (Who keeps letting this guy in here?!).

      I'm sorry, but I just can't resist lol. This can be done in 19 Scripts, using 5 channels; and you can have more switches, without destroying your Scripting budget (a 4 sequence switch can be done with 25 Scripts, and 6 channels). Also, on Duck's demo map, hitting switch Bravo followed by Alpha resets the Switches early (it doesn't wait for you to hit Charlie and complete the sequence).

      I've whipped up a demo map to check that my method works, and I'll post a detailed explanation tomorrow after I get back from work (honest, I will, I promise. I've already built the map, I just need to take some pretty screenshots).


      Edit: OK, today is now tomorrow. Don't let your dreams be dreams!

      Firstly, I've modified Duck's "Sequential Switches" map - adding my optimisations, and turning it into a 4-sequence switch instead of a 3-sequence switch; so you can access that on my FileShare (GT: BuddhaCrane) if you feel so inclined - I've called the map "Script: Ordered Switches".

      Note that I'll be explaining this as a 4-sequence switch, instead of a 3-sequence switch. I'm also not going to cover the scripts that provide feedback via the cones because, well, they're the same as Duck's... I'm only going to show the scripting for the logic - the meat!

      I've split this into 3 chunks of logic:

      Simple enough this one, you have 4 switches, and each one has a single "On Interaction: Power On", using channels Alpha through to Delta.

      Script 1: Activates Switch 1
      Condition (OnInteraction)
      Action [PowerSet: Alpha: On]

      Script 2: Activates Switch 2
      Condition (OnInteraction)
      Action [PowerSet: Bravo: On]

      Script 3: Activates Switch 3
      Condition (OnInteraction)
      Action [PowerSet: Charlie: On]

      Script 4: Activates Switch 4
      Condition (OnInteraction)
      Action [PowerSet: Delta: On]

      First of all, what is the correct sequential order? In my example I'm saying that the correct order is to turn on Alpha, then Bravo, then Charlie, and finally Delta. If you turn on the channels in that order, via the switches, then you've activated the sequence correctly. Any other combination of turning on those 4 channels is no good, sir! So now to script that logic...

      For this we need to create 4 Scripts.

      Script 1: When the 2nd switch is activated, while the 1st switch is off, flag that we've hit a switch out of order (by turning on Power Channel Echo)
      Condition (OnMulti: Minimum=3) (OnPower: Alpha: Off) (OnPower: Bravo: Toggle) (OnPower: Bravo: On)
      Action [PowerSet: Echo: On]

      Script 2: When the 3rd switch is activated, while the 2nd switch is off, flag that we've hit a switch out of order
      Condition (OnMulti: Minimum=3) (OnPower: Bravo: Off) (OnPower: Charlie: Toggle) (OnPower: Charlie: On)
      Action [PowerSet: Echo: On]

      Script 3: When the 4th switch is activated, while the 3rd switch is off, flag that we've hit a switch out of order
      Condition (OnMulti: Minimum=3) (OnPower: Charlie: Off) (OnPower: Delta: Toggle) (OnPower: Delta: On)
      Action [PowerSet: Echo: On]

      Script 4: When the 4th switch is activated, while no failures have been flagged, and while the 3rd switch is on, flag that all 4 switches have been activated in the correct order (by turning on Power Channel Foxtrot)
      Condition (OnMulti: Minimum=4) (OnPower: Echo: Off) (OnPower: Charlie: On) (OnPower: Delta: Toggle) (OnPower: Delta: On)
      Action [PowerSet: Foxtrot: On]

      The "trick" here is that we're using the "Power: Toggle" condition within the Multi condition, to make sure we only evaluate these scripts during the moment that the switches are activated - this is why, for instance, Script 1's conditions are only met when Bravo is turned on 1st and then Alpha, but not when Alpha is turned on 1st and then Bravo.

      The actual logic here is that I'm checking to see if, when a switch has been hit, whether it's been hit in the wrong order. If it has been hit in the wrong order then flag it. This means that channel Echo will only remain off while the switches are being hit in precisely the correct order.

      The 4th Script is our "win" condition. When activating our 4th switch, if we've not had any failures so far (i.e. Echo is still Off) and we've hit this switch in the right order (i.e. Charlie is On), then flag that we've hit everything in the right sequence by turning on Foxtrot. Channel Foxtrot is then what you use to, say, make a door open or something when you get the sequence right.

      When the player hits all 4 switches, we need to reset them all back to off again - mainly so that they can try the sequence again if they got it wrong. That's what this final bit of logic is for.

      Script 1: Once all switches have been activated, trigger a reset (by sending a message on Alpha)
      Condition (OnMulti: Minimum=4) (OnPower: Alpha: On) (OnPower: Bravo: On) (OnPower: Charlie: On) (OnPower: Delta: On)
      Action [Message: Alpha]

      Script 2: Reset Switch 1
      Condition (OnMessage: Alpha)
      Action [PowerSet: Alpha: Off]

      Script 3: Reset Switch 2
      Condition (OnMessage: Alpha)
      Action [PowerSet: Bravo: Off]

      Script 4: Reset Switch 3
      Condition (OnMessage: Alpha)
      Action [PowerSet: Charlie: Off]

      Script 5: Reset Switch 4
      Condition (OnMessage: Alpha)
      Action [PowerSet: Delta: Off]

      Script 6: Reset Failure Flag
      Condition (OnMessage: Alpha)
      Action [PowerSet: Echo: Off]


      You'll notice that I've used a message channel of Alpha, even though I'm already using a power channel of Alpha; normally you should be wary of sharing channels like this, but in this case it's all good - you'll just have to trust me on this :p.


      Note: If you need your switches to reset during a round change, then you can just add a Script that sends a message to Alpha (kicking off the Reset behaviour) on Round Start.

      Script 8 (Optional):
      Trigger Reset on Round Change
      Condition (OnRoundStart)
      Action [Message: Alpha]


      So there you have it! A more optimised version of creating Sequential Switches, which will allow you to create higher sequences without breaking your Scripting Budget. Which, to clarify, means that it scales linearly as you add higher sequences - rather than exponentially.
      Last edited: Mar 4, 2016
      Yumudas Beegbut likes this.
    5. Yumudas Beegbut
      Yumudas Beegbut
      Darn, I resisted longer than Buddha, lol. I should just back away slowly. I thought it would take far less scripts, but I can't quite wrap my limping brain around it well enough to come up with a solid plan.

      Still, this is really good work by pducky (let me know if I should never ever call you that again). It's thorough and the explanation and spreadsheet images are clear. I might switch over to using the spreadsheet thing.

      Make sure to use the OR function of Multi too. Brain 1 scripts 4-6 and Brain 2 scripts 1-6 can be combined into three scripts using Multi: Minimum=1.

      That was me on YouTube saying I thought that the power states start off. Or maybe they just effectively act as though they're off when they have a null value. I tested and found:
      1. Toggling them from 1st thing turns then on
      2. Multi conditions testing if they're off work even if they haven't been set to anything
      3. Turning them off 1st thing doesn't trigger (On Power: Off) Conditions. They have to be turned on then off.
      4. Still, having a reset between rounds is the right way to do it.
      These are just minor things. Not trying to be an ass. Holy crap, I just had to add "ass" to my phone. I think I underestimated what it would take to do sequences & I'm kinda glad Psychoduck plowed through it so I don't have to. Can't wait to see what buddhacrane comes up with. Just made my phone learn "Psychoduck" and "buddhacrane". You guys come right after "ass".
    6. ♥ Sky
      ♥ Sky
      I also have a method for a switch sequence, with a slightly different method and it also behaves in a different way, where getting a selection wrong in the sequence forces a restart. This means that it is slightly easier to figure out in the long run, but it is also much more linear in terms of scalability, as I will explain.

      Instead of using scripting brains and extra channels, instead I have decided to use what I call the "dummy switch" method, where the switches themselves are spawned and despawned when power states are turned on and off.

      In total, for a 4 switch sequence including a moving doorway and a reset button, I have used 27 scripts, 4 channels and 8 switch objects. The map demo also includes lights that change colour when the right switch is pressed, bumping the used scripts to 35 (2 per light, 4 lights)

      On to the meat and gravy of it all.

      To start with, there must be 1 "correct" switch and 3 "dummy" switches available for interaction. When a player interacts with the correct switch, it will despawn the first dummy switch and replace it with the second correct switch. When the second correct switch is pressed, it will despawn the second dummy switch and spawn the third correct switch. Finally, when the third correct switch is pressed, it will despawn the final left over dummy switch and spawn the final correct switch, which when interacted with will complete the sequence and in the case of the demo map, open the green door to the cinema, so you can all go and watch Deadpool for the 12th time. (Side note, if you've not seen Deadpool, I highly recommend it.) If at any point, a dummy switch is interacted with, all correct switches, aside from the first switch are despawned and replaced with their dummy counterparts again. The reset button is only needed once the sequence has been completed to put all the dummy switches back and close the door again. If the sequence is intended to be single use only this switch can be ignored, saving 1 switch object and 3 scripts.

      So, that's the theory, here's that put into practise.

      Switch 1:
      On Interaction
      Power Set
      Alpha: ON

      Switch 2:
      On Match Start
      Despawn
      --
      On Power Set
      Alpha: ON
      Spawn
      --
      On Power Set
      Alpha: OFF
      Despawn
      --
      On Interaction
      Power Set
      Bravo: ON

      Switch 3:
      On Match Start
      Despawn
      --
      On Power Set
      Bravo: ON
      Spawn
      --
      On Power Set
      Bravo: OFF
      Despawn
      --
      On Interaction
      Power Set
      Charlie: ON

      Switch 4:
      On Match Start
      Despawn
      --
      On Power Set
      Charlie: ON
      Spawn
      --
      On Power Set
      Charlie: OFF
      Despawn
      --
      On Interaction
      Power Set
      Delta: ON

      Dummy 1:
      On Power Set
      Alpha: ON
      Despawn
      --
      On Power Set
      Alpha: OFF
      Spawn

      Dummy 2:
      On Power Set
      Bravo: ON
      Despawn
      --
      On Power Set
      Bravo: OFF
      Spawn
      --
      On Interaction
      Power Set
      Alpha: OFF
      --
      On Spawn
      Power Set
      Alpha: OFF

      Dummy 3:
      On Power Set
      Charlie: ON
      Despawn
      --
      On Power Set
      Charlie: OFF
      Spawn
      --
      On Interaction
      Power Set
      Bravo: OFF
      --
      On Spawn
      Power Set
      Bravo: OFF

      Reset Switch:
      On Interaction
      Power Set
      Delta: OFF
      --
      On Interaction
      Power Set
      Charlie: OFF

      Door:
      On Power Set
      Delta: ON
      Move Up*
      --
      On Power Set
      Delta: OFF
      Move Down/Reset Position*

      *Can be any kind of action in the world, not limited to the door, only used as an example. The despawn script is designed for doors that open and close.

      To review what the scripting above shows:
      • When switch 1 is pressed, alpha channel is turned on, despawning dummy 1 and spawning switch 2
      • When switch 2 is pressed, bravo channel is turned on, despawning dummy 2 and spawning switch 3
      • When switch 3 is pressed, charlie channel is turned on, despawning dummy 3 and spawning switch 4
      • When switch 4 is pressed, delta channel is turned on, opening the door
      • When dummy 1 is pressed, nothing happens as no channels will be on when this dummy is available to be interacted with
      • When dummy 2 is pressed, alpha channel is turned off, spawning dummy 1 and despawning switch 2
      • When dummy 3 is pressed, bravo channel is turned off, despawning switch 3 andspawning dummy 2, which turns channel alpha off and spawns dummy 1 and despawns switch 2
      • When the reset switch is pressed, delta and charlie channels are turned off, closing the door, despawning switches 2, 3 & 4, and spawning dummy 1, 2 & 3
      This method of scripting is also linear in terms of adding more switches to the sequence, only requiring 1 more channel, 2 more switches and 8 scripts for each extra step in the sequence. Below is an example of how to set up a 5th switch or the sequence.

      Switch 5:
      On Match Start
      Despawn
      --
      On Power Set
      Delta: ON
      Spawn
      --
      On Power Set
      Delta: OFF
      Despawn
      --
      On Interaction
      Power Set
      Echo: ON

      Dummy 4:
      On Power Set
      Delta: ON
      Despawn
      --
      On Power Set
      Delta: OFF
      Spawn
      --
      On Interaction
      Power Set
      Charlie: OFF
      --
      On Spawn
      Power Set
      Charlie: OFF

      Reset Switch:
      On Interaction
      Power Set
      Echo: OFF
      --
      On Interaction
      Power Set
      Delta: OFF

      Door:
      On Power Set
      Echo: ON
      Move Up*
      --
      On Power Set
      Echo: OFF
      Move Down/Reset Position*

      *Can be any kind of action in the world, not limited to the door, only used as an example. The despawn script is designed for doors that open and close.

      All this does is add an extra step to the sequence by moving the door to echo channel, and having the 5th switch and 4th dummy respond to delta being powered.


      So there you have it, yet another method of having a sequence of switches open a door.

      A map demo called "SeqTest" can be found in my Fileshare/bookmarks, under my Gamertag: SkybladePhoenix


      EDIT:
      I may have found an issue with my scripting, where if the 1st and second switches are active, pressing the 3rd dummy switch may not cause a reset like it should. Investigation into this will happen tonight/tomorrow and an update will be posted if required.
      Last edited: Mar 4, 2016
    7. buddhacrane
      buddhacrane
      Ah, well in that case ... :p

      Here's a more optimised way of creating a switch sequence that forces a restart as soon as a wrong switch is pressed - same behaviour as Sky's method. This uses 4 Channels, 15 Scripts (not including "feedback cones" - 23 if including feedback), and 4 Switches to create a 4 Sequence switch.

      This is less complicated than my other version because you no longer need to hold onto a flag indicating whether the switches have been activated in the right order. Instead, you just reset as soon as you detect a switch being hit in the wrong order.

      3 chunks of logic again:

      Slight twist on this one, all switches activate with a single "On Interaction: Power On", except for the last one in the sequence; which uses a Message channel instead (this will allow us to re-purpose that same channel later - it'll all make sense, trust me).

      Script 1: Activates Switch 1
      Condition (OnInteraction)
      Action [PowerSet: Alpha: On]

      Script 2: Activates Switch 2
      Condition (OnInteraction)
      Action [PowerSet: Bravo: On]

      Script 3: Activates Switch 3
      Condition (OnInteraction)
      Action [PowerSet: Charlie: On]

      Script 4: Activates Switch 4
      Condition (OnInteraction)
      Action [Message: Delta]

      Again, the correct order is to turn on Alpha, then Bravo, then Charlie, and finally Delta. If you turn on the channels in that order, via the switches, then you've activated the sequence correctly. As soon as you hit the wrong switch though, you have to start over.

      For this we need to create 4 Scripts.

      Script 1: When the 2nd switch is activated, if the 1st switch is off, force a reset (by sending a message on Alpha)
      Condition (OnMulti: Minimum=3) (OnPower: Alpha: Off) (OnPower: Bravo: Toggle) (OnPower: Bravo: On)
      Action [Message: Alpha]

      Script 2: When the 3rd switch is activated, if the 2nd switch is off, force a reset
      Condition (OnMulti: Minimum=3) (OnPower: Bravo: Off) (OnPower: Charlie: Toggle) (OnPower: Charlie: On)
      Action [Message: Alpha]

      Script 3: When the 4th switch is activated, if the 3rd switch is off (and the sequence isn't yet complete), force a reset
      Condition (OnMulti: Minimum=3) (OnPower: Charlie: Off) (OnMessage: Delta) (OnPower: Delta: Off)
      Action [Message: Alpha]

      Script 4: When the 4th switch is activated, if the 3rd switch is on, flag that all 4 switches have been turned on in the correct order (by turning on Power Channel Delta)
      Condition (OnMulti: Minimum=2) (OnPower: Charlie: On) (OnMessage: Delta)
      Action [PowerSet: Delta: On]

      Power Channel Delta acts as our flag as to whether the sequence has been hit correctly; therefore you can hook into its On/Off state with whatever you want to have happen when the sequence is correct. This is also why the 4th switch is using Delta as a Message channel only - and no, in this setup they won't interfere with each other.

      Whenever the player hits a switch out of sequence, we need to reset any active switches. The reset "Action" is done with these scripts.

      Script 1: Reset Switch 1
      Condition (OnMessage: Alpha)
      Action [PowerSet: Alpha: Off]

      Script 2: Reset Switch 2
      Condition (OnMessage: Alpha)
      Action [PowerSet: Bravo: Off]

      Script 3: Reset Switch 3
      Condition (OnMessage: Alpha)
      Action [PowerSet: Charlie: Off]


      Additionally, if you want to reset the whole thing after hitting all the switches correctly, then setup a switch like so:

      Script 1: Reset the "flag" that we've completed the sequence correctly
      Condition (OnInteraction)
      Action [PowerSet: Delta: Off]

      Script 2: Activate Reset Action
      Condition (OnInteraction)
      Action [Message: Alpha]

      If you want to add more switches to the sequence, then each extra switch only requires 1 extra Switch, 1 extra Channel, and 3 extra Scripts.

      Demo map on my fileshare (GT: BuddhaCrane) called "Script: Ordrd Switches 2".


      EDIT: And I've just realised I can apply this same logic to the other version, and save a channel (editing previous post lol)
      Last edited: Mar 4, 2016
      Sn1p3r C likes this.
    8. Psychoduck
      Psychoduck
      Aw, you guys. Taking my ideas and making them better. :D

      Seriously, I love how much collaboration has come out of scripting in this game. First with multiple-round timers, now with this. Every time someone comes up with a new method for doing something, everyone else just rolls with it and builds off it. It's really cool to see.
      Yumudas Beegbut likes this.
    9. buddhacrane
      buddhacrane
      Teeeeeeeeeeeeeechnically I already created one of these back in MCC (my Rebirth puzzle map had one of these). These are just revamps of that concept using Halo 5's Scripting mechanics. :p. And in fact, my MCC version allowed you to hit the same switch more than once as part of the sequence; I've not even shown an example of how you could do that with Halo 5's Scripting mechanics yet (and I don't intend to - I'm done showing multiple ways of achieving the same thing now lol)

      But yes, I totally agree. Collaborating/improving/and building upon our ideas is really great - especially when it comes to the Scripting; it's a very iterative process, just like building real software (no one creates a version 1 of their software and then considers it perfect and complete!)

      And on that note, I just edited my first post because I realised that I could tweak it (using the method I created in my second post) to make it more optimised and scaleable. I'm going to go ahead and call that my definitive version now (until I make a V3 lol)
      Yumudas Beegbut likes this.

Share This Page