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

Discussion in 'C++ Codes' started by Static_, Feb 24, 2018.

  1. Static_

    Static_ Support Team Support Team

    Joined:
    Feb 14, 2015
    Messages:
    3,657
    Likes Received:
    1,604
    Best Answers:
    265
    player.h

    replace getAttackSpeed() with this
    Code (C++):
    1.         uint32_t getAttackSpeed() const {
    2.             if (attackSpeed > 0) {
    3.                 return attackSpeed;
    4.             }
    5.             return vocation->getAttackSpeed();
    6.         }
    add this above getAttackSpeed()
    Code (C++):
    1.         void setAttackSpeed(uint32_t speed) {
    2.             attackSpeed = speed;
    3.         }
    luascript.h
    below
    Code (C++):
    1. static int luaPlayerGetFightMode(lua_State* L);
    add
    Code (C++):
    1.         static int luaPlayerSetAttackSpeed(lua_State* L);
    2.         static int luaPlayerGetAttackSpeed(lua_State* L);
    luascript.cpp
    below
    Code (C++):
    1. int LuaScriptInterface::luaPlayerHasSecureMode(lua_State* L)
    2. {
    3.     // player:hasSecureMode()
    4.     Player* player = getUserdata<Player>(L, 1);
    5.     if (player) {
    6.         pushBoolean(L, player->secureMode);
    7.     } else {
    8.         lua_pushnil(L);
    9.     }
    10.     return 1;
    11. }
    add
    Code (C++):
    1. int LuaScriptInterface::luaPlayerSetAttackSpeed(lua_State* L)
    2. {
    3.     // player:setAttackSpeed(ms)
    4.     Player* player = getUserdata<Player>(L, 1);
    5.     uint32_t ms = getNumber<uint32_t>(L, 2);
    6.     if (player) {
    7.         player->setAttackSpeed(ms);
    8.         pushBoolean(L, true);
    9.     } else {
    10.         lua_pushnil(L);
    11.     }
    12.     return 1;
    13. }
    14.  
    15. int LuaScriptInterface::luaPlayerGetAttackSpeed(lua_State* L)
    16. {
    17.     // player:getAttackSpeed()
    18.     Player* player = getUserdata<Player>(L, 1);
    19.     if (player) {
    20.         lua_pushnumber(L, player->getAttackSpeed());
    21.     } else {
    22.         lua_pushnil(L);
    23.     }
    24.     return 1;
    25. }
    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
     
    Xinax, silveralol, 2Rec and 2 others like this.
  2. bayview

    bayview Codex NG

    Joined:
    Jan 25, 2018
    Messages:
    372
    Likes Received:
    147
    Best Answers:
    17
    Thanks for copying my code and releasing it as your own.
     
  3. felipek9

    felipek9 New Member

    Joined:
    Sep 4, 2014
    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Can you please explain where exactly?

    replace getAttackSpeed() with this
    Code (Text):
    1. uint32_t getAttackSpeed() const {
    2. if (attackSpeed > 0) {
    3. return attackSpeed;
    4. }
    5. return vocation->getAttackSpeed();
    6. }
    add this above getAttackSpeed()
    Code (Text):
    1.  
    2. void setAttackSpeed(uint32_t speed) {
    3. attackSpeed = speed
    4. }
     
  4. Static_

    Static_ Support Team Support Team

    Joined:
    Feb 14, 2015
    Messages:
    3,657
    Likes Received:
    1,604
    Best Answers:
    265
    inside player.h
    there's only 1 of each of those functions, ctrl+f and replace them
     
  5. RazorBlade

    RazorBlade Retired Snek

    Joined:
    Nov 7, 2009
    Messages:
    2,027
    Likes Received:
    606
    Best Answers:
    1
    Don't you need to register the methods to the Player class?
     
  6. Static_

    Static_ Support Team Support Team

    Joined:
    Feb 14, 2015
    Messages:
    3,657
    Likes Received:
    1,604
    Best Answers:
    265
    no, you just need to change the pre-existing getAttackSpeed in player.h
     
  7. RazorBlade

    RazorBlade Retired Snek

    Joined:
    Nov 7, 2009
    Messages:
    2,027
    Likes Received:
    606
    Best Answers:
    1
    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
     

Share This Page

Loading...