SA-MP Forums

SA-MP Forums (https://forum.sa-mp.com/index.php)
-   Scripting Help (https://forum.sa-mp.com/forumdisplay.php?f=12)
-   -   Price or item does not update (https://forum.sa-mp.com/showthread.php?t=667174)

Blake95 11/06/2019 01:53 PM

Price or item does not update
 
This is a item purchase script I'm going to do, when I click on item and it will be added into your cart. ItemCache has ten slots which is max_cart_item but when I click on them, it does not add to your list.


Code:

if(GetPVarInt(playerid, "UI_Purchase"))
        {
            new index_id = PlayerCache_GetFree(playerid);
            for(new x = 0; x < 10; x ++)
            {
                if(clickedid == Store_UI[x])
                {
                    if(PlayerCache_GetFree(playerid))
                    {
                            PlayerInfo[playerid][ItemCache][index_id] = x;
                            SetPVarInt(playerid, "PriceCount", GetPVarInt(playerid, "PriceCount") + ReturnItemPrice(x));
                                        if(PlayerInfo[playerid][ItemCache][x])
                                        {
                                            if(GetPVarInt(playerid, "PriceCount") - ReturnItemPrice(PlayerInfo[playerid][ItemCache][index_id]) > 0)
                                            {
                                                    SetPVarInt(playerid, "PriceCount", GetPVarInt(playerid, "PriceCount") - ReturnItemPrice(PlayerInfo[playerid][ItemCache][index_id]));
                                                }
                                                else
                                                {
                                                    SetPVarInt(playerid, "PriceCount", 0);
                                                }
                                                PlayerInfo[playerid][ItemCache][index_id] = -1;
                                        }
                                }
                                new price[24];
                                format(price, 24, "~g~$%s", MoneyFormat(GetPVarInt(playerid, "PriceCount")));
                                PlayerTextDrawSetString(playerid, Store_Cart[playerid], price);
                }
            }


Code:

stock PlayerCache_GetFree(playerid)
{
        for(new i = 0; i < 10; i ++)
        {
                if(!PlayerInfo[playerid][ItemCache][i])
                        return i+1;
        }
        return -1;
}


Code:

[debug] Run time error 4: "Array index out of bounds"
[debug]  Attempted to read/write array element at negative index -1
[debug] AMX backtrace:
[debug] #0 000bbbc4 in public OnPlayerClickTextDraw (0, 17) in csrp.amx

HELP ME! Thank you!

Plastikmensch 11/06/2019 03:54 PM

Re: Price or item does not update
 
So, what happens there is that
Code:

if(PlayerCache_GetFree(playerid))
is always true if return value is not 0.
You can confirm that by doing this in OnGameModeInit:
Code:

new test = -1;
if(test) print("test true");
//will print "test true" into console.

You should check for the return value if not 0 or 1:
Code:

if(PlayerCache_GetFree(playerid)!=-1)

Blake95 12/06/2019 03:16 AM

Re: Price or item does not update
 
Thank you! Error is solved.. but item doesn't update.. I click the item and "pricecount" not update.. always 0..

Code:

stock ReturnItemPrice(itemid)
{
        switch(itemid)
        {
            case 0:
                        return 500;
            case 1:
                return 10000;
            case 2:
                return 1500;
            case 3:
                    return 500;
            case 4:
                    return 200;
                case 5:
                    return 500;
                case 6:
                    return 5000;
                case 7:
                    return 200;
                case 8:
                    return 500;
                case 9:
                    return 3000;
        }
        return 1;
}


Koreadars 12/06/2019 04:46 AM

Re: Price or item does not update
 
Remove the 'return 1;'.

Plastikmensch 12/06/2019 06:36 AM

Re: Price or item does not update
 
Might be bad math. If I understand your code correctly, you check if PriceCount - ItemPrice is greater than 0.
Code:

if(PlayerInfo[playerid][ItemCache][x])
                                        {
                                            if(GetPVarInt(playerid, "PriceCount") - ReturnItemPrice(PlayerInfo[playerid][ItemCache][index_id]) > 0)//here
                                            {
                                                    SetPVarInt(playerid, "PriceCount", GetPVarInt(playerid, "PriceCount") - ReturnItemPrice(PlayerInfo[playerid][ItemCache][index_id]));//Also you might want to to + instead of - here
                                                }
                                                else
                                                {
                                                    SetPVarInt(playerid, "PriceCount", 0);
                                                }
                                                PlayerInfo[playerid][ItemCache][index_id] = -1;
                                        }

But if you select the first item, PriceCount - ItemPrice will be 0 which will execute the else and set PriceCount to 0.
Assuming the PriceCount is the total price of all items, you might want to add and not subtract from it.

The ReturnItemPrice function is fine except you might don't want to return 1 if itemid is not inside switch.

Blake95 13/06/2019 03:08 AM

Re: Price or item does not update
 
Doesn't work.. item not updated.. only 0.. :(
I remove 'return 1;' don't work.
Any idea?

Koreadars 13/06/2019 06:50 AM

Re: Price or item does not update
 
pawn Code:
ReturnItemPrice(itemid)
{
    new rtnval;
    switch(itemid)
    {
        case 0: rtnval = 500;
        case 1: rtnval = 10000;
        case 2: rtnval = 1500;
        case 3: rtnval = 500;
        case 4: rtnval = 200;
        case 5: rtnval = 500;
        case 6: rtnval = 5000;
        case 7: rtnval = 200;
        case 8: rtnval = 500;
        case 9: rtnval = 3000;
    }
    return rtnval;
}
Works?

Blake95 13/06/2019 01:24 PM

Re: Price or item does not update
 
Doesn't work! :(

Maybe bad code...

Code:

if(PlayerCache_GetFree(playerid)!= -1)
                    {
                            PlayerInfo[playerid][ItemCache][index_id] = x;
                            SetPVarInt(playerid, "PriceCount", GetPVarInt(playerid, "PriceCount") + ReturnItemPrice(x));
                                       
                                        if(PlayerInfo[playerid][ItemCache][x])
                                        {
                                            if(GetPVarInt(playerid, "PriceCount") - ReturnItemPrice(PlayerInfo[playerid][ItemCache][index_id]) > 0)//here
                                            {
                                                    SetPVarInt(playerid, "PriceCount", GetPVarInt(playerid, "PriceCount") - ReturnItemPrice(PlayerInfo[playerid][ItemCache][index_id]));//Also you might want to to + instead of - here
                                                }
                                                else
                                                {
                                                    SetPVarInt(playerid, "PriceCount", 0);
                                                }
                                                PlayerInfo[playerid][ItemCache][index_id] = -1;
                                        }
                                }
                                new price[24];
                                format(price, 24, "~g~$%s", MoneyFormat(GetPVarInt(playerid, "PriceCount")));
                                PlayerTextDrawSetString(playerid, Store_Cart[playerid], price);
                }


SiaReyes 13/06/2019 02:57 PM

Re: Price or item does not update
 
Maybe it should be like

Code:

SetPVarInt(playerid, "PriceCount",ReturnItemPrice(x));

Plastikmensch 13/06/2019 03:38 PM

Re: Price or item does not update
 
Quote:

Originally Posted by Blake95 (Post 4102080)
Doesn't work! :(

Maybe bad code...

Code:

if(PlayerCache_GetFree(playerid)!= -1)
                    {
                            PlayerInfo[playerid][ItemCache][index_id] = x;
                            SetPVarInt(playerid, "PriceCount", GetPVarInt(playerid, "PriceCount") + ReturnItemPrice(x));
                                       
                                        if(PlayerInfo[playerid][ItemCache][x])
                                        {
                                            if(GetPVarInt(playerid, "PriceCount") - ReturnItemPrice(PlayerInfo[playerid][ItemCache][index_id]) > 0)//here
                                            {
                                                    SetPVarInt(playerid, "PriceCount", GetPVarInt(playerid, "PriceCount") - ReturnItemPrice(PlayerInfo[playerid][ItemCache][index_id]));//Also you might want to to + instead of - here
                                                }
                                                else
                                                {
                                                    SetPVarInt(playerid, "PriceCount", 0);
                                                }
                                                PlayerInfo[playerid][ItemCache][index_id] = -1;
                                        }
                                }
                                new price[24];
                                format(price, 24, "~g~$%s", MoneyFormat(GetPVarInt(playerid, "PriceCount")));
                                PlayerTextDrawSetString(playerid, Store_Cart[playerid], price);
                }


Did you read my reply?

Quote:

Originally Posted by Plastikmensch (Post 4101942)
Might be bad math. If I understand your code correctly, you check if PriceCount - ItemPrice is greater than 0.
Code:

if(PlayerInfo[playerid][ItemCache][x])
{
  if(GetPVarInt(playerid, "PriceCount") - ReturnItemPrice(PlayerInfo[playerid][ItemCache][index_id]) > 0)//here
  {
      SetPVarInt(playerid, "PriceCount", GetPVarInt(playerid, "PriceCount") - ReturnItemPrice(PlayerInfo[playerid][ItemCache][index_id]));//Also you might want to to + instead of - here
  }
  else
  {
      SetPVarInt(playerid, "PriceCount", 0);
  }
  PlayerInfo[playerid][ItemCache][index_id] = -1;
}

But if you select the first item, PriceCount - ItemPrice will be 0 which will execute the else and set PriceCount to 0.
Assuming the PriceCount is the total price of all items, you might want to add and not subtract from it.

The ReturnItemPrice function is fine except you might don't want to return 1 if itemid is not inside switch.

Also the code in my reply wasn't fixed code. I quoted your code to show you where the problems are, hence the "//here" and "//You might want to do + instead of - here"

Also I don't quite understand, what you are trying to do.
You add ItemPrice to PriceCount, check if PriceCount - ItemPrice is greater than 0 which it will never be on the first item, so you have unreachable code, where you are subtracting from PriceCount again (which would be 0 again) and in the else, which gets executed, you set PriceCount to 0 and then show PriceCount to the player with a textdraw?


All times are GMT. The time now is 04:50 AM.

Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.