Problem with foreach?
#1

Suddenly my foreach include starts to act up...I ran a loop with 6 players online and it only executed the code for ID 0. What could wrong?
Reply
#2

It called by any timer?
Can you show the code?
Reply
#3

Yea it's called by a timer. The loop is in a 2 millisecond timer (it's a part of my speedometer)
Reply
#4

If you are using SetTimer, replace it with SetTimerEx
Reply
#5

Okay, I will but what's the problem? It has been working all along.
Reply
#6

I'm sorry, I fumbled in my thinking.
Your speedometer is something like this?

pawn Код:
public Speedometer()
{
    //
}
Or
pawn Код:
public Speedometer(playerid)
{
    //
}
Reply
#7

I'm wondering if it's conflicting with Blue-G's MySQL R7 plugin, cause it was working fine when I was on R6, or it's just a co-incident.
Reply
#8

Post the code wherever you use foreach

There can be quite a lot of things wrong, and it would be hard to find out without seeing the code.
Reply
#9

I only have the problem in the code snippet I PMed to you. Is it possible for a 1 millisecond timer to be faster than a loop through 6 players?
Reply
#10

It really depends on what you're trying to do.

You're trying to make a speedometer so instead of using a timer, use the OnPlayerUpdate callback. It gets called around 30 times a sec, and it has a playerid parameter so you can avoid issues with foreach.

But if you actually need a timer use y_timers.
pawn Код:
task Timer[1000]()
{
    foreach(Player, i)
    {
        // if the player is in a vehicle
        if(IsPlayerInAnyVehicle(i))
        {
            // math code here
            // textdraw code here
        }
    }
    return 1;
}
With y_timers, you don't have to use SetTimer, you don't have to forward any timers. The number between the [ ] is the tick rate, put in how often in milliseconds you want the function to be called.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)