Lua Troubleshooting

Fenastra

SRB2 Enjoyer
Hey fellas! I need some help with Lua.

For context, I've learned the basics of Lua coding, such as variables, functions and whatnot. However, SRB2's bastardized Lua is still confounding me.

I attempted to make a simple mod for Sonic that causes him to slowly float up when the Jump button is held. The ability is only meant to be usable for 100 tics, forcing Sonic to land before he can start using it again. However, Sonic seems to be able to use it indefinitely. I found that the touchedGround boolean is never false, even after 100 tics.

I'm unsure of what I'm actually doing here, so I can imagine that any part of this script is messing things up. Although, I'm confident the main issue has to do with line 7.

Can anyone tell me what's preventing this code from working as intended, and how I can fix it?
The lua is attached.
 

Attachments

  • lua thing.lua
    1.7 KB · Views: 55
Players don't have a state. Mobjs do.
You can get a player's mobj through player.mo, or in your case since your player is in p, p.mo.
NOTE that mo can be nil if the player is spectating or otherwise unavailable. Account for that before messing with the player object in some way!



On a separate note,
if p.state == S_PLAY_STND or S_PLAY_WALK or S_PLAY_RUN then
This is not correct syntax. What Lua is reading here is:
IS (p.state == S_PLAY_STND) A TRUTHY? or IS (S_PLAY_WALK) A TRUTHY? or IS (S_PLAY_RUN) A TRUTHY?

If you want to check if p.state - or, well, p.mo.state - is any of these three possible options, then you have to repeat the check three times:
if p.mo.state == S_PLAY_STND or p.mo.state == S_PLAY_WALK or p.mo.state == S_PLAY_RUN then
It's a bit long, but does the job.

An alternative way is to create a table outside all hooks and write key-value pairs for the states you wish to check, with the key being a state, and its value being true.
For example, if I create a table somewhere that looks like this:
Funny:
local coolStates = {
    [S_PLAY_STND] = true,
    [S_PLAY_WALK] = true,
    [S_PLAY_RUN] = true,
}
then I can then check if the player is wearing any of these states like this:
if coolStates[p.mo.state] == true then



Note that a player is not necessarily standing on the ground if they're on any of these three states. Walking off a ledge, for example, keeps you in whatever animation you were on.
To check if a mobj is touching the ground, use the P_IsObjectOnGround function.



Be careful with these variables you defined outside the hooks. The way you wrote it, players don't get their own instance of the variables, but rather all players will use the same variable to decide if they will be using your ability.

To make sure each player has its own set of variables, initialize them within the player.
As an example: within a player hook (or somewhere that provides you with a player), do something like this BEFORE messing with the variables:
funey:
if p.jumpCount == nil then -- does the player not have a jumpCount?
    p.jumpCount = 0 -- give it one with a default value
end
if p.touchedGround == nil then -- do the same with this one here
    p.touchedGround = false
end
This will give an instance of jumpCount and touchedGround to each individual player that you can then mess with, without other players meddling with it.
 

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

Back
Top