Is there a way to optimize this code?
#9

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;

Quote:
Originally Posted by DuyDang2412
Посмотреть сообщение
I write the code just to see if it works. But it seems long because i create a lot of variables (i don't know if creating a lot of variables like above is needed?) and there're too much steps (strfind, strmid and strval) just to split one short string.


Thank you, this looks shorter (and less variables and may be faster?) than mine.
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;

Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 3 Guest(s)