26.02.2018, 16:02
(
Последний раз редактировалось GaByM; 27.02.2018 в 04:40.
)
Kind of foreach, but it uses string functions (strcat, strfind, strdel) creating an array with no gaps and
this include does not replace y_iterate/foreach!
Advantages:
- The size of the iterator is not necessary to be the maximum value you want to add (if your minigame system can hold 10 players, you can create your iterator with size 10, and not MAX_PLAYERS)
- You can add values between 0 and 16777214.
- The values are saved in order they joined
- No need to initialize multidimensional iterators.
- Inside loop() you can use StepIter_ to check the index.
- Each iteration of the loop() should be as fast as foreach's.
Disadvantages:
- You can have the same value multiple times inside the iterator.
- Adding and removing a value from iterators is slower than Iter_Add/Iter_Remove.
Syntax:
Here is an example of a loop:
Which prints this:
Functions:
AddIter(iterator, value) - add a value inside the iterator
RemoveIter(iterator, value) - removes a value from the iterator
SafeRemoveIter(iterator, value, contor) - removes a value from the iterator inside a loop
CheckSlotIter(iterator, slot) - the value of the iterator at index slot, since the values are stored consecutive inside the array, the index matters.
ContainsIter(iterator, value) - 1 if the value is inside the iterator, 0 otherwise
CountIter(iterator) - the number of values inside the iterator
RandomIter(iterator) - returns a random value from the iterator
ClearIter(iterator) - resets the iterator
When to use:
- Creating a query (e.g.: for a saving system)
You don't want to save all players at the same time, so you decide to save 1 player at 6 seconds
This method looks fine, but you can save an account which just logged in a few seconds ago. Why would a newly joined player need a save, when there are so many players whose account were not saved for minutes?
So now you create an array in which you store the timestamp when you saved their accounts, and everytime save() is called, you check which account wasn't saved for a long period of time.
Or you can do this:
Changelog:
- Iter_Step is now StepIter_counter
- CheckSlot is now CheckSlotIter
Notes:
- Default iterators and those mentioned in this post are not included.
- Strcat & strfind also works with negative numbers (not 0) but mixing positive and negative values inside the same iterator does not work correctly.
Download:
https://pastebin.com/0MXKajZi
this include does not replace y_iterate/foreach!
Advantages:
- The size of the iterator is not necessary to be the maximum value you want to add (if your minigame system can hold 10 players, you can create your iterator with size 10, and not MAX_PLAYERS)
- You can add values between 0 and 16777214.
- The values are saved in order they joined
- No need to initialize multidimensional iterators.
- Inside loop() you can use StepIter_ to check the index.
- Each iteration of the loop() should be as fast as foreach's.
Disadvantages:
- You can have the same value multiple times inside the iterator.
- Adding and removing a value from iterators is slower than Iter_Add/Iter_Remove.
Syntax:
Here is an example of a loop:
PHP код:
new Iter:hi<4>;
AddIter(hi, 5);
AddIter(hi, 0);
AddIter(hi, 19);
AddIter(hi, 19);
loop(new i : hi)
{
printf("hi[%i] = %i", StepIter_i, i);
}
Код:
hi[0] = 5 hi[1] = 0 hi[2] = 19 hi[3] = 19
AddIter(iterator, value) - add a value inside the iterator
RemoveIter(iterator, value) - removes a value from the iterator
SafeRemoveIter(iterator, value, contor) - removes a value from the iterator inside a loop
PHP код:
// This is how it should be done
loop(new i : my_iter)
{
SafeRemoveIter(my_iter, i, StepIter_i);
}
ContainsIter(iterator, value) - 1 if the value is inside the iterator, 0 otherwise
CountIter(iterator) - the number of values inside the iterator
RandomIter(iterator) - returns a random value from the iterator
ClearIter(iterator) - resets the iterator
When to use:
- Creating a query (e.g.: for a saving system)
You don't want to save all players at the same time, so you decide to save 1 player at 6 seconds
PHP код:
main()
{
SetTimer("save", 6_000, 1);
}
forward save();
public save()
{
lastSaved++; //increment the last player saved
lastSaved %= MAX_PLAYERS; //make sure we don't overflow
SavePlayer(lastSaved);
}
So now you create an array in which you store the timestamp when you saved their accounts, and everytime save() is called, you check which account wasn't saved for a long period of time.
Or you can do this:
PHP код:
new Iter:saved<MAX_PLAYERS>;
main()
{
SetTimer("save", 6_000, 1);
}
forward save();
public save()
{
new player = CheckSlotIter(saved, 0); //check who is next in the query
SavePlayer(player);
RemoveIter(saved, player); //remove player from the que
AddIter(saved, player); //adds them at the end of the que
}
hook OnPlayerConnect(playerid)
{
AddIter(saved, playerid); // now at the end of the query
}
hook OnPlayerDisconnect(playerid, reason)
{
RemoveIter(saved, playerid);
}
Changelog:
- Iter_Step is now StepIter_counter
- CheckSlot is now CheckSlotIter
Notes:
- Default iterators and those mentioned in this post are not included.
- Strcat & strfind also works with negative numbers (not 0) but mixing positive and negative values inside the same iterator does not work correctly.
Download:
https://pastebin.com/0MXKajZi