SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 04/02/2017, 05:10 AM   #3431
Hansrutger
Gangsta
 
Hansrutger's Avatar
 
Join Date: May 2011
Location: Sweden
Posts: 760
Reputation: 161
Default Re: Little coding questions - For general minor queries 5

Think of arrays as tables and rows. There are different types of arrays in terms of size, there is the most usual one as many say "normal" array but known as one dimensional array. There are also multi dimensional arrays. A one dimensional array has only 1 row in it filled with data including their titles, often said "index".

Example:
Code:
new pInfo[10];
This would be able to have data in spot 0, 1, 2, ..., 9. Their titles for every "spot" would be the numbers between 0 to 9, which is their index number.

As for multi dimensional arrays they are often used in SA-MP for pretending to have a "table" while running the script, making it a lot easier to handle data. Example:

While this might illustrate a slightly bad example, I like it because it was a good one I found on ******! Imagine the "Title" being different indexes, in this there would be 7 "titles" or "indexes". Therefore the first dimension should be 7. Then let's count the data's in one column EXCLUDING the title. We do not want to place something in the title cell, it's just there to display the index number! We have 7 there as well per column. In programing we would create this in array form with this syntax:
Code:
ArrayName[first_dimension][second_dimension];

In this example: ArrayName[7][7];
This will indicate that we have 7 titles and we have 7 cells to distribute data in, for EVERY title we have.



What enums actually does in most cases is just pretending to be names of "indexes". So for example here:
Code:
enum e_Index {
     INDEX_ZERO,
     INDEX_ONE,
     INDEX_TWO
}
pInfo[e_Index];

pInfo[INDEX_ZERO] == pInfo[0]
pInfo[INDEX_ONE] == pInfo[1]
pInfo[INDEX_TWO] == pInfo[2]
They are litterally the same thing, it just makes it easier to understand the code when reading it. This is what you most likely will be finding yourself using enums for in SA-MP, there are some other stuff you can use them for but for that you can probably just ****** search "enum".
__________________


Hansrutger is offline   Reply With Quote
Old 04/02/2017, 05:36 AM   #3432
princejeet1510
Big Clucker
 
princejeet1510's Avatar
 
Join Date: Mar 2016
Location: Trying to figure it out.
Posts: 119
Reputation: 4
Thumbs up Re: Little coding questions - For general minor queries 5

@Hansrutger
PHP Code:
Hatsoff to you man
+Rep;
The first ever time that bad example clear fucking every doubt I had......That was what actually I was looking for that what happened actually by defining those numbers but they noone other than similar to table and rows as you told...
Once again thanks alot man.
__________________
princejeet1510 is offline   Reply With Quote
Old 04/02/2017, 01:49 PM   #3433
Jeroen52
Gangsta
 
Jeroen52's Avatar
 
Join Date: Oct 2008
Location: The Netherlands
Posts: 882
Reputation: 74
Default Re: Little coding questions - For general minor queries 5

Quote:
Originally Posted by princejeet1510 View Post
@Hansrutger
PHP Code:
Hatsoff to you man
+Rep;
The first ever time that bad example clear fucking every doubt I had......That was what actually I was looking for that what happened actually by defining those numbers but they noone other than similar to table and rows as you told...
Once again thanks alot man.
And Arrays don't need to have a size written down if you're going to fill it with data on creation of the Array.
Here is an example.
PHP Code:
new var_VehicleNames[][] =
{
    
"Landstalker""Bravura""Buffalo""Linerunner""Perrenial""Sentinel""Dumper""Fire truck""Trashmaster",
    
"Stretch""Manana""Infernus""Voodoo""Pony""Mule""Cheetah""Ambulance""Leviathan""Moonbeam",
    
"Esperanto""Taxi""Washington""Bobcat""Whoopee""BF Injection""Hunter""Premier""Enforcer",
    
"Securicar""Banshee""Predator""Bus""Rhino""Barracks""Hotknife""Trailer""Previon""Coach",
    
"Cabbie""Stallion""Rumpo""RC Bandit""Romero""Packer""Monster""Admiral""Squalo""Seasparrow",
    
"Pizzaboy""Tram""Trailer""Turismo""Speeder""Reefer""Tropic""Flatbed""Yankee""Caddy""Solair",
    
"Berkley's RC Van""Skimmer""PCJ-600""Faggio""Freeway""RC Baron""RC Raider""Glendale""Oceanic",
    
"Sanchez""Sparrow""Patriot""Quad""Coastguard""Dinghy""Hermes""Sabre""Rustler""ZR-350""Walton",
    
"Regina""Comet""BMX""Burrito""Camper""Marquis""Baggage""Dozer""Maverick""News Chopper""Rancher",
    
"FBI Rancher""Virgo""Greenwood""Jetmax""Hotring""Sandking""Blista Compact""Police Maverick",
    
"Boxville""Benson""Mesa""RC Goblin""Hotring Racer A""Hotring Racer B""Bloodring Banger""Rancher",
    
"Super GT""Elegant""Journey""Bike""Mountain Bike""Beagle""Cropduster""Stunt""Tanker""Roadtrain",
    
"Nebula""Majestic""Buccaneer""Shamal""Hydra""FCR-900""NRG-500""HPV1000""Cement Truck""Tow Truck",
    
"Fortune""Cadrona""SWAT Truck""Willard""Forklift""Tractor""Combine""Feltzer""Remington""Slamvan",
    
"Blade""Streak""Freight""Vortex""Vincent""Bullet""Clover""Sadler""Firetruck""Hustler""Intruder",
    
"Primo""Cargobob""Tampa""Sunrise""Merit""Utility""Nevada""Yosemite""Windsor""Monster""Monster",
    
"Uranus""Jester""Sultan""Stratum""Elegy""Raindance""RC Tiger""Flash""Tahoma""Savanna""Bandito",
    
"Freight Flat""Streak Carriage""Kart""Mower""Dune""Sweeper""Broadway""Tornado""AT-400""DFT-30",
    
"Huntley""Stafford""BF-400""News Van""Tug""Trailer""Emperor""Wayfarer""Euros""Hotdog""Club",
    
"Freight Box""Trailer""Andromada""Dodo""RC Cam""Launch""LSPD Car""SFPD Car""LVPD Car",
    
"Police Rancher""Picador""Splashy""Alpha""Phoenix""Glendale""Sadler""Luggage""Luggage""Stairs",
    
"Boxville""Tiller""Utility Trailer"
}; 
So if you have that array, you can get a vehicle name with this function.
PHP Code:
stock GetVehicleModelName(modelID)
{
    new 
vehiclesName[25];
    if(
modelID >= 400 && modelID <= 612)
        
format(vehiclesNamesizeof(vehiclesName), "%s"var_VehicleNames[modelID 400]);
    return 
vehiclesName;

__________________
Jeroen52 is offline   Reply With Quote
Old 04/02/2017, 01:59 PM   #3434
princejeet1510
Big Clucker
 
princejeet1510's Avatar
 
Join Date: Mar 2016
Location: Trying to figure it out.
Posts: 119
Reputation: 4
Default Re: Little coding questions - For general minor queries 5

Quote:
Originally Posted by Jeroen52 View Post
And Arrays don't need to have a size written down if you're going to fill it with data on creation of the Array.
Here is an example.
PHP Code:
new var_VehicleNames[][] =
{
    
"Landstalker""Bravura""Buffalo""Linerunner""Perrenial""Sentinel""Dumper""Fire truck""Trashmaster",
    
"Stretch""Manana""Infernus""Voodoo""Pony""Mule""Cheetah""Ambulance""Leviathan""Moonbeam",
    
"Esperanto""Taxi""Washington""Bobcat""Whoopee""BF Injection""Hunter""Premier""Enforcer",
    
"Securicar""Banshee""Predator""Bus""Rhino""Barracks""Hotknife""Trailer""Previon""Coach",
    
"Cabbie""Stallion""Rumpo""RC Bandit""Romero""Packer""Monster""Admiral""Squalo""Seasparrow",
    
"Pizzaboy""Tram""Trailer""Turismo""Speeder""Reefer""Tropic""Flatbed""Yankee""Caddy""Solair",
    
"Berkley's RC Van""Skimmer""PCJ-600""Faggio""Freeway""RC Baron""RC Raider""Glendale""Oceanic",
    
"Sanchez""Sparrow""Patriot""Quad""Coastguard""Dinghy""Hermes""Sabre""Rustler""ZR-350""Walton",
    
"Regina""Comet""BMX""Burrito""Camper""Marquis""Baggage""Dozer""Maverick""News Chopper""Rancher",
    
"FBI Rancher""Virgo""Greenwood""Jetmax""Hotring""Sandking""Blista Compact""Police Maverick",
    
"Boxville""Benson""Mesa""RC Goblin""Hotring Racer A""Hotring Racer B""Bloodring Banger""Rancher",
    
"Super GT""Elegant""Journey""Bike""Mountain Bike""Beagle""Cropduster""Stunt""Tanker""Roadtrain",
    
"Nebula""Majestic""Buccaneer""Shamal""Hydra""FCR-900""NRG-500""HPV1000""Cement Truck""Tow Truck",
    
"Fortune""Cadrona""SWAT Truck""Willard""Forklift""Tractor""Combine""Feltzer""Remington""Slamvan",
    
"Blade""Streak""Freight""Vortex""Vincent""Bullet""Clover""Sadler""Firetruck""Hustler""Intruder",
    
"Primo""Cargobob""Tampa""Sunrise""Merit""Utility""Nevada""Yosemite""Windsor""Monster""Monster",
    
"Uranus""Jester""Sultan""Stratum""Elegy""Raindance""RC Tiger""Flash""Tahoma""Savanna""Bandito",
    
"Freight Flat""Streak Carriage""Kart""Mower""Dune""Sweeper""Broadway""Tornado""AT-400""DFT-30",
    
"Huntley""Stafford""BF-400""News Van""Tug""Trailer""Emperor""Wayfarer""Euros""Hotdog""Club",
    
"Freight Box""Trailer""Andromada""Dodo""RC Cam""Launch""LSPD Car""SFPD Car""LVPD Car",
    
"Police Rancher""Picador""Splashy""Alpha""Phoenix""Glendale""Sadler""Luggage""Luggage""Stairs",
    
"Boxville""Tiller""Utility Trailer"
}; 
So if you have that array, you can get a vehicle name with this function.
PHP Code:
stock GetVehicleModelName(modelID)
{
    new 
vehiclesName[25];
    if(
modelID >= 400 && modelID <= 612)
        
format(vehiclesNamesizeof(vehiclesName), "%s"var_VehicleNames[modelID 400]);
    return 
vehiclesName;

Ohh okay....But why like if I want to using MySQL make a query it will be like this
PHP Code:
new query [126]; 
Why they need to be given those values ?? And what are limits for them...
__________________
princejeet1510 is offline   Reply With Quote
Old 04/02/2017, 02:07 PM   #3435
Jeroen52
Gangsta
 
Jeroen52's Avatar
 
Join Date: Oct 2008
Location: The Netherlands
Posts: 882
Reputation: 74
Default Re: Little coding questions - For general minor queries 5

Quote:
Originally Posted by princejeet1510 View Post
Ohh okay....But why like if I want to using MySQL make a query it will be like this
PHP Code:
new query [126]; 
Why they need to be given those values ?? And what are limits for them...
Because if you use MySQL it returns strings/arrays, and you need a place to store the information.
You can also process them into integers, depending on the datatype of the MySQL row returned.

And the limit for query is 125 characters.
__________________
Jeroen52 is offline   Reply With Quote
Old 04/02/2017, 02:13 PM   #3436
princejeet1510
Big Clucker
 
princejeet1510's Avatar
 
Join Date: Mar 2016
Location: Trying to figure it out.
Posts: 119
Reputation: 4
Default Re: Little coding questions - For general minor queries 5

Thanks....
+1 rep;
__________________
princejeet1510 is offline   Reply With Quote
Old 04/02/2017, 02:13 PM   #3437
Hansrutger
Gangsta
 
Hansrutger's Avatar
 
Join Date: May 2011
Location: Sweden
Posts: 760
Reputation: 161
Default Re: Little coding questions - For general minor queries 5

The limit isn't from MySQL but rather from pawn. My script usually dies (process stops working) if I use an array over 2048 size which isn't that odd. Anyhow, if you really want to create a big array, go with 2048 as size for the string.

Why they need to have 126? They don't really have to, it's just that most people setup strings like that, although they usually use 128 instead of 126 . Only thing that it tells is how long your string will be, and with string I mean char array.

Remember the one-dimensional row? Well strings are one-dimensional character arrays in pawn.
Code:
new string[5] = "Hello";

string[0] == 'H'
string[1] == 'e'
string[2] == 'l'
string[3] == 'l'
string[4] == 'o'
It's basically a table (or well a row) with cell values, just like any other array. Bottom line is that you should only assign the length of an array, of which you think you will use. So if you are just going to create a simple UPDATE with one value changing (example below) then it shouldn't be a too long array, mostly I use 128 for those.
Code:
new query[128];
format(query, sizeof(query), "UPDATE some_table SET some_column_name = %i", some_integer);
However if I want to update the WHOLE table on one row, I would need a bigger array, perhaps even run 2 MySQL queries depending on how many columns you want to update. Usually the biggest table you will have in terms of columns is your main account holder, you know pInfo[playerid][pAge] or whatever you use for instance, those will usually be the biggest ones so it's probably wise to go with the highest value if you're not sure how big it is, even dividing it into two queries as I suggested above.

The point of assigning smaller sized arrays is that, you wouldn't want to fill your room with lots of space but not filling it with items right? Same thing with arrays, you don't want to create more "spots" (indexes) than it has to be, sure it's still nice to have some space left over in the room, same in the array just to be sure.

EDIT: Sorry I started typing when there was no response.

EDIT2: Just read the above response, the limit is not 125 lmao.
__________________


Hansrutger is offline   Reply With Quote
Old 04/02/2017, 02:25 PM   #3438
Spmn
Gangsta
 
Join Date: Jun 2015
Location: Romania
Posts: 486
Reputation: 131
Default Re: Little coding questions - For general minor queries 5

Quote:
Originally Posted by Hansrutger View Post
Code:
new string[5] = "Hello";

string[0] == 'H'
string[1] == 'e'
string[2] == 'l'
string[3] == 'l'
string[4] == 'o'
You forgot about null terminator.
Spmn is offline   Reply With Quote
Old 04/02/2017, 02:28 PM   #3439
Lordzy
High-roller
 
Lordzy's Avatar
 
Join Date: Mar 2012
Location: NetherRealm
Posts: 2,698
Reputation: 1177
Default Re: Little coding questions - For general minor queries 5

Quote:
Originally Posted by Hansrutger View Post
Remember the one-dimensional row? Well strings are one-dimensional character arrays in pawn.
Code:
new string[5] = "Hello";

string[0] == 'H'
string[1] == 'e'
string[2] == 'l'
string[3] == 'l'
string[4] == 'o'
This won't work because "Hello" has a word-length of 5 and strings end with a NULL character. So 5+1=6 should be the string size of your string.
pawn Code:
new string[6] = "Hello";
'H', 'e', 'l', 'l', 'o', '\0'

EDIT : Late.

About update queries : I usually use parallel queries (mysql_pquery()) for updating my tables. Since no data has to be retrieved from such a query, different pools can be used.
__________________
Currently inactive - I don't play at any SA-MP servers nor work on anything in PAWN for now. The projects that I've done so far in PAWN, which requires updates will be taking some time.
Lordzy is offline   Reply With Quote
Old 04/02/2017, 02:33 PM   #3440
princejeet1510
Big Clucker
 
princejeet1510's Avatar
 
Join Date: Mar 2016
Location: Trying to figure it out.
Posts: 119
Reputation: 4
Default Re: Little coding questions - For general minor queries 5

So an array for sure has to have one null character...?
__________________
princejeet1510 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
how to make a soldier aotumaticly be general when the general dies ? AlbanianGuy Scripting Help 35 13/05/2012 03:35 PM
Minor problem Desert Help Archive 4 17/03/2010 06:24 PM
Counting with mysql queries actiwe Help Archive 2 26/02/2010 02:36 PM
SQL queries using GROUP BY Dabombber Help Archive 1 05/10/2009 07:47 PM


All times are GMT. The time now is 12:08 PM.


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