Counting float
#1

Hello,

I'm working on my inventory system and I want a function that counts the weight from all the slots.
This is what is made:

PHP код:
Float:GetInventoryWeight(playerid)
{
    new 
Float:counting;
    for(new 
i=1MAX_SLOTSi++)
    {
        
floatadd(countingGetObjectWeightFromInvID(Player[playerid][InvSlots][i]));
        
printf("%i. Weight = %.2f || counting = %f"iGetObjectWeightFromInvID(Player[playerid][InvSlots][i]), counting);
    }
    return 
Float:counting;

This is what I get:
Код HTML:
1. Weight = 2.50 || counting = 0.000000
2. Weight = 0.10 || counting = 0.000000
3. Weight = 0.10 || counting = 0.000000
4. Weight = 0.10 || counting = 0.000000
5. Weight = 0.10 || counting = 0.000000
6. Weight = 1.00 || counting = 0.000000
7. Weight = 1.00 || counting = 0.000000
8. Weight = 1.00 || counting = 0.000000
9. Weight = 0.50 || counting = 0.000000
10. Weight = 0.50 || counting = 0.000000
11. Weight = 0.50 || counting = 0.000000
12. Weight = 0.00 || counting = 0.000000
13. Weight = 0.00 || counting = 0.000000
14. Weight = 0.00 || counting = 0.000000
15. Weight = 0.00 || counting = 0.000000
16. Weight = 0.00 || counting = 0.000000
17. Weight = 0.00 || counting = 0.000000
18. Weight = 0.00 || counting = 0.000000
19. Weight = 0.00 || counting = 0.000000
20. Weight = 0.00 || counting = 0.000000
The GetObjectWeightFromInvID is working perfectly but why isn't it adding to counting?
Reply
#2

nvm I found an other method. But is there a reason why the old method didn't work?

PHP код:
Float:GetInventoryWeight(playerid)
{
    new 
Float:counting;
    for(new 
i=1MAX_SLOTSi++)
    {
        
counting += GetObjectWeightFromInvID(Player[playerid][InvSlots][i]);
        
printf("%i. Weight = %.2f || counting = %f"iGetObjectWeightFromInvID(Player[playerid][InvSlots][i]), counting);
    }
    return 
Float:counting;

Reply
#3

Because floatadd adds the two passed floats and returns its sum there is no change will occur to the vars that passed. https://sampwiki.blast.hk/wiki/Floatadd
Reply
#4

The + operator calls floatadd in the background when applied to floating point numbers. See float.inc for that. I don't know why anyone would prefer to use the function because it's longer to type and less straightforward.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)