SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 18/11/2019, 08:43 PM   #1
bookknp
Huge Clucker
 
bookknp's Avatar
 
Join Date: Dec 2013
Posts: 241
Reputation: 24
Question I have a question

Hi guys, I'm trying to make a "for" use all the elements of an array, could someone tell me how? +rep

Example:

PHP Code:
new BotMarasID[3][] = {
    {
"bot62"},
    {
"bot63"},
    {
"bot64"}
    };

for(new 
0sizeof(BotMarasID); i++) 
bookknp is offline   Reply With Quote
Old 18/11/2019, 09:08 PM   #2
Kwarde
High-roller
 
Kwarde's Avatar
 
Join Date: Nov 2009
Location: The Netherlands
Posts: 2,634
Reputation: 1653
Default Re: I have a question

First of all: You don't need to define the array size if it has (an) initial value(s) -which you have:
Code:
new BotMarasID[][] = {
    {"bot62"},
    {"bot63"},
    {"bot64"}
};
The compiler will automatically set the size (in this case, [3][5]). It's not bad to define the size yourself but I personally prefer not doing it if it has an initial value already. -If you'd want to add more later you'd have to increase the size manually ofc.
If you're not planning to modify the array you may want to define it as a constant:
Code:
new const BotMarasID[][] = {...}
.
By defining it as a constant (const) you tell the compiler it may not be modified. If you'd do this it'd return an error. It is handy because it decreases the chance of mistakes. Anyway..

A for loop runs as long at the statement is true: (just like with while() loops):
Code:
for (new i; i < sizeof(BotMarasID); i++) //new i; < No need to use '= 0' because not defining a value will always automatically be 0. Another small thing tho.
//This statement runs as long 'i' is below 'sizeof(BotMarasID)' (which is 3).
//Every loop i gets increased by one (i++)
You can use 'i' to access the array:

Code:
BotMarasID[i]
.

If you'd want to print the names:

Code:
for (new i; i < sizeof(BotMarasID); i++)
    printf("Index %d: %s", i, BotMarasID[i]);
This would print:

Index 0: bot62
Index 1: bot63
Index 2: bot64

If I understood your question correctly (how to use all elements of an array?) -You gave the correct code for that already
__________________
Do not click on this URL -It is secret information
RIP Slayer 1981-2019 \m/
Discord: Kwarde#1991
Kwarde is offline   Reply With Quote
Old 18/11/2019, 09:34 PM   #3
bookknp
Huge Clucker
 
bookknp's Avatar
 
Join Date: Dec 2013
Posts: 241
Reputation: 24
Default Re: I have a question

Quote:
Originally Posted by Kwarde View Post
First of all: You don't need to define the array size if it has (an) initial value(s) -which you have:
Code:
new BotMarasID[][] = {
    {"bot62"},
    {"bot63"},
    {"bot64"}
};
The compiler will automatically set the size (in this case, [3][5]). It's not bad to define the size yourself but I personally prefer not doing it if it has an initial value already. -If you'd want to add more later you'd have to increase the size manually ofc.
If you're not planning to modify the array you may want to define it as a constant:
Code:
new const BotMarasID[][] = {...}
.
By defining it as a constant (const) you tell the compiler it may not be modified. If you'd do this it'd return an error. It is handy because it decreases the chance of mistakes. Anyway..

A for loop runs as long at the statement is true: (just like with while() loops):
Code:
for (new i; i < sizeof(BotMarasID); i++) //new i; < No need to use '= 0' because not defining a value will always automatically be 0. Another small thing tho.
//This statement runs as long 'i' is below 'sizeof(BotMarasID)' (which is 3).
//Every loop i gets increased by one (i++)
You can use 'i' to access the array:

Code:
BotMarasID[i]
.

If you'd want to print the names:

Code:
for (new i; i < sizeof(BotMarasID); i++)
    printf("Index %d: %s", i, BotMarasID[i]);
This would print:

Index 0: bot62
Index 1: bot63
Index 2: bot64

If I understood your question correctly (how to use all elements of an array?) -You gave the correct code for that already
very thx master, +rep
bookknp is offline   Reply With Quote
Old 19/11/2019, 05:58 AM   #4
Calisthenics
High-roller
 
Join Date: May 2018
Posts: 1,229
Reputation: 205
Default Re: I have a question

Furthermore you do not need an extra dimension:
pawn Code:
new BotMarasID[][] =
{
    "bot62",
    "bot63",
    "bot64"
};
Calisthenics 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
[QUESTION] Kind a team based question Dirkon Scripting Help 1 24/03/2013 08:05 PM
Question regarding playerid (noob question!) Mattjones17 Help Archive 5 27/10/2010 04:21 PM
Data type question.. (and mac address question) DarrenReeder Help Archive 2 21/10/2010 08:20 PM
[Question] small question jonybomb Help Archive 10 06/04/2010 08:09 PM
Hello all i got one question (very easy to answer J.I.C question) SAMPfan109 General 0 06/03/2010 04:43 PM


All times are GMT. The time now is 10:47 PM.


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