Counting float - Printable Version
+- SA-MP Forums Archive (
https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (
https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (
https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Counting float (
/showthread.php?tid=624645)
Counting float -
PeanutButter - 20.12.2016
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=1; i < MAX_SLOTS; i++)
{
floatadd(counting, GetObjectWeightFromInvID(Player[playerid][InvSlots][i]));
printf("%i. Weight = %.2f || counting = %f", i, GetObjectWeightFromInvID(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?
Re: Counting float -
PeanutButter - 20.12.2016
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=1; i < MAX_SLOTS; i++)
{
counting += GetObjectWeightFromInvID(Player[playerid][InvSlots][i]);
printf("%i. Weight = %.2f || counting = %f", i, GetObjectWeightFromInvID(Player[playerid][InvSlots][i]), counting);
}
return Float:counting;
}
Re: Counting float -
SyS - 20.12.2016
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
Re: Counting float -
Vince - 20.12.2016
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.