Make a momentary switch/ pressure plate tile [4.5.9]

SciNEStist

Well-known member
This Tutorial will teach you how to make a barrier that only disappears when an object is touching a tile. as soon as there are no objects touching the tile again, the barrier will go back up. Could be a fun way to add puzzle elements to your game, or with a bit of modification, you could reproduce the bridge at the end of the castle levels in SMB1 that drops Koopa into the lava.

The button doesn't necessarily have to toggle a barrier either, you can take the principals of this code and re-purpose it to do anything you want when stepping on and off a tile.

step 1: add a user variable "button" in your project setttings window, user variables tab. it's initial value should be 0

step 2: add the change_all_tiles macro written by pigeonaut HERE

step 3: you will need to draw 4 tiles. your button in the off and on positions, and your barrier in the off and on positions. you will need to know the tile # of where you are putting these tile graphics for later

step 4: set up your Button tile

copy this code and save it as "button.asm", save it somewhere inside your GameEngineData\Routines\BASE_4_5\Game\TileScripts folder (or anywhere you want as long as it's in the gameengine folder)

Code:
LDA button
ORA #%00000001
STA button ; sets the flag that an object is touching the button
RTS

afterwards, go to your project settings, script settings tab, then game>Tiletypes and pick what tile # you want to be the button, point it to your button.asm file (do not replace any important tile #s, like 0 or 1, use an unused tile #)

step 5: add your barrier tile
same as set 4, but with new code.
copy this code and save it as "barrier.asm", save it somewhere inside your GameEngineData\Routines\BASE_4_5\Game\TileScripts folder (or anywhere you want as long as it's in the gameengine folder)

Code:
LDA button
AND #%00000010 ; checks if the barrier is toggled off
BNE +
    LDA ObjectUpdateByte
    ORA #%00000001
    STA ObjectUpdateByte ;; makes solid only if barrier is on
+
RTS

afterwards, go to your project settings, script settings tab, then game>Tiletypes and pick what tile # you want to be the barrier, point it to your barrier.asm file (do not replace any important tile #s, like 0 or 1, use an unused tile #)

step 6: Add the code that toggles everything
Put this code into scripts>defined scripts> then game> sprite post-draw (usually dospritepostdraw.asm)

Code:
LDA button
AND #%00000001
BNE +
    JMP +nottouching
+
    ;the button is being touched now
    LDA button
    AND #%00000010
    BEQ +nottoggled
        ;the button is touched, and the barrier is down
        ;DO NOTHING
        JMP +donebutton
    +nottoggled
        ;the button is touched, but the barrier isnt up yet
        LDA button
        ORA #%00000010
        STA button
;;;MODIFY THE NEXT 2 LINES!!!!!!
        change_all_tiles #$0A, #$AE, #$0A;toggles the barrier off (args should be barrier tile type, off barrier tile graphic #, barrier tile type again)
        change_all_tiles #$08, #$68, #$08;toggles the button pressed (args should be button tile type, pressed button tile graphic #, button tile type again)
        JMP +donebutton
+nottouching
    LDA button
    AND #%00000010
    BNE +
        JMP +nottoggled
    +
        ;the button is untouched, but the barrier is still down
        LDA button
        AND #%11111101
        STA button
;;;MODIFY THE NEXT 2 LINES!!!!!!
        change_all_tiles #$0A, #$0A, #$0A ; toggles your barrier on (args should be barrier tile type, on barrier tile graphic #, barrier tile type again)
        change_all_tiles #$08, #$62, #$08 ; toggles your button released (args should be button tile type, unpressed button tile graphic #, button tile type again)
        JMP +donebutton
    +nottoggled
        ;the button is not touched and the barrier is down
        ;DO NOTHING

+donebutton ;this part resets the flag
LDA button
AND #%11111110
STA button

you are not done yet though. all 4 of the "change_all_tiles" needs to be modified

change_all_tiles arg0, arg1, arg2
; arg0 = the tile type that you want to replace
; arg1 = metatile to change into (graphic)
; arg2 = collision to change into (tile type)

For our purpose, arg0 and arg2 will match, since we are not changing what type of tile they are. set them to your button and barrier tile types (the collision type)
in my example, tile 08 was the button and tile 10 (#$0A) is the barrier
arg1 will be the tile graphics, so either your barrier or button tile graphics in their off and on positions

STEP 7: Paint your screen.
by defaulet the button will be unpressed and the barrier up, so use those graphics, then set your tile collision types.

your done!

the way this script works is that the button tile toggles a flag (the first bit of the user variable "button") whenever an object is touching it.
then after all the tile collision code (in the post draw script) , we check if that flag is set. if it is, then we know at least 1 object is touching the button. the script then checks the 2nd bit of our "button" variable to see if the barrier is toggled on or off. if the bits don't match, then it executes the code to change the barrier on or off to make them match.

after doing all the checks it resets the flag to 0, so unless there is something touching the button to change it back to 1, next time it loops through it will know nothing is touching it. if nothing is touching it and the barrier is up, then it will execute the code to toggle the barrier off again to make sure they match again.
 

nesker

New member
for anyone using the change_all_tiles marco and noticing sudden crashes, at the start of the macro add:

Code:
TXA 
PHA 
TYA 
PHA

and at the very end of the macro add

Code:
PLA 
TAY 
PLA 
TAX

The reason for this is if you are calling this macro when you are already running a loop using the x or y registry, this macro overrides it. by temporarily storing the x and y registry, you can call it back again at the end of the macro and continue on your way

edit: updated thanks to a suggestion by ClutterCross
 
Last edited:
Top Bottom