• 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

Support Team
Joined
Feb 14, 2015
Messages
5,496
Best answers
547
Reaction score
3,395
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:

felipek9

New Member
Joined
Sep 4, 2014
Messages
2
Best answers
0
Reaction score
0
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
}
 
OP
Infernum

Infernum

Support Team
Joined
Feb 14, 2015
Messages
5,496
Best answers
547
Reaction score
3,395
inside player.h
there's only 1 of each of those functions, ctrl+f and replace them
 

RazorBlade

Retired Snek
Joined
Nov 7, 2009
Messages
2,040
Best answers
3
Reaction score
600
Location
Canada
Don't you need to register the methods to the Player class?
 

RazorBlade

Retired Snek
Joined
Nov 7, 2009
Messages
2,040
Best answers
3
Reaction score
600
Location
Canada
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
 

ibkfly

New Member
Joined
Jan 1, 2010
Messages
68
Best answers
0
Reaction score
4
What happens if i try to do this in TFS 1.1?
 

samco

4x4 Developer.
Joined
Jul 3, 2007
Messages
932
Best answers
7
Reaction score
197
Location
Spain
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:
OP
Infernum

Infernum

Support Team
Joined
Feb 14, 2015
Messages
5,496
Best answers
547
Reaction score
3,395
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);
 

Tempesto

Member
Joined
Feb 12, 2018
Messages
79
Best answers
0
Reaction score
5
Location
Ruining your servers.
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.
 

demon088

#088 in the Horde
Joined
Jun 17, 2009
Messages
176
Best answers
3
Reaction score
16
Location
Hell
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!
 
OP
Infernum

Infernum

Support Team
Joined
Feb 14, 2015
Messages
5,496
Best answers
547
Reaction score
3,395
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.
 

rickgv

Member
Joined
Oct 22, 2007
Messages
44
Best answers
0
Reaction score
12
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.
 

ssiwyy158

Member
Joined
Jan 24, 2011
Messages
94
Best answers
2
Reaction score
6
0 errors during compile but server cannot take off console appears and disappears

(sorry for english)
 
Last edited:
Top