[Include] foreach 0.4.1 standalone include
#21

Just bumping this here for the newbies!
Reply
#22

Quote:
Originally Posted by Kar
Посмотреть сообщение
Just bumping this here for the newbies!
No worries.

OT: Useful!
Reply
#23

Hi there. Does anyone happen to know if you can use "break" to stop a foreach loop?

pawn Код:
foreach(new playerid : Player)
{
    if(playerid >= 5)
    {
        break; // for example?
    }
}
I don't currently have a way to test this with another player. Thank you.
Reply
#24

Quote:
Originally Posted by Liberation
Посмотреть сообщение
Hi there. Does anyone happen to know if you can use "break" to stop a foreach loop?

pawn Код:
foreach(new playerid : Player)
{
    if(playerid >= 5)
    {
        break; // for example?
    }
}
I don't currently have a way to test this with another player. Thank you.
Yes, you can!
Reply
#25

Quote:
Originally Posted by Doritoss
Your foreach include is broken with the YSI package.
Note to all: This is a standalone include, do not use it with YSI!

YSI has y_iterate (which is foreach in YSI)!
Reply
#26

Quote:
Originally Posted by Kar
Посмотреть сообщение
Note to all: This is a standalone include, do not use it with YSI!

YSI has y_iterate (which is foreach in YSI)!
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.
Reply
#27

Quote:
Originally Posted by Crayder
Посмотреть сообщение
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.
Thanks for giving the public more information

I already know.

And technically the support for the newer standalones was dropped for YSI 4.
Reply
#28

Here is my version of foreach:

pawn Код:
/*
 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)]
Faster than ******'s as per my speed tests and have some good functions to work around with.

Example:
pawn Код:
#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;
}
Reply
#29

Quote:
Originally Posted by Gammix
Посмотреть сообщение
Here is my version of foreach:
[...]
Does it even work?

pawn Код:
#include <a_samp>
#include <g_foreach>

new Iter:TEST<32>;

main(){}
public OnGameModeInit()
{
    Iter_Add(5)<TEST>;
    Iter_Add(2)<TEST>;
   
    Iter_Loop(i)<TEST>
    {
        printf("%i", i);
    }
    return 1;
}

Prints out:
-1
5
Reply
#30

Quote:
Originally Posted by Stinged
Посмотреть сообщение
Does it even work?

pawn Код:
#include <a_samp>
#include <g_foreach>

new Iter:TEST<32>;

main(){}
public OnGameModeInit()
{
    Iter_Add(5)<TEST>;
    Iter_Add(2)<TEST>;
   
    Iter_Loop(i)<TEST>
    {
        printf("%i", i);
    }
    return 1;
}

Prints out:
-1
5
Yep, fixed it in Iter_Loop.
Reply
#31

I have some problem to report
Sometimes it wont iterate.I tried many tweaks and debugs but none of them worked.But sometimes when i restart the server it will work. I tried below code today morning :
PHP код:
#define FILTERSCRIPT
#include<a_samp>
#include<foreach>
#include<zcmd>
CMD:hello(playerid)
{
 new var = 
0;
 foreach(new 
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;

OUTPUT:
@first
PHP код:
Result 
after reconnecting to server and on the 3rd time

PHP код:
 Iterating
 Result 

hope you will look into the problem.
Reply
#32

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; 
}
If var == player count, then the issue lies in your code.
Reply
#33

Quote:
Originally Posted by Spmn
Посмотреть сообщение
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; 
}
If var == player count, then the issue lies in your code.
Thank you for replying i didnt restart my server by gmx and today i tested that on first connect to the server the foreach is skipping each iterations but when i exit from the game and reconnect it starts working i think its a problem in "Player" var seeding in the include.And yeah i tested it in that way @ first then also it was all same result then i added some functions and done the checking and i have seen that Kar bumped this topic so i decided to come up with last debug code thats why..
Reply
#34

I just saw the post of Ghost hacker im also facing this problem any way to fix it?
Reply
#35

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 код:
#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( 
SS_Player)
{
var++;
printf("iterating");
}
printf("i after complete iterations = %d var = %d ",i,var);
return 
1;

Reply
#36

Quote:
Originally Posted by Sreyas
Посмотреть сообщение
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 код:
#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( 
SS_Player)
{
var++;
printf("iterating");
}
printf("i after complete iterations = %d var = %d ",i,var);
return 
1;

OMG its now fucking working thanks! so this was the problem from default iterator?
Reply
#37

Quote:
Originally Posted by GhostHacker
Посмотреть сообщение
OMG its now fucking working thanks! so this was the problem from default iterator?
Idk cause i didnt built this include i think kar or yless will answer to this. I just did what my head told me and im not sure its a right thing to do.
Reply
#38

Quote:
Originally Posted by GhostHacker
Посмотреть сообщение
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).
Reply
#39

Quote:
Originally Posted by ZiGGi
Посмотреть сообщение
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).
Why would you remove NPC support? You added Actor support, but removed one of the main iterators. I use both, the "Character" and "Bot" iterators, so I'm not exactly a fan of this change...
Reply
#40

Quote:
Originally Posted by ZiGGi
Посмотреть сообщение
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).
They use it as filterscript so RCON command reloadfs should have been used.

About the Vehicle iterator: it is for local vehicles, each script has each own vehicles not global (from all scripts).
If you could add both options would be nice.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)