• 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!

C++ change your stats percent // Points

Error 502

Intermediate OT User
Joined
Sep 25, 2022
Messages
237
Solutions
8
Reaction score
145
Location
Chile
1.5 Nekiro downgrade.

I don't have much experience in C++. I come to ask how to do an IF function without dying trying xd

I want to know if the way of coding in c++ is correct, especially the storage


Context: how to change the way to display HP and mana according to storage , changing them with talkations. using c++

as in the gif



protocolgame.cpp on void ProtocolGame::AddPlayerStats(NetworkMessage& msg)


HP

C++:
void ProtocolGame::AddPlayerStats(NetworkMessage& msg)
{
    msg.addByte(0xA0);

   
//Get storage and IF
        std::string value;
    getStorage(7999, value);
int32_t check = atoi(value.c_str());
if(check >=1){

// IF storage >=1  then                   HP in   percent

    if (player->getMaxHealth() > 0)
    {
        msg.add<uint16_t>(std::min<int32_t>(player->getHealth() * 100 / player->getMaxHealth(), std::numeric_limits<uint16_t>::max()));
        msg.add<uint16_t>(100);
    }
    else
    {
        msg.add<uint16_t>(0);
        msg.add<uint16_t>(0);
    }
}
else
   
    {
// else storage = 0   then                  HP in   Points

  msg.add<uint16_t>(std::min<int32_t>(player->getHealth(), std::numeric_limits<uint16_t>::max()));
    msg.add<uint16_t>(std::min<int32_t>(player->getMaxHealth(), std::numeric_limits<uint16_t>::max()));
    }




MANA
C++:
if(check >=1){
    if (player->getMaxMana() > 0)
    {
        msg.add<uint16_t>(std::min<int32_t>(player->getMana() * 100 / player->getMaxMana(), std::numeric_limits<uint16_t>::max()));
        msg.add<uint16_t>(100);
    }
    else
    {
        msg.add<uint16_t>(0);
        msg.add<uint16_t>(0);
    }
}
    else
    {
 msg.add<uint16_t>(std::min<int32_t>(player->getMana(), std::numeric_limits<uint16_t>::max()));
    msg.add<uint16_t>(std::min<int32_t>(player->getMaxMana(), std::numeric_limits<uint16_t>::max()));


}



Beta script lua On Server

Lua:
local creatureEvent = CreatureEvent("setStorage")

function creatureEvent.onLogin(player)
   
    if player:getLastLoginSaved() == 0 then
    player:setStorageValue(7999, 1)
    end

    return true
end

creatureEvent:register()


local talkAction = TalkAction("!hp")

function talkAction.onSay(player, words, param, type)
   
if player:getStorageValue(7999) == 1 then

player:sendTextMessage(MESSAGE_STATUS_SMALL, 'HP/Mana in percentage')
player:setStorageValue(7999, 0)
else

    player:sendTextMessage(MESSAGE_STATUS_SMALL, 'HP/Mana  in points')
    player:setStorageValue(7999, 1)
    return false
end

talkAction:register()
I don't need help in lua but in c++ I don't know the language, I don't know how to call storages or do if functions
 
Solution
Here is one trick

Add this on your talkaction, it will update the stats for you
player:setHealth(player:getHealth())

Option 2:

Update the stats when you access the storage key

Player::addStorageValue
C++:
    if (key == 7999)
        sendStats();

Option 3:

Make a new lua function to call the method inside the talkaction
C++:
int LuaScriptInterface::luaPlayerUpdateStats(lua_State* L)
{
    // player:updateStats()
    Player* player = getUserdata<Player>(L, 1);
    if (!player) {
        lua_pushnil(L);
        return 1;
    }

    player->sendStats();
    pushBoolean(L, true);
    return 1;
}
UP

I tried it and it worked.
important detail, I have to logout every time I want to change from points to %

my hp is in points
use the talkations
it does not work
if i logout
it works and my hp is in %

What part should I edit in the src , so that the client is updated every time I do the talkations and change from hp to points without having to logout
 
Here is one trick

Add this on your talkaction, it will update the stats for you
player:setHealth(player:getHealth())

Option 2:

Update the stats when you access the storage key

Player::addStorageValue
C++:
    if (key == 7999)
        sendStats();

Option 3:

Make a new lua function to call the method inside the talkaction
C++:
int LuaScriptInterface::luaPlayerUpdateStats(lua_State* L)
{
    // player:updateStats()
    Player* player = getUserdata<Player>(L, 1);
    if (!player) {
        lua_pushnil(L);
        return 1;
    }

    player->sendStats();
    pushBoolean(L, true);
    return 1;
}
 
Solution
Back
Top