Lua/Custom player ability
One of the most useful aspects of Lua scripting in SRB2 is the ability to give characters entirely new gameplay mechanics. This tutorial will provide you with knowledge on how to use basic Lua hooks to add new abilities.
Making a double jump with the AbilitySpecial hook
Start a new Lua script (in any text editor of your choice) and paste the following into it:
addHook("AbilitySpecial", function(p)
end)
First, we only want this script to run if the player is using Sonic, so we'll add the following:
addHook("AbilitySpecial", function(p)
if p.mo.skin == "sonic" then
end
end)
Next, the jump should be able to be used once. We'll check if the player has already used their ability with the PF_THOKKED
player flag.
addHook("AbilitySpecial", function(p)
if p.mo.skin == "sonic" then
if p.pflags & PF_THOKKED then
return true
end
end
end)
Now, if the player has already used the double jump and presses jump again, the script will exit.
addHook("AbilitySpecial", function(p)
if p.mo.skin == "sonic" then
if p.pflags & PF_THOKKED then
return true
end
p.pflags = $ | PF_THOKKED
end
end)
Finally, we will set the player's z momentum. Unlike most other games, Sonic Robo Blast 2 uses the z axis as the upwards direction.
addHook("AbilitySpecial", function(p)
if p.mo.skin == "sonic" then
if p.pflags & PF_THOKKED then
return true
end
P_SetObjectMomZ(p.mo, 10*FRACUNIT, true)
p.pflags = $ | PF_THOKKED
end
end)
The boolean true
determines if the momentum is relative to the player's existing vertical momentum, which also corrects for their scale.
But that's not all, there are many other hooks out there. See Lua/Hooks for a complete list.
Lua | [view] | |
Language features | Syntax • Metatables | |
SRB2 data | Actions • Constants • Functions • Global variables • Hooks • Userdata structures | |
SRB2Kart data | Kart Userdata structures • Kart Functions • Kart Hooks • Kart Global Variables and Constants | |
Tutorials | Freeslots and Object resources • Custom player ability |