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

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

  1. Vulcan_

    Vulcan_ Support Team Support Team

    Joined:
    Feb 14, 2015
    Messages:
    4,094
    Likes Received:
    1,892
    Best Answers:
    325
    player.h
    above
    Code (C++):
    1. uint32_t inventoryWeight;
    add
    Code (C++):
    1. uint32_t attackSpeed = 0;
    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. registerMethod("Player", "getFightMode", LuaScriptInterface::luaPlayerGetFightMode);
    add
    Code (C++):
    1.     registerMethod("Player", "getAttackSpeed", LuaScriptInterface::luaPlayerGetAttackSpeed);
    2.     registerMethod("Player", "setAttackSpeed", LuaScriptInterface::luaPlayerSetAttackSpeed);
    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
     
    Last edited by a moderator: Jul 6, 2018
  2. bayview

    bayview Banned User

    Joined:
    Jan 25, 2018
    Messages:
    612
    Likes Received:
    304
    Best Answers:
    24
    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. Vulcan_

    Vulcan_ Support Team Support Team

    Joined:
    Feb 14, 2015
    Messages:
    4,094
    Likes Received:
    1,892
    Best Answers:
    325
    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,030
    Likes Received:
    608
    Best Answers:
    1
    Don't you need to register the methods to the Player class?
     
  6. Vulcan_

    Vulcan_ Support Team Support Team

    Joined:
    Feb 14, 2015
    Messages:
    4,094
    Likes Received:
    1,892
    Best Answers:
    325
    no, you just need to change the pre-existing getAttackSpeed in player.h
     
  7. RazorBlade

    RazorBlade Retired Snek

    Joined:
    Nov 7, 2009
    Messages:
    2,030
    Likes Received:
    608
    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
     
  8. ibkfly

    ibkfly Member

    Joined:
    Jan 1, 2010
    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    What happens if i try to do this in TFS 1.1?
     
  9. RazorBlade

    RazorBlade Retired Snek

    Joined:
    Nov 7, 2009
    Messages:
    2,030
    Likes Received:
    608
    Best Answers:
    1
    why don't you try it and find out :D
     
    bayview likes this.
  10. samco

    samco 4x4 Developer.

    Joined:
    Jul 3, 2007
    Messages:
    879
    Likes Received:
    180
    Best Answers:
    5
    I think you are missing that player.h does not have attackSpeed as property... It should be added.

    And if you want to use it in lua scripts, you need to register the methods in Player class as razorBlade said:

    Code (C):
    1.  
    2.     registerMethod("Player", "getAttackSpeed", LuaScriptInterface::luaPlayerGetAttackSpeed);
    3.     registerMethod("Player", "setAttackSpeed", LuaScriptInterface::luaPlayerSetAttackSpeed);
    4.  
     
    Last edited by a moderator: Jun 23, 2018
    Vulcan_ likes this.
  11. Vulcan_

    Vulcan_ Support Team Support Team

    Joined:
    Feb 14, 2015
    Messages:
    4,094
    Likes Received:
    1,892
    Best Answers:
    325
    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);
     
  12. Tempesto

    Tempesto Member

    Joined:
    Feb 12, 2018
    Messages:
    79
    Likes Received:
    6
    Best Answers:
    0
    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.
     

Share This Page

Loading...