• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua Function [TFS 1.2/1.3] player:setAttackSpeed(ms) | player:getAttackSpeed()

Infernum

Senator
Joined
Feb 14, 2015
Messages
5,643
Solutions
559
Reaction score
3,946
player.h
above
C++:
uint32_t inventoryWeight;
add
C++:
uint32_t attackSpeed = 0;

replace getAttackSpeed() with this
C++:
        uint32_t getAttackSpeed() const {
            if (attackSpeed > 0) {
                return attackSpeed;
            }
            return vocation->getAttackSpeed();
        }

add this above getAttackSpeed()
C++:
        void setAttackSpeed(uint32_t speed) {
            attackSpeed = speed;
        }

luascript.h
below
C++:
static int luaPlayerGetFightMode(lua_State* L);
add
C++:
        static int luaPlayerSetAttackSpeed(lua_State* L);
        static int luaPlayerGetAttackSpeed(lua_State* L);

luascript.cpp

below
C++:
registerMethod("Player", "getFightMode", LuaScriptInterface::luaPlayerGetFightMode);
add
C++:
    registerMethod("Player", "getAttackSpeed", LuaScriptInterface::luaPlayerGetAttackSpeed);
    registerMethod("Player", "setAttackSpeed", LuaScriptInterface::luaPlayerSetAttackSpeed);

below
C++:
int LuaScriptInterface::luaPlayerHasSecureMode(lua_State* L)
{
    // player:hasSecureMode()
    Player* player = getUserdata<Player>(L, 1);
    if (player) {
        pushBoolean(L, player->secureMode);
    } else {
        lua_pushnil(L);
    }
    return 1;
}
add
C++:
int LuaScriptInterface::luaPlayerSetAttackSpeed(lua_State* L)
{
    // player:setAttackSpeed(ms)
    Player* player = getUserdata<Player>(L, 1);
    uint32_t ms = getNumber<uint32_t>(L, 2);
    if (player) {
        player->setAttackSpeed(ms);
        pushBoolean(L, true);
    } else {
        lua_pushnil(L);
    }
    return 1;
}

int LuaScriptInterface::luaPlayerGetAttackSpeed(lua_State* L)
{
    // player:getAttackSpeed()
    Player* player = getUserdata<Player>(L, 1);
    if (player) {
        lua_pushnumber(L, player->getAttackSpeed());
    } else {
        lua_pushnil(L);
    }
    return 1;
}

note: if you're using 1.2 and you don't want the player to be interrupted while using items/walking you need to add this code: Fix #1319 by Mkalo · Pull Request #2109 · otland/forgottenserver · GitHub
 
Last edited by a moderator:
Can you please explain where exactly?

replace getAttackSpeed() with this
Code:
uint32_t getAttackSpeed() const {
if (attackSpeed > 0) {
return attackSpeed;
}
return vocation->getAttackSpeed();
}

add this above getAttackSpeed()
Code:
void setAttackSpeed(uint32_t speed) {
attackSpeed = speed
}
 
inside player.h
there's only 1 of each of those functions, ctrl+f and replace them
 
no, you just need to change the pre-existing getAttackSpeed in player.h
what about in luascript.cpp, normally all the functions you put in there are registered to the Player class so that it can be used in Lua on Player userdata
 
I think you are missing that player.h does not have attackSpeed as property... It should be added.

what about in luascript.cpp, normally all the functions you put in there are registered to the Player class so that it can be used in Lua on Player userdata
And if you want to use it in lua scripts, you need to register the methods in Player class as razorBlade said:

Code:
    registerMethod("Player", "getAttackSpeed", LuaScriptInterface::luaPlayerGetAttackSpeed);
    registerMethod("Player", "setAttackSpeed", LuaScriptInterface::luaPlayerSetAttackSpeed);
 
Last edited by a moderator:
it doesn't have to be exactly under that function, it can be underneath any function that starts with luaPlayerXXXX
for 1.2 you could put it under static int luaPlayerSetStamina(lua_State* L);
 
I had other errors with my source where the boost files weren't connecting with compilation process properly as well, so I don't know why there was problem when I tried compiling. I tried putting in the right spots. Idk boost isn't working for me properly. I'm done trying different ideas, thanks for responding though.
 
I'm sorry for asking this, but how can I apply this to an action or spell script? I have tried only adding something like:
Code:
player:setAttackSpeed(1000)
But it hasn't modified the att speed...
Can someone explain further about this?
Thanks for the assistance!
 
You're using it correctly, but I'm guessing your base attack speed is 1000ms in vocations.xml. Remember that it's not an additive bonus, it sets the attack speed to what you pass to the function.
 
I just tested it, and it works perfectly.
I wonder how could I create a spell to temporarily increase my attack speed.
Anyways, thanks, Infernum for one more awesome mod.
 
0 errors during compile but server cannot take off console appears and disappears

(sorry for english)
 
Last edited:
I have set it up but i dont understand how do i choose the attack speed set?
 
I have set it up but i dont understand how do i choose the attack speed set?
you can for example add this to an action script and it will set player attack speed to 1000:

Lua:
player:setAttackSpeed(1000)
 
Interesting, but imo it should be handled the same way the player speed is:

  • a condition with attackspeed change
  • a function to change base value
  • a possibility to do it as an item attribute
 
Interesting, but imo it should be handled the same way the player speed is:

  • a condition with attackspeed change
  • a function to change base value
  • a possibility to do it as an item attribute

but yeah, I agree, it would be cool to have spells and items with that
 
Back
Top