Accurate YI Switch Block Behavior (Fix included)

Post here for help and support regarding LunaLua and SMBX2's libraries and features.
dochalkos
Shy Guy
Shy Guy
Posts: 5
Joined: Sat Nov 26, 2022 8:49 pm

Accurate YI Switch Block Behavior (Fix included)

Postby dochalkos » Mon Nov 28, 2022 6:30 am

The YI Switch blocks in SMBX2 work on a P-switch-activated toggle, when their behavior from the original game has them set to an invisible timer (like the effects of a traditional P-switch). A similar thread on the subject was made a while back explaining the same thing:
viewtopic.php?t=27548

For anyone similar looking for a solution, check deice's post below.
Include his script in a custom lua file, then drop it in "levelname"/blocks/ai, where "levelname" is the folder for the level you want to use the blocks in.
Make two custom blocks with adjacent ID numbers, then copy the code from block-689.lua & block-690.lua into them. Copy the sprites for the blocks too, rename them to the IDs of your custom blocks, and everything should work fine.

Old topic:
I don't know much about lua or coding but I figured a fix for this behavior wouldn't be too hard. The scripts for the relevant blocks both seem to only have one line in them of note.

block-689.lua

Code: Select all

pswitch.registerSet(blockID + 1, blockID)
return block
block-690.lua

Code: Select all

pswitch.registerSet(blockID - 1, blockID)
return block
I gather it means that when a p-switch (or another object that uses p-switch behavior) is activated, the blocks just increment their IDs so that they change into the other.

I tried looking for other p-switch related code in different scripts, but most of the new features in SMBX2 that revolve around p-switch use are toggle-based instead of timer-based. What should I be doing here, is this something easy to change or a bit more complicated? Could someone give me an example of code that uses the typical timer-based P-switch behavior?
Last edited by dochalkos on Mon Nov 28, 2022 3:49 pm, edited 1 time in total.

deice
Volcano Lotus
Volcano Lotus
Posts: 538
Joined: Fri Jul 23, 2021 7:35 am

Re: YI Switch Block Behavior Change

Postby deice » Mon Nov 28, 2022 9:40 am

i had actually looked into this a bit more some time ago but must've forgotten to post it in that thread you linked.
the permanent toggle of the yoshi's island switch blocks is indeed not intended behavior. namely, the "pswitchable" ai script fails to account for a vanilla smbx bug where the "P Switch - End" event triggers twice.

this will likely be patched in a future version, but in the meantime it's possible to circumvent this by using a custom ai script:

Code: Select all

local pswitchable = {}
local blockutils = require("blocks/blockutils")

local sets = {}
local registeredIDs = {}

local psState = false

function pswitchable.registerSet(id1, id2)
    if registeredIDs[id1] or registeredIDs[id2] then return end
    table.insert(sets, {id1, id2})
    registeredIDs[id1] = true
    registeredIDs[id2] = true
end

function pswitchable.onEventDirect(obj, eventName)
    if (eventName == "P Switch - Start" and not psState) or (eventName == "P Switch - End" and psState) then
        if(eventName == "P Switch - Start") then
	    psState = true
        else
	    psState = false
	end
	
        for k,v in ipairs(sets) do
            blockutils.queueSwitch(v[1], v[2])
        end
    end
end

function pswitchable.onInitAPI()
    registerEvent(pswitchable, "onEventDirect")
end

return pswitchable
(there might be caveats to this that i've missed too, but it seems to work)

then you can make two custom blocks and just copy the code from the basegame switch blocks (just make sure their ids are adjacent and in the same order), except you'd replace

Code: Select all

require("blocks/ai/pswitchable")
with a require for your custom ai script.

dochalkos
Shy Guy
Shy Guy
Posts: 5
Joined: Sat Nov 26, 2022 8:49 pm

Re: YI Switch Block Behavior Change

Postby dochalkos » Mon Nov 28, 2022 3:42 pm

deice wrote:
Mon Nov 28, 2022 9:40 am
i had actually looked into this a bit more some time ago but must've forgotten to post it in that thread you linked.
the permanent toggle of the yoshi's island switch blocks is indeed not intended behavior. namely, the "pswitchable" ai script fails to account for a vanilla smbx bug where the "P Switch - End" event triggers twice.
Haha wow I couldn't ask for a better response. Thank you man, your code did the trick. I didn't know about the p-switch bug, though I suspected something wasn't working properly because of the way the blocks would flicker between dotted-line and solid when the p-switch event ended.

Just_Thomas
Blooper
Blooper
Posts: 190
Joined: Sat Dec 16, 2023 3:32 am
Pronouns: he/him

Re: Accurate YI Switch Block Behavior (Fix included)

Postby Just_Thomas » Sun Dec 17, 2023 6:05 am

For those looking for a ready-to-use package
here:
https://www.file-upload.net/download-15 ... e.zip.html
or here:
https://fastupload.io/en/0KpJqNHXuQZpK5B/file
or here:
https://file.io/t8kWb1EreiiC
All credits to user DeIce for providing the code and fix. Graphics/Colors for blocks are in SMB3 Allstars style.


Return to “LunaLua Help”

Who is online

Users browsing this forum: No registered users and 0 guests

SMWCentralTalkhausMario Fan Games GalaxyKafukaMarioWikiSMBXEquipoEstelari