• This is a read only backup of the old Emudevs forum. If you want to have anything removed, please message me on Discord: KittyKaev

[SOLVED] {C++} Waring: truncation from double to float

Status
Not open for further replies.

katos

Noble Member
Hey all!

Getting a few warnings on a script, and it doesn't work in-game, but the core compiles.

Warning 3 warning C4305: 'initializing' : truncation from 'double' to 'float' D:\katos\test core\dev\src\server\scripts\Custom\Arena_Graveyard.cpp 15 1 scripts
Warning 4 warning C4305: 'initializing' : truncation from 'double' to 'float' D:\katos\test core\dev\src\server\scripts\Custom\Arena_Graveyard.cpp 16 1 scripts
Warning 5 warning C4305: 'initializing' : truncation from 'double' to 'float' D:\katos\test core\dev\src\server\scripts\Custom\Arena_Graveyard.cpp 17 1 scripts
Warning 6 warning C4305: 'initializing' : truncation from 'double' to 'float' D:\katos\test core\dev\src\server\scripts\Custom\Arena_Graveyard.cpp 18 1 scripts

This one has me rather stumped. I am wondering if i'd be best setting the floats to:

Code:
      const float x = -8556.891602;
                const float y = 1954.818481;
                const float z = 100.351578;
                const float o = 5.100673;

Any help would be much appreciated.
The script: http://pastebin.com/fg6t6CtP

Regards,
Katos.
 

jahler

Enthusiast
It's because you're assigning a double to a float.

Use float x = -8556.891602f;

The "f" tells the compiler it's a float value.
 

yvoms

Exalted Member
Hey all!

Getting a few warnings on a script, and it doesn't work in-game, but the core compiles.



This one has me rather stumped. I am wondering if i'd be best setting the floats to:

Code:
      const float x = -8556.891602;
                const float y = 1954.818481;
                const float z = 100.351578;
                const float o = 5.100673;

Any help would be much appreciated.
The script: http://pastebin.com/fg6t6CtP

Regards,
Katos.

It's because you're assigning a double to a float.

Use float x = -8556.891602f;

The "f" tells the compiler it's a float value.


True, so the code would be :
Code:
      const float x = -8556.891602f;
                const float y = 1954.818481f;
                const float z = 100.351578f;
                const float o = 5.100673f;
 

katos

Noble Member
True, so the code would be :
Code:
      const float x = -8556.891602f;
                const float y = 1954.818481f;
                const float z = 100.351578f;
                const float o = 5.100673f;

So I was right with my suspicions? Thanks, I will try this tomorrow then.

Many thanks!
Katos
 

FoxGaming

Exalted Member
So I was right with my suspicions? Thanks, I will try this tomorrow then.

Many thanks!
Katos

You don't necessarily have to store them as constants, the biggest change is you have to cast them to floats so the compiler knows to store them as floats instead of doubles.
 

ToxicDev

Banned
I remember this katos should of just came to me i got you on skype.. ;O

They never really had to be defined i think i did that to make it easier to edit. If your too lazy to try what he said just use this http://pastebin.com/zMa7Sz9A it's just a quick edit without the x,y,z,o defined instead there in the teleport method. I did this edit then i read yvoms post and he is probably right adding a f in there should fix them. I'll have to remember that in the future.
 
Last edited:

Tommy

Founder
I remember this katos should of just came to me i got you on skype.. ;O.

Or he can make a thread on EmuDevs (like he did) and get more support and ideas of what to do about such warnings. Skype is dumb for support, hence why we don't do support via Skype since it takes away from SEO.

Regardless, I do agree on the part of defining the said variables: you can remove them and just add them to:

Code:
killed->TeleportTo(1, -8556.891602, 1954.818481, 100.351578, 5.100673);

since they serve no real purpose as Toxic implied. Same goes for areaId as well. The script is pretty nasty in-general too, I'll clean it up below:

Code:
class OnPvPDeath : public PlayerScript
{
public: 
    OnPvPDeath() : PlayerScript("OnPvPDeath") { }
 
    void OnPVPKill(Player* /*killer*/, Player* killed)
    {
        if (killed->GetAreaId() == 3478)
        {
            if (killed->isDead())
            {
                killed->TeleportTo(1, -8556.891602f, 1954.818481f, 100.351578f, 5.100673f);
                killed->ResurrectPlayer(1.0f, false);
                killed->SpawnCorpseBones();
                killed->SaveToDB();
            }
        }
    }
};
 
void AddSC_OnPvPDeath()
{
    new OnPvPDeath();
}
 

katos

Noble Member
I remember this katos should of just came to me i got you on skype.. ;O

They never really had to be defined i think i did that to make it easier to edit. If your too lazy to try what he said just use this http://pastebin.com/zMa7Sz9A it's just a quick edit without the x,y,z,o defined instead there in the teleport method. I did this edit then i read yvoms post and he is probably right adding a f in there should fix them. I'll have to remember that in the future.


Hi toxic, I actually didn't come to you as I thought you left emulation, also as tommy said I could get more insight into the issue and as such would be able to further my knowledge of c++ and understand what the warning was a little better! I thank you for your response none the less.

Or he can make a thread on EmuDevs (like he did) and get more support and ideas of what to do about such warnings. Skype is dumb for support, hence why we don't do support via Skype since it takes away from SEO.

Regardless, I do agree on the part of defining the said variables: you can remove them and just add them to:

Code:
killed->TeleportTo(1, -8556.891602, 1954.818481, 100.351578, 5.100673);

since they serve no real purpose as Toxic implied. Same goes for areaId as well. The script is pretty nasty in-general too, I'll clean it up below:

Code:
class OnPvPDeath : public PlayerScript
{
public: 
    OnPvPDeath() : PlayerScript("OnPvPDeath") { }
 
    void OnPVPKill(Player* /*killer*/, Player* killed)
    {
        if (killed->GetAreaId() == 3478)
        {
            if (killed->isDead())
            {
                killed->TeleportTo(1, -8556.891602f, 1954.818481f, 100.351578f, 5.100673f);
                killed->ResurrectPlayer(1.0f, false);
                killed->SpawnCorpseBones();
                killed->SaveToDB();
            }
        }
    }
};
 
void AddSC_OnPvPDeath()
{
    new OnPvPDeath();
}

Interesting that the definition of the areas as floats serves no real purpose, I'll try the script like this tommy and report back. Many thanks for your help there! :)
 

Tommy

Founder
Interesting that the definition of the areas as floats serves no real purpose, I'll try the script like this tommy and report back. Many thanks for your help there! :)

Jahler practically summed up the warning explanation, hence why I didn't write anything about it. Variables aren't pointless in general, however, it makes them pointless when you are going to only use them on one line of code and that's it for a 30 line teleport on death script. It would make more sense if you were reinitializing the variables for more parts of the script, if the script gets any bigger. Though, the time you spend on reinitializing variables can result in the time you can spend adding it to the function arguments itself (which at that point could keep the script clean from unnecessary variables). As for the areaId, if you were to add more, I would recommend in putting them into an enumerator instead of creating a variable. Example of that:

Code:
enum AreaIds
{
    MY_AREA_ID = 3478
};

And all you would have to do is (for the if statement):

Code:
if (killed->GetAreaId() == MY_AREA_ID)
 
Status
Not open for further replies.
Top