lua help

chipakpaka

Member
i used a tutorial from the wiki in order to add Z momentum to my character's glide. it pretty much works, except for the fact that my character kinda uses multiability and i only want the Z momentum boost happen once, and not when i use the glide again before touching the ground.

the code:
Lua:
addHook("AbilitySpecial", function()
    for p in players.iterate
        if p.mo.skin == "idk"
        and p.pflags & PF_THOKKED then
            return true
        end
        P_SetObjectMomZ(p.mo, p.mindash/3, false)
        p.pflags = $|PF_THOKKED
    end
end)

sorry if my question isn't clear, but it would help me a lot.
 
what if you test:
The Code:
addHook("AbilitySpecial", function()
    for p in players.iterate
        if p.mo.skin == "idk"
        and not p.pflags & PF_THOKKED then
            return true
        end
        P_SetObjectMomZ(p.mo, p.mindash/3, false)
        p.pflags = $|PF_THOKKED
    end
end)
 
what if you test:
The Code:
addHook("AbilitySpecial", function()
    for p in players.iterate
        if p.mo.skin == "idk"
        and not p.pflags & PF_THOKKED then
            return true
        end
        P_SetObjectMomZ(p.mo, p.mindash/3, false)
        p.pflags = $|PF_THOKKED
    end
end)
it didn't work, something about "attempt to perform arithmetric on a boolean value". i think i'll try to think of other abilities, but thanks anyway
 
it didn't work, something about "attempt to perform arithmetric on a boolean value". i think i'll try to think of other abilities, but thanks anyway
i think i founded the solution:

Lua:
addHook("AbilitySpecial", function()
    p.idk_thokked = $ or false
    for p in players.iterate
        if p.mo.skin == "idk"
        and p.idk_thokked == false then
            return true
        end
        P_SetObjectMomZ(p.mo, p.mindash/3, false)
        p.pflags = $|PF_THOKKED
        p.idk_thokked == true
    end
end)
sorry if this dont work too.
 
i used a tutorial from the wiki in order to add Z momentum to my character's glide. it pretty much works, except for the fact that my character kinda uses multiability and i only want the Z momentum boost happen once, and not when i use the glide again before touching the ground.

the code:
Lua:
addHook("AbilitySpecial", function()
    for p in players.iterate
        if p.mo.skin == "idk"
        and p.pflags & PF_THOKKED then
            return true
        end
        P_SetObjectMomZ(p.mo, p.mindash/3, false)
        p.pflags = $|PF_THOKKED
    end
end)

sorry if my question isn't clear, but it would help me a lot.
There are multiple problems with this script, here's the whole shabang:

Lua:
addHook("AbilitySpecial", function(p)
    -- players.iterate loops through every player to execute code. It's only an acceptable alternative if you need to do that and the hook doesn't do it for you.
    -- using it, your code runs for everyone regardless of if they activated the AbilitySpecial or not.
    if p.mo.skin == "idk" then
        return -- return true is an override, so sonic can't thok if you use it here, since he's not idk
    end
    if p.mo.idk_boosted == false then
        P_SetObjectMomZ(p.mo, p.mindash/3, false)
        p.mo.idk_boosted = true -- set it to true so you can't do it until you touch ground
    end
end)

addHook("MobjThinker", function(mo)
    if mo.player == nil then
        return -- this is because of "voodoo dolls", MT_PLAYERs without a player attached.
    end
    -- I'm using MobjThinker for this because PlayerThink can miss being on the ground if you jump as soon as you touch the ground
    -- do this as Junio Sonic; you'll keep your boost even though you touched the ground
    if mo.idk_boosted == nil or P_IsObjectOnGround(mo) then
        mo.idk_boosted = false
    end
    -- all userdata custom vars are set to nil unless you set it
end, MT_PLAYER)

-- some extra notes

-- if you use more userdata custom vars you should do something like this:
--if mo.idkTable == nil then
--    mo.idkTable = {}
--    mo.idkTable.boosted = false
--    mo.idkTable.variable = whatever initial value you want
--end
-- this way, no matter how bad your naming schema is, you don't have conflicts with other mods potentially using your variables by coincidence
-- since they'd need to access the same table to get there, which should be unique enough to not have this issue happen to it
-- the main difference between userdata and tables is what happens if you set something to it
-- for example, let's say you do this:

-- local var idk = mo.idkTable
-- idk.boosted = true

-- you'd actually not do anything unless you added this after:
-- mo.idkTable = idk

-- vvhen you set a value to userdata, and modify something in the userdata, it modifies the userdata you set the value to, this is called a pointer.
-- if you set a value to a table, all of the contents of that table are copied to the nevv variable. If you modify something in the table, that modification is relevant to that specific variable. If this is confusing, here's another data type:

-- local var vspeed = mo.momz
-- vspeed = 10*FRACUNIT

-- this of course doesn't actually set mo.momz to 10*FRACUNIT, instead you set vspeed to mo.momz, overwritten it with 10*FRACUNIT, and did nothing with it.
-- mo.momz was not modified directly, so it wasn't modified. This is how tables work.
 
Last edited:
There are multiple problems with this script, here's the whole shabang:

Lua:
addHook("AbilitySpecial", function(p)
    -- players.iterate loops through every player to execute code. It's only an acceptable alternative if you need to do that and the hook doesn't do it for you.
    -- using it, your code runs for everyone regardless of if they activated the AbilitySpecial or not.
    if p.mo.skin == "idk" then
        return -- return true is an override, so sonic can't thok if you use it here, since he's not idk
    end
    if p.mo.idk_boosted == false then
        P_SetObjectMomZ(p.mo, p.mindash/3, false)
        p.mo.idk_boosted = true -- set it to true so you can't do it until you touch ground
    end
end)

addHook("MobjThinker", function(mo)
    if mo.player == nil then
        return -- this is because of "voodoo dolls", MT_PLAYERs without a player attached.
    end
    -- I'm using MobjThinker for this because PlayerThink can miss being on the ground if you jump as soon as you touch the ground
    -- do this as Junio Sonic; you'll keep your boost even though you touched the ground
    if mo.idk_boosted == nil or P_IsObjectOnGround(mo) then
        mo.idk_boosted = false
    end
    -- all userdata custom vars are set to nil unless you set it
end, MT_PLAYER)

-- some extra notes

-- if you use more userdata custom vars you should do something like this:
--if mo.idkTable == nil then
--    mo.idkTable = {}
--    mo.idkTable.boosted = false
--    mo.idkTable.variable = whatever initial value you want
--end
-- this way, no matter how bad your naming schema is, you don't have conflicts with other mods potentially using your variables by coincidence
-- since they'd need to access the same table to get there, which should be unique enough to not have this issue happen to it
-- the main difference between userdata and tables is what happens if you set something to it
-- for example, let's say you do this:

-- local var idk = mo.idkTable
-- idk.boosted = true

-- you'd actually not do anything unless you added this after:
-- mo.idkTable = idk

-- vvhen you set a value to userdata, and modify something in the userdata, it modifies the userdata you set the value to, this is called a pointer.
-- if you set a value to a table, all of the contents of that table are copied to the nevv variable. If you modify something in the table, that modification is relevant to that specific variable. If this is confusing, here's another data type:

-- local var vspeed = mo.momz
-- vspeed = 10*FRACUNIT

-- this of course doesn't actually set mo.momz to 10*FRACUNIT, instead you set vspeed to mo.momz, overwritten it with 10*FRACUNIT, and did nothing with it.
-- mo.momz was not modified directly, so it wasn't modified. This is how tables work.
sorry it took me longer than usually to reply, i was taking a break from my computer for a few days. anyways, i tested the script and it didn't work, but that's ok since i have some other ideas for abilities, and i could still use your notes, so thanks for that.
 

Who is viewing this thread (Total: 1, Members: 0, Guests: 1)

Back
Top