Is there a way to optimize this code?
#10

Quote:
Originally Posted by xVIP3Rx
Посмотреть сообщение
He is trying to say don't use strings at all, Use implemented functions to extract the integer and fraction, This "probably, I'm not sure" should be faster than using a string

Explained as much as I can below
PHP код:
_givePlayerMoney(playeridFloat:amount)
{
    
ResetPlayerMoney(playerid);
    
character[playerid][cash] += floatround(amountfloatround_floor);                    //Gets the integer part of a float(Rounds downwards, Ignoring fractions basically)
    
character[playerid][coin] += floatround(floatfract(amount)*100floatround_floor);    //Gets the fraction part of the float, multiply it by a hundred to get the first two fractions (You will have to change that if you use more or less than two fractions, or adjust the code) then use the method above to convert it to an integer
    
    
SetPlayerMoney(playeridcharacter[playerid][cash]);
    
updatePlayerCoinTextDraw(playerid);
    return 
1;


Having variables is fine, as long as you're using them right, Variables don't take much space or time to make anyways, And sometimes they helps clear things out even if they're unnecessary.

an example for the above code with variables:
PHP код:
_givePlayerMoney(playeridFloat:amount)
{
    
ResetPlayerMoney(playerid);
    new 
cash floatround(amountfloatround_floor);
    new 
coin floatround(floatfract(amount)*100floatround_floor);
    
character[playerid][cash] += cash;
    
character[playerid][coin] += coin;
    
SetPlayerMoney(playeridcharacter[playerid][cash]);
    
updatePlayerCoinTextDraw(playerid);
    return 
1;

I understood. You really help me out! Thank you.

Quote:
Originally Posted by AmigaBlizzard
Посмотреть сообщение
You don't have to use 2 separate arrays to store this.
When you give the player some money like $450, give him 450 * 100 = 45000.
Then you'll be storing his money in cents instead of whole dollars.

To extract both values (the dollars and cents), you can use it like this:
Код:
	new money, dollars, cents;
	money = 5062; // This is in fact 50 dollars and 62 cents
	dollars = money / 100;
	cents = money - (dollars * 100);

	printf("Money: %i", money);
	printf("Dollars: %i", dollars);
	printf("Cents: %i", cents);
Due to how integers work, dividing the total money in cents by 100, it rounds the result down automatically.
In this example, the dollars variable will hold "50" (5062 / 100 is essentially 50.62 but with integers, the part behind the comma drops away, leaving you with just the value "50").
Then you take the difference between the whole amount of cents and the rounded down value of dollars multiplied by 100.
This will become 5062 - (50 * 100) = 5062 - 5000 = 62.
So with only 1 value, you can store both values and separate them easily.

The only drawback to this: you are only able to store money up to 21 million dollars instead of 2.1 billion dollars due to having a signed 32-bit integer value and using the last 2 digits to store your cents instead of whole dollars.
I would follow your way if I could but a player can have more than 99 cents so your way may not work in some cases. Thanks anyway!
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)