SA-MP Forums Archive
Array problem - 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: Array problem (/showthread.php?tid=609705)



Array problem - TwinkiDaBoss - 15.06.2016

Alright so the problem is quite hard to explain.

I have this system where I can dynamicly add stuff to a shop. Now it works perfectly besides 1 problem, it will constantly return weapon 0 and price 0. Well basically everything as 0.

PHP Code:
case DIALOG_WEAPON_PURCHASE: {
            if(!
response) return true;
             for(new 
i=0MAX_SHOPSi++) {
                 new 
ShopInfo[i][ShopID];
                if(
GetPlayerMoney(playerid) < ShopData[z][listitem][ContentPrice]) return SCM(playerid,COLOR_RED,"You dont have enough money. You need %i to buy this",ShopData[i][listitem][ContentPrice]);
                  if(
IsPlayerInRangeOfPoint(playerid,5.0,ShopInfo[i][ShopX],ShopInfo[i][ShopY],ShopInfo[i][ShopZ])) {
                    
GivePlayerWeapon(playerid,ShopData[z][listitem][ContentWeapon],ShopData[z][listitem][ContentAmmo]);
                    
SCM(playerid,COLOR_RED,"You have purchased weapon ID: %i for %i$",ShopData[z][listitem][ContentWeapon],ShopData[z][listitem][ContentPrice]);
                    
GivePlayerMoney(playerid,ShopData[z][listitem][ContentPrice]);
                }
            }
        } 
Now to understand my code, look at this.
This is how i show them whats in the shop.

Every item is linked to certain shop ID (this part of the code works, the one above doesnt!)

PHP Code:
YCMD:buy(playerid,params[],help) {
    new 
string[50], mainstring[1024], resultsfound 0gunname[32];
    for(new 
i=0MAX_SHOPSi++) {
        if(
IsPlayerInRangeOfPoint(playerid,5.0,ShopInfo[i][ShopX],ShopInfo[i][ShopY],ShopInfo[i][ShopZ])) {
            for(new 
x=0MAX_CONTENT_DATAx++) {
                new 
ShopInfo[i][ShopID];
                if(
ShopData[z][x][ContentID] == ShopInfo[i][ShopID]) {
                    
GetWeaponName(ShopData[z][x][ContentWeapon],gunname,sizeof(gunname));
                    
format(string,sizeof(string),"%s\t%i\t%i$\n",gunname,ShopData[z][x][ContentAmmo],ShopData[z][x][ContentPrice]);
                    
strcat(mainstring,string);
                    
resultsfound += 1;
                }
            }
        }
    }
    if(
resultsfound == 0) return SendClientMessage(playerid,COLOR_RED,"No weapons are found for this shop");
    
    new 
finalstring[1100];
    
format(finalstring,sizeof(finalstring),"Weapon Name\t Ammo\t Price\n %s",mainstring);
    
ShowPlayerDialog(playerid,DIALOG_WEAPON_PURCHASEDIALOG_STYLE_TABLIST_HEADERS,"Pick a weapon to buy",finalstring,"Buy","Cancel");
    return 
true;

Basically my enum is like this

new ShopData[MAX_SHOPS][MAX_CONTENT_DATA][CON_DATA];

MAX SHOPS - bound to certain shop ID
MAX_CONTENT_DATA its own ID.

For example, loading from sql that also works
PHP Code:
public OnContentLoad() {
    new 
rows cache_num_rows();
    
    for(new 
i=0rowsi++) {
        new 
id cache_get_field_content_int(i,"ID");
        
ShopData[id][i][ContentKey] = cache_get_field_content_int(i,"Unique"); //unique Key ID
        
ShopData[id][i][ContentWeapon] = cache_get_field_content_int(i,"WeaponID");
        
ShopData[id][i][ContentID] = id;
        
ShopData[id][i][ContentPrice] = cache_get_field_content_int(i,"Price");
        
ShopData[id][i][ContentAmmo] = cache_get_field_content_int(i,"Ammo");
        
        
printf("Shop ID: %i :: Content linked ID: %i :: Content Unique ID: %i",id,ShopData[id][i][ContentID],ShopData[id][i][ContentKey]);
    }




Re: Array problem - Konstantinos - 15.06.2016

Since content data are mixed up you need to count every time an item from that shop appears and if it equals the listitem, then give the weapon etc.

PHP Code:
if (!response) return true;
new 
count_listitem = -1z;
for (new 
0MAX_SHOPSi++)
{
    if (
IsPlayerInRangeOfPoint(playerid5.0ShopInfo[i][ShopX], ShopInfo[i][ShopY], ShopInfo[i][ShopZ]))
    {
        for (new 
0MAX_CONTENT_DATAx++)
        {
            
ShopInfo[i][ShopID];
            if (
ShopData[z][x][ContentID] == ShopInfo[i][ShopID])
            {
                if (++
count_listitem == listitem)
                {
                    if (
GetPlayerMoney(playerid) < ShopData[z][listitem][ContentPrice]) return SCM(playeridCOLOR_RED"You dont have enough money. You need %i to buy this"ShopData[i][listitem][ContentPrice]);
                    
GivePlayerWeapon(playeridShopData[z][listitem][ContentWeapon], ShopData[z][listitem][ContentAmmo]);
                    
SCM(playeridCOLOR_RED"You have purchased weapon ID: %i for %i$"ShopData[z][listitem][ContentWeapon], ShopData[z][listitem][ContentPrice]);
                    
GivePlayerMoney(playeridShopData[z][listitem][ContentPrice]);
                    break;
                }
            }
        }
    }

Unfortunately, you'll have to loop again and check which shop it is. A per-player variable would be useful I guess if there are many shops.

EDIT: Added money check, forgot it.


Re: Array problem - TwinkiDaBoss - 15.06.2016

Testing

Edit: Well I jsut tested it, the problem is, it will just work out for shop ID:1, the rest will simply be returned as price 0 and weapon ID:0


Re: Array problem - Konstantinos - 15.06.2016

The only way to identify the problem is debugging. Print the values when using /buy command and when responding to the dialog and see the outputs.


Re: Array problem - TwinkiDaBoss - 15.06.2016

Yeah sorry forgot to debug.

I did some now and these are the results. The shop ID is correct but as you can see content ID is still 0
PHP Code:
[21:55:05Weapon ID:: Weapon Shop ID:: Content ID0
[21:55:07Weapon ID:: Weapon Shop ID:: Content ID0
[21:55:22Weapon ID:: Weapon Shop ID:: Content ID0
[21:55:23Weapon ID:: Weapon Shop ID:: Content ID
PHP Code:
printf("Weapon ID: %i :: Weapon Shop ID: %i :: Content ID: %i",ShopData[z][listitem][ContentWeapon],i,ShopData[z][listitem][ContentID]); 



Re: Array problem - Konstantinos - 15.06.2016

I meant more of excessive debugging like:
PHP Code:
YCMD:buy(playeridparams[], help)
{
    new 
string[50], mainstring[1024], resultsfound 0gunname[32];
    for (new 
0MAX_SHOPSi++)
    {
        
printf("DEBUG: /buy -> i: %i"i);
        if (
IsPlayerInRangeOfPoint(playerid5.0ShopInfo[i][ShopX], ShopInfo[i][ShopY], ShopInfo[i][ShopZ]))
        {
            
printf("DEBUG: /buy -> in range: %i"i);
            for (new 
0MAX_CONTENT_DATAx++)
            {
                new 
ShopInfo[i][ShopID];
                
printf("DEBUG: /buy -> x: %i & z: %i & ContentID: %i"xzShopData[z][x][ContentID]);
                if (
ShopData[z][x][ContentID] == ShopInfo[i][ShopID])
                {
                    
GetWeaponName(ShopData[z][x][ContentWeapon], gunnamesizeof(gunname));
                    
format(stringsizeof(string), "%s\t%i\t%i$\n"gunnameShopData[z][x][ContentAmmo], ShopData[z][x][ContentPrice]);
                    
strcat(mainstringstring);
                    
resultsfound += 1;
                }
            }
        }
    }
    if (
resultsfound == 0) return SendClientMessage(playeridCOLOR_RED"No weapons are found for this shop");
    
printf("DEBUG: /buy -> resultsfound: %i"resultsfound);
    new 
finalstring[1100];
    
format(finalstringsizeof(finalstring), "Weapon Name\t Ammo\t Price\n %s"mainstring);
    
ShowPlayerDialog(playeridDIALOG_WEAPON_PURCHASEDIALOG_STYLE_TABLIST_HEADERS"Pick a weapon to buy"finalstring"Buy""Cancel");
    return 
true;

Do the same for the dialog.


Re: Array problem - TwinkiDaBoss - 15.06.2016

Ahhhh, I did something else and finally got it working, I simply added

PHP Code:
#define MAX_CONTENT_HOLD 255
new Item_CurrentlyBrowing[MAX_PLAYERS][MAX_CONTENT_HOLD],
    
Price_CurrentlyBrowing[MAX_PLAYERS][MAX_CONTENT_HOLD],
    
Ammo_CurrentlyBrowing[MAX_PLAYERS][MAX_CONTENT_HOLD];
Item_CurrentlyBrowing[playerid][resultsfound] = ShopData[z][x][ContentWeapon];
                     
Price_CurrentlyBrowing[playerid][resultsfound] = ShopData[z][x][ContentAmmo];
                     
Ammo_CurrentlyBrowing[playerid][resultsfound] = ShopData[z][x][ContentPrice];
                    
resultsfound += 1
And then
PHP Code:
GivePlayerWeapon(playerid,Item_CurrentlyBrowing[playerid][listitem],Ammo_CurrentlyBrowing[playerid][listitem]);
              
GivePlayerMoney(playerid,-Price_CurrentlyBrowing[playerid][listitem]);
              
              new 
string[128];
              
format(string,sizeof(string),"Weapon ID: %i :: Price; %i$ :: Ammo: %i",Item_CurrentlyBrowing[playerid][listitem],Ammo_CurrentlyBrowing[playerid][listitem],Price_CurrentlyBrowing[playerid][listitem]);
            
SendClientMessage(playerid,COLOR_RED,string); 
And it seems to work!


Quote:

A per-player variable would be useful I guess if there are many shops.

Thanks for the suggestion bro! I highly appreciate it and thanks for all the help!