• 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++ health and healthpercent in spell's cost, please check code

andu

Sold 649 scripts, 25 maps and 9 events!
Joined
Aug 7, 2009
Messages
969
Solutions
17
Reaction score
354
GitHub
olrios
Twitch
olrios
Edit:
I did this, compiled it and seem to works without any issues. Please check it for potential problems.



Old post:
I'd like to make healthCost. Spells where you have to spend heal to cast. Tried to make it in VS but without success.
Got error
Code:
spells.cpp(640): warning C4018: '<': signed/unsigned mismatch
In this line:
C++:
if (player->getHealth() < getHealthCost(player) && !player->hasFlag(PlayerFlag_HasInfiniteMana)) {

Here is my getHealthCost:
C++:
uint32_t Spell::getHealthCost(const Player* player) const
{
    uint32_t finalHealth = 0;
    if (health != 0) {
        finalHealth = health;
    }

    if (healthPercent != 0) {
        uint32_t maxHealth = player->getMaxHealth();
        uint32_t healthCost = (maxHealth * healthPercent) / 100;
        finalHealth = finalHealth + healthCost;
    }

    return finalHealth;
}

When I tried to solve it with info from stackoverflow website then no errors in console, no errors in VS but server crashes during loading.
Any tips? I Also added healthCost in everyplace where manaCost is too. Just copied it and changed mana to health. But cannot handle the issue with crashing
 
Last edited:
You are not checking if player pointer is valid. Add If (!player) { return 0; } at the top
 
You are not checking if player pointer is valid. Add If (!player) { return 0; } at the top
I copied step by step uint32_t Spell::getManaCost, in mana case there's no checking for player. And btw this is not fixing the issue. There must be sth different.
 
You are not checking if player pointer is valid. Add If (!player) { return 0; } at the top
the checking should be done in playerSpellCheck which never expect player to be nullptr so it is useless check and only cpu-consuming not fixing anything.

Got error
Code:
spells.cpp(640): warning C4018: '<': signed/unsigned mismatch
In this line:
C++:
if (player->getHealth() < getHealthCost(player) && !player->hasFlag(PlayerFlag_HasInfiniteMana)) {
this isn't error just warning because if statement use different instructions for checking signed int's and unsigned int's if you want to get rid of this warning simple use this if statement:
C++:
if (player->getHealth() < static_cast<int32_t>(getHealthCost(player)) && !player->hasFlag(PlayerFlag_HasInfiniteMana))
but I recommend changing the whole getHealthCost to use int32_t not uint32_t however results should be the same as with cast.
About the crash you probably done something wrong in configureSpell because you wrote that the server crashing at server startup, maybe compile server in debug mode? It should tell you more that what we can deduce with the code you post because there's nothing wrong with it.
 
#edit
Didn't saw your post.

I'll try to debug it like you said. Will give more info soon. Going to sleep right now. See you tommorow.
 
Not sure I'm publishing it in the correct way. I forked forgottenserver and created new branch in the fork. Idk how GitHub exactly works.

But code works, no errors, no issues, no crashes, please check it for potential problems.

Here's code:
 
Last edited:
Back
Top