Attempting to create a custom ability and need some help

Dodobee

Avid Among us enjoyer
I am trying to make an ability where if the player jumps and then touches the ground again and jumps directly after they get a higher jump. I honestly have no idea how lua works but I tried something. I'm not sure what's the problem so if somebody can point out some issues, I would greatly appreciate it.
Lua:
local doublejump = 0
addHook("ThinkFrame", function()
    for player in players.iterate do
        if player.mo.skin ~= "sonic" then
            continue
        end
       
        if (player.pflags & PF_JUMPED)
        and doublejump = 0 then
        local double jump = 1
        end
       
        if (player.pflags & PF_JUMPED)
        and doublejump = 1 then
        p.jumpfactor = 1.5
        end
    end
end)
 
Last edited:
What do we do with this information?
You aren't asking for help or criticism or anything of the sort, you're just stating you're trying to make an ability.
 
I am trying to make an ability where if the player jumps and then touches the ground again and jumps directly after they get a higher jump. I honestly have no idea how lua works but I tried something.
Lua:
local doublejump = 0
addHook("ThinkFrame", function()
    for player in players.iterate do
        if player.mo.skin ~= "sonic" then
            continue
        end
      
        if (player.pflags & PF_JUMPED)
        and doublejump = 0 then
        local double jump = 1
        end
      
        if (player.pflags & PF_JUMPED)
        and doublejump = 1 then
        p.jumpfactor = 1.5
        end
    end
end)
A good effort in my opinion, you do something than i cannot, that's why im so suprised, because not everyone can domain that.
 
What do we do with this information?
You aren't asking for help or criticism or anything of the sort, you're just stating you're trying to make an ability.
I apologize, I was meaning to ask for help but it seems like I forgot. So basically I don't really know how to make this ability so I needed some help
 
I am trying to make an ability where if the player jumps and then touches the ground again and jumps directly after they get a higher jump. I honestly have no idea how lua works but I tried something.
Lua:
local doublejump = 0
addHook("ThinkFrame", function()
    for player in players.iterate do
        if player.mo.skin ~= "sonic" then
            continue
        end
      
        if (player.pflags & PF_JUMPED)
        and doublejump = 0 then
        local double jump = 1
        end
      
        if (player.pflags & PF_JUMPED)
        and doublejump = 1 then
        p.jumpfactor = 1.5
        end
    end
end)
This is amazing but looks so simple at the same time
 
I was meaning to ask for help
Ah, gotcha

What you did up there gets close, but it's not what you're looking to do.
If your code worked, it would only give you bonus jump height while you're in the middle of a jump, and since the bonus jump isn't being reset, it stays with the player forever.

Start by thinking what are you going to need for this. Since you want to have a little amount of time for the jump bonus to be active, you're going to need a timer for the player to hold onto.
Since this is something simple for the player, you don't need to use ThinkFrame - You can use PlayerThink just fine.
Start with a simple hook like this:
Lua:
addHook("PlayerThink", function(player) -- runs once per player each frame
    if player.mo.skin ~= "sonic" then
        return
    end
   
    if player.jumpbonustime == nil then -- if the player doesn't have a jumpbonustime timer
        player.jumpbonustime = 0 -- create one, starting at 0
    end
end)
This checks if the skin is NOT sonic, and stops the hook if that's the case.
Afterwards, it creates a jumpbonustime field within the player if it doesn't exist, and it starts at 0. This will be your timer.
That's all so far!

By itself the timer does nothing, since the script is not touching that field! Therefore, you give it a reason to move.
Since it's a timer, eventually it will have some number above 0, so we must add a check for that and we substract by 1 if that's the case:
Lua:
    if player.jumpbonustime == nil then -- if the player doesn't have a jumpbonustime timer
        player.jumpbonustime = 0 -- create one, starting at 0
    end
   
    -- and after that, but before the "end)", you add:
    if player.jumpbonustime > 0 then -- is jumpbonustime above 0?
        player.jumpbonustime = $ - 1 -- decrement by 1
    end

We added the check, but nothing in the script makes the timer go above 0 right now.
We should probably do that now by adding a new player check: See if the player is in the air because of a jump, and if so, set the timer to 1 second:
Lua:
    if player.jumpbonustime == nil then -- if the player doesn't have a jumpbonustime timer
        player.jumpbonustime = 0 -- create one, starting at 0
    end
   
    if not P_IsObjectOnGround(player.mo) -- is the player object in the air right now?
    and (player.pflags & PF_JUMPED) then -- is it because of a jump?
        player.jumpbonustime = 1*TICRATE -- set to 1 second
    end
   
    if player.jumpbonustime > 0 then -- is jumpbonustime above 0?
        player.jumpbonustime = $ - 1 -- decrement by 1 tic
    end
The constant TICRATE holds the internal game speed, which in this case is 35 since SRB2 runs at 35 tics a second (this is different to the renderer's "frames per second" counter). Because a TICRATE equals a single second, we use TICRATE whenever we want to measure game time.

This chunk of code will force the timer to stay at 1 second for as long as the player is in the air because of a jump. When the player stops meeting those conditions, the timer won't be forced to that number anymore, and it will begin counting down.

Finally, you change the player's jumpfactor based on jumpbonustime's number:
Lua:
    if not P_IsObjectOnGround(player.mo) -- is the player object in the air right now?
    and (player.pflags & PF_JUMPED) then -- is it because of a jump?
        player.jumpbonustime = 1*TICRATE -- set to 1 second
    end
   
    local normaljump = skins[player.skin].jumpfactor
    -- store in "normaljump" what would be the normal player's jumpfactor
   
    if player.jumpbonustime > 0 then -- is jumpbonustime above 0?
        player.jumpbonustime = $ - 1 -- decrement by 1 tic
        player.jumpfactor = normaljump + normaljump/2 -- make the player's jump 50% stronger
    else -- is jumpbonustime 0 (or lower)?
        player.jumpfactor = normaljump -- normal player jump force
    end
skins[player.skin].jumpfactor represents a player's default jumpfactor, but it's a lot of characters to type for that, so instead we take its value and store it in a new local variable named normaljump. Now whenever we want to reference that, we use normaljump :)
Afterwards, we set player.jumpfactor twice: Once when player.jumpbonustime > 0, and again when that is not the case. For as long as the player has jumpbonustime, they will have an increased jumpfactor.

And that should be it! Jumping won't make the player have a stronger jump (jumpfactor is changed after the player jumps), but once the player is airborne, a timer will be prepared. Once the player lands, the timer will tick down to 0 and for its duration, the player will have a stronger jump.

Hope this makes sense. Have fun!



A few things to note:
  • SRB2 Lua does not support decimal numbers. If an expression results in a decimal, the result is rounded down.
  • Variable names may not have spaces. You can use letters from A to Z, lowercase a to z, numbers (not at the start of the variable) and the underscore _.
 

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

Back
Top