SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 11/06/2019, 01:53 PM   #1
Blake95
Little Clucker
 
Join Date: Nov 2016
Posts: 9
Reputation: 2
Default 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!
Blake95 is offline   Reply With Quote
Old 11/06/2019, 03:54 PM   #2
Plastikmensch
Huge Clucker
 
Join Date: Aug 2016
Posts: 240
Reputation: 13
Default 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)

Last edited by Plastikmensch; 11/06/2019 at 03:54 PM. Reason: fixed typo
Plastikmensch is offline   Reply With Quote
Old 12/06/2019, 03:16 AM   #3
Blake95
Little Clucker
 
Join Date: Nov 2016
Posts: 9
Reputation: 2
Default 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;
}
Blake95 is offline   Reply With Quote
Old 12/06/2019, 04:46 AM   #4
Koreadars
Big Clucker
 
Join Date: Mar 2019
Location: Czech Republic, Prdelákov.
Posts: 128
Reputation: 6
Default Re: Price or item does not update

Remove the 'return 1;'.
Koreadars is offline   Reply With Quote
Old 12/06/2019, 06:36 AM   #5
Plastikmensch
Huge Clucker
 
Join Date: Aug 2016
Posts: 240
Reputation: 13
Default 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.
Plastikmensch is offline   Reply With Quote
Old 13/06/2019, 03:08 AM   #6
Blake95
Little Clucker
 
Join Date: Nov 2016
Posts: 9
Reputation: 2
Default Re: Price or item does not update

Doesn't work.. item not updated.. only 0..
I remove 'return 1;' don't work.
Any idea?
Blake95 is offline   Reply With Quote
Old 13/06/2019, 06:50 AM   #7
Koreadars
Big Clucker
 
Join Date: Mar 2019
Location: Czech Republic, Prdelákov.
Posts: 128
Reputation: 6
Default 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?
Koreadars is offline   Reply With Quote
Old 13/06/2019, 01:24 PM   #8
Blake95
Little Clucker
 
Join Date: Nov 2016
Posts: 9
Reputation: 2
Default 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);
	        }
Blake95 is offline   Reply With Quote
Old 13/06/2019, 03:38 PM   #9
Plastikmensch
Huge Clucker
 
Join Date: Aug 2016
Posts: 240
Reputation: 13
Default Re: Price or item does not update

Quote:
Originally Posted by Blake95 View Post
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 View Post
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?
Plastikmensch is offline   Reply With Quote
Old 14/06/2019, 02:58 AM   #10
Blake95
Little Clucker
 
Join Date: Nov 2016
Posts: 9
Reputation: 2
Default Re: Price or item does not update

I read you reply.. and i trying your idea maybe.. but doesn't work..
Blake95 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Price or item does not update 0x88 Scripting Help 2 18/10/2017 05:24 PM
Price Item Biz Help AlexPalermitano97 Scripting Help 4 11/03/2017 10:51 PM
bag system error, item can only replace the item on slop 0 Chrisli520 Scripting Help 0 28/11/2013 08:49 PM
Item selling for $1 and not specified price Phil_Cutcliffe Scripting Help 5 03/06/2013 07:17 PM
/sell [item] [amount] [price] iGetty Scripting Help 2 05/02/2012 09:23 PM


All times are GMT. The time now is 07:53 AM.


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