13.07.2016, 05:20
Just bumping this here for the newbies!
foreach(new playerid : Player)
{
if(playerid >= 5)
{
break; // for example?
}
}
Originally Posted by Doritoss
Your foreach include is broken with the YSI package.
|
Technically the latest foreach standalones are supposed to be able to communicate with YSI so their functionality with each other remains. YSI's foreach is more up to date and has more features though.
|
/*
native Iter_Loop(&variable)<type> { }
native Iter_LoopEx(&variable, &index, start, end)<type> { }
native _
native Iter_Size<type>;
native Iter_PoolSize<type>;
native Iter_IsFull<type>;
native _
native Iter_Add(value)<type>;
native Iter_Remove(value)<type>;
native Iter_Value(index)<type>;
native Iter_Index(value)<type>;
native Iter_IsValidValue(index)<type>;
native _
native Iter_Random<type>;
*/
#define Iter:%1<%2> \
IterData_%1[%2] = {-1, ...}, IterSize_%1 = %2, IterPoolSize_%1 = 0
#define Iter_Loop(%1)<%2> \
for (new %1_index = 0, %1 = IterData_%2[0]; %1_index < IterPoolSize_%2; %1 = IterData_%2[++%1_index])
#define Iter_LoopEx(%1,%3,%4,%5)<%2> \
for (new %3 = %4, %1 = IterData_%2[%4], %1_limit = (((%5 < 0 && %5 >= IterPoolSize_%2) || (%5 < %4)) ? (IterPoolSize_%2) : (%5)); %3 < %1_limit; %1 = IterData_%2[++%3])
#define Iter_Size<%2> \
IterSize_%2
#define Iter_PoolSize<%2> \
(IterPoolSize_%2 - 1)
#define Iter_IsFull<%2> \
((IterPoolSize_%2 + 1) == IterSize_%2)
#define Iter_Add(%1)<%2> \
if (_Iter_Add(IterData_%2, IterSize_%2, IterPoolSize_%2, %1)) IterPoolSize_%2++
stock bool: _Iter_Add(array[], size, index, value)
{
if (index >= size)
return false;
array[index] = value;
return true;
}
#define Iter_Remove(%1)<%2> \
if (_Iter_Remove(IterData_%2, IterPoolSize_%2, %1)) IterPoolSize_%2--
stock bool: _Iter_Remove(array[], poolsize, value)
{
new bool: _shift;
for (new _i; _i < poolsize; _i++)
{
if (!_shift)
{
if (array[_i] == value)
_shift = true;
}
if (_shift)
{
if ((_i + 1) < poolsize)
array[_i] = array[_i + 1];
}
}
if (!_shift)
return false;
array[poolsize] = -1;
return true;
}
#define Iter_Value(%1)<%2> \
((%1 < IterPoolSize_%2 && %1 >= 0) ? (IterData_%2[%1]) : (-1))
#define Iter_Index(%1)<%2> \
_Iter_Index(IterData_%2, IterSize_%2, %1)
stock _Iter_Index(array[], size, check)
{
for (new _i; _i < size; _i++)
{
if (array[_i] == check)
return _i;
}
return -1;
}
#define Iter_IsValidValue(%1)<%2> \
_Iter_IsValidValue(IterData_%2, IterSize_%2, IterPoolSize_%2, %1)
stock bool: _Iter_IsValidValue(array[], size, poolsize, check)
{
if (poolsize == 0)
return false;
for (new _i; _i < size; _i++)
{
if (array[_i] == check)
return true;
}
return false;
}
#define Iter_Random<%2> \
IterData_%2[random(IterPoolSize_%2)]
#include <a_samp>
#include <g_foreach>
new Iter:PLAYERS<MAX_PLAYERS>;
public OnPlayerConnect(playerid)
{
Iter_Add(playerid)<PLAYERS>;
return 1;
}
public OnPlayerDisconnect(playerid, reason)
{
Iter_Remove(playerid)<PLAYERS>;
return 1;
}
forward OnCall();
public OnCall()
{
Iter_Loop(i)<PLAYERS>
{
printf("PlayerId: %i", i);
}
return 1;
}
#define FILTERSCRIPT
#include<a_samp>
#include<foreach>
#include<zcmd>
CMD:hello(playerid)
{
new var = 0;
foreach(new i : Player)
{
printf("Iterating\n");
if(!strcmp(Getname(i),"ghost",true))
var++;
}
printf("Result : %d ",var);
return 1;
}
Getname(playerid)
{
new name[24];
GetPlayerName(playerid,name,24);
return name;
}
Result : 0
Iterating
Result : 1
CMD:hello(playerid, params[]) { new var = 0; foreach(new i : Player) { printf("Iterating\n"); var++; } printf("Result : %d ",var); return 1; }
Did you restart your server by typing gmx in server console? (or any other equivalent)
If so, don't do it anymore. Just stop&start the server. // Why don't you simply test foreach like this: Код:
CMD:hello(playerid, params[]) { new var = 0; foreach(new i : Player) { printf("Iterating\n"); var++; } printf("Result : %d ",var); return 1; } |
#include <a_samp>
#include<foreach>
#include<zcmd>
#define FILTERSCRIPT
new Iterator:SS_Player<MAX_PLAYERS>;
public OnPlayerConnect(playerid)
{
Iter_Add(SS_Player,playerid);
return 1;
}
public OnPlayerDisconnect(playerid,reason)
{
Iter_Remove(SS_Player,playerid);
return 1;
}
public OnFilterScriptExit()
{
Iter_Clear(SS_Player);
return 1;
}
CMD:test(playerid)
{
new var,i;
printf("i before iterating = %d ",i);
printf("Contains %d", Iter_Count(Player));
foreach( i : SS_Player)
{
var++;
printf("iterating");
}
printf("i after complete iterations = %d var = %d ",i,var);
return 1;
}
Sorry for double posting but i really found a thing.When i use my own custom iterator for player instead of the default one its working Ghost you should also try these whether its working for you or not:
PHP код:
|
OMG its now fucking working thanks! so this was the problem from default iterator?
|
By default array with values is not cleared after restarting.
I have tried to fix that problem and little bit optimize foreach also i have added Vehicle and Actor iterators from latest y_iterator. You can check my version here: https://github.com/Open-GTO/foreach Changes: - Removed support NPC scripts. - Removed FOREACH_NO_BOTS option. - Actor and Vehicle iterators. - Optimized ALS hooking process (without CallLocalFunction). |
By default array with values is not cleared after restarting.
I have tried to fix that problem and little bit optimize foreach also i have added Vehicle and Actor iterators from latest y_iterator. You can check my version here: https://github.com/Open-GTO/foreach Changes: - Removed support NPC scripts. - Removed FOREACH_NO_BOTS option. - Actor and Vehicle iterators. - Optimized ALS hooking process (without CallLocalFunction). |