SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 10/09/2016, 01:51 AM   #1
Arthurillo
Little Clucker
 
Arthurillo's Avatar
 
Join Date: Aug 2016
Posts: 30
Reputation: 0
Default y_iterate - tutorial basic

I have seen the two guides are of y_iterate, but I can not understand the basics, because the tutorials teach you a generalized form.

Could someone explain me the basic use of y_iterate ?, what is iterators and that sort of thing.
Arthurillo is offline   Reply With Quote
Old 10/09/2016, 08:30 AM   #2
Misiur
High-roller
 
Misiur's Avatar
 
Join Date: Jul 2009
Location: Poland
Posts: 2,528
Reputation: 556
Default Re: y_iterate - tutorial basic

Basically y_iterate/foreach iterator is just an array which holds taken indexes of another array (that array is of dynamic size, using y_malloc) (+ helper variable holding count of indexes held). y_iterate has few built-in iterators, and iterator functions.
The most common builtin iterator used is Player. Basically when a new player connects it adds his id this special array (and removes it when he disconnects).
pawn Code:
foreach(new playerid:Player) {
    printf("Player %d is currently connected", playerid);
}

You can create your own iterators. In example below I use #define to, well, define the size of the iterator, because I can keep all my defines in one place, and naming a number reduces number of "magic numbers" (too much number in that sentence).
pawn Code:
#define DescriptiveNameOfNumber 20

new Iter:MySpecialIterator<DescriptiveNameOfNumber>;
You just created an iterator with 20 slots. You have to add items to it manually. Say you're loading something from database/ini file/whatever - the code is mainly pseudo-code:
pawn Code:
new MySpecialData[DescriptiveNameOfNumber] = { -1, ... };

MyFunctionLoadingData() {
    for(new i = 0; i != rows; ++i) {
        MySpecialData[i] = fetch_data_from_source(i);
        Iter_Add(MySpecialIterator, i);
    }
}

Just like that you can now do:
pawn Code:
foreach(new index:MySpecialIterator) {
    printf("My special data at index %d holds", MySpecialData[i]);
}

Neat! There are also 2d-iterators:
pawn Code:
#define MAX_CELLPHONES_PER_PLAYER 2

new Iter:PlayerCellphones[MAX_PLAYERS]<MAX_CELLPHONES_PER_PLAYER>;

Unless you are using Zeex's compiler (which I guess you don't as you're a beginner), you must initialize it in OnGameModeInit (I can't remember ATM if main is good too, but just to be safe):
pawn Code:
public OnGameModeInit() {
    Iter_Init(PlayerCellphones);
}

Now let's say player wants to buy a cellphone, and you want to check if he has an empty slot:
pawn Code:
YCMD:buycell(playerid, params[], help) {
    if(Iter_Free(PlayerCellphones[playerid]) == cellmin) SCM(playerid, -1, "Sorry mate, no more cellphones for you!");
}

We use Iter_Free to check if there are empty slots. If not, the function will return cellmin (this is under discussion, but at the moment of writing that's the value you should use)/

Removing a value is as easy as
pawn Code:
Iter_Remove(YourIterator, index);

There's also Iter_Alloc (add at first free slot), Iter_Random (return random item from an iterator) and Iter_Clear (remove all values).

That's all the basics you need. For those of you more interested, there are some more things:
pawn Code:
new Iterator:PlayerCars<MAX_PLAYERS, MAX_VEHICLES>;
This iterator is used when you're sure that no two players can own the same vehicle - it decreases iterator size from MAX_PLAYERS * MAX_VEHICLES to MAX_PLAYERS + MAX_VEHICLES. For usage, I recommend reading through YSI unit tests.

There are iterator functions, which are really cool:
pawn Code:
foreach(new playerid:Reverse(Player)) //Reverse the array looping
foreach(new i:Powers(4)) //1, 4, 16, ...

You can write your own custom iterator functions, just look through iterators.inc. I don't exactly remember more than using @iterfunc, but there are live examples so I won't bother.

And of course, there is integration with other YSI libraries (basically just iterator functions, but builtin in other libraries). If you are using y_groups, you should be familiar with Group (now deperecated in favour of GroupMember):

pawn Code:
foreach(new player:GroupMember[MyGroup])

That's it. Reading YSI source (you don't need to know macro voodoo to do so, most of it is really clean code) is the best code reference.

My not-so-good tutorial, there's some code examples, not much changed since then http://misiur.github.io/2015/03/07/y...how-to-use-it/
Misiur is offline   Reply With Quote
Old 10/09/2016, 08:41 AM   #3
Mister0
Huge Clucker
 
Mister0's Avatar
 
Join Date: Aug 2015
Posts: 220
Reputation: 19
Default Re: y_iterate - tutorial basic

Misiur, what benefits have iteration? Is faster with much? I mean is like mysql or ini is method of stock?
And worth to transform your scripts to iteration if you don t have used?
Mister0 is offline   Reply With Quote
Old 10/09/2016, 09:21 AM   #4
Misiur
High-roller
 
Misiur's Avatar
 
Join Date: Jul 2009
Location: Poland
Posts: 2,528
Reputation: 556
Default Re: y_iterate - tutorial basic

It's RAM vs CPU. So, having iterators takes few more bytes of RAM, in exchange for faster iterations. Say, you have 5 car slots for a player. Slots 0, 2 and 4 are taken. Normally you'd have to do something like
pawn Code:
for(new index = 0; index != sizeof PlayerCars[]; ++index) {
    if(PlayerCars[playerid][index] == INVALID_VEHICLE_ID) continue;
    //Now do something with the valid car
}
You'd have to use that pattern every time you worked on player cars, be it buying components, locking vehicle, etc. etc. That's prone to being forgotten. But if you use an iterator, you'll just use (not including adding and removing player cars to their slots).
pawn Code:
foreach(new index:PlayerCars[playerid]) //index will be 0, 2 and 4

As for converting whole gamemode: it depends. I won't recommend it, but the process itself is mostly painless. But when writing gm from ground up, I can recommend it as I use it extensively.

Quote:
I mean is like mysql or ini is method of stock?
I have no idea what that sentence means.
Misiur is offline   Reply With Quote
Old 10/09/2016, 09:22 AM   #5
Konstantinos
Spam Machine
 
Konstantinos's Avatar
 
Join Date: Dec 2011
Posts: 11,828
Reputation: 1401
Default Re: y_iterate - tutorial basic

http://web.archive.org/web/201403291...ad.php?t=92679

That is the thread I learned how to use foreach/y_iterate.
__________________
Life is like riding a bicycle. To keep your balance, you must keep moving.

[Tutorial] How to use SQLite
[FilterScript] Tune System
Konstantinos is offline   Reply With Quote
Old 10/09/2016, 01:46 PM   #6
Arthurillo
Little Clucker
 
Arthurillo's Avatar
 
Join Date: Aug 2016
Posts: 30
Reputation: 0
Default Respuesta: y_iterate - tutorial basic

Thank you. I'll read it carefully, if I have any questions, I will mention them.
Arthurillo is offline   Reply With Quote
Old 11/09/2016, 01:36 AM   #7
Arthurillo
Little Clucker
 
Arthurillo's Avatar
 
Join Date: Aug 2016
Posts: 30
Reputation: 0
Default Respuesta: y_iterate - tutorial basic

Can I be explain the use of Iter_SafeRemove ?, I know I explained in the post of ****** , but I do not see might not use or understand its functionality. Thanks
Arthurillo is offline   Reply With Quote
Old 11/09/2016, 08:04 AM   #8
Konstantinos
Spam Machine
 
Konstantinos's Avatar
 
Join Date: Dec 2011
Posts: 11,828
Reputation: 1401
Default Re: y_iterate - tutorial basic

Normally we use Iter_Remove to remove a value from an iterator. However if we want to remove a value inside a foreach loop, we should use Iter_SafeRemove otherwise it will just stop there and won't continue.
__________________
Life is like riding a bicycle. To keep your balance, you must keep moving.

[Tutorial] How to use SQLite
[FilterScript] Tune System
Konstantinos is offline   Reply With Quote
Old 11/09/2016, 08:21 AM   #9
PrO.GameR
Gangsta
 
PrO.GameR's Avatar
 
Join Date: Oct 2012
Posts: 694
Reputation: 121
Default Re: y_iterate - tutorial basic

Basically foreach uses the array slot to determine next iteration, when you remove one of the slots, it cleans the array slot so it can't find the next one, because of this you need to SafeRemove the slot when you are removing it inside a loop (for example removing all the houses in a loop).
Thats pretty much a casual way to explain it.
__________________
Blueberry Prison Roleplay will be back soon!
Follow the forums for more information about opening day.

Forums
PrO.GameR is offline   Reply With Quote
Old 12/09/2016, 12:03 AM   #10
Arthurillo
Little Clucker
 
Arthurillo's Avatar
 
Join Date: Aug 2016
Posts: 30
Reputation: 0
Default Re: y_iterate - tutorial basic

Ok!

I have a doubt. All examples showing the use of multi dimensional iterators are car owners. What if car rent?, because I think it would have to create a three-dimensional array or at least several array to know if it is a rental car and to know who alkyl.

What it is done in this case ?.
Arthurillo 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
[Tutorial] Basic Filterscript Tutorial RevolutionLite Tutorials 11 08/10/2019 04:07 PM
[Tutorial] Lawedan Basic PAWN Tutorial Deskoft Tutorials 11 31/08/2011 12:13 AM
[Tutorial] Basic MySQL Tutorial Faraday Help Archive 43 03/06/2011 12:55 PM
[Tutorial] Simple basic tutorial with name & vehicleid DarkClone Tutorials 11 20/03/2011 07:25 AM


All times are GMT. The time now is 12:05 AM.


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