Loop problem.
#1

Good evening,problem is in the loop which is called when player is leaving server, his role in script is to destroy every object which was created for that player. To be correct I am using Streamer dynamic object function to create every object for every player individually. Then when disconnect script should delete all objects for that player. But problem is that this deleting is happining only for ID 0, not for other ID`s.
CODE:
PHP код:
public OnPlayerDisconnect(playerid, reason)
{
    for(new 
i = 0; i < 2000; i++)
    {
        if(!
IsValidDynamicObject(i)) continue;
    
        if(
Streamer_GetIntData(STREAMER_TYPE_OBJECT, i, E_STREAMER_PLAYER_ID) == playerid)
        {
             
DestroyDynamicObject(i);    
        }
    }
    return 
1;
} 
Reply
#2

I doubt if OnPlayerDisconnect will stay for long, use a timer which does these when a player disconnects. Secondly, use CountDynamicObjects() to be sure of the number of objects to be looped.

EDIT : Aren't player objects (dynamic ones) removed by the plugin upon their disconnection? It's been a while since I've been around SA-MP, so I cannot assure you.
Reply
#3

Looks ok to me, are you probably not setting E_STREAMER_PLAYER_ID properly?

Edit: About what Lordzy said, maybe the Streamer plugin invalidates the players id and deletes it everywhere it occured? I don't know if that is a possibility
Reply
#4

Quote:
Originally Posted by Lordzy
Посмотреть сообщение
I doubt if OnPlayerDisconnect will stay for long, use a timer which does these when a player disconnects. Secondly, use CountDynamicObjects() to be sure of the number of objects to be looped.

EDIT : Aren't player objects (dynamic ones) removed by the plugin upon their disconnection? It's been a while since I've been around SA-MP, so I cannot assure you.
Well I tried what you said, it`s kind of fixed... I will try timer too.
Reply
#5

Quote:
Originally Posted by Sanady
Посмотреть сообщение
Well I tried what you said, it`s kind of fixed... I will try timer too.
You must get the Upper Index of dynamic objects, not the amount of dynamic objects created (it's not the same - there can be huge index holes using your method).

Say there are 3 players on your server (0, 1, 2) and you have 1000 objects for each player. When ID 0 and 1 leave, CountDynamicObjects will return 1000, however the last index for player 2 is 2999.

Код:
Streamer_GetUpperBound(STREAMER_TYPE_OBJECT);
To make it even more efficient you can save the lowest and the highest index of the objects for each players and loop in between those.
Having these 2 values will probably save you a LOT of looping.

Also, do not use a Timer, no need to do that.

The object does not get destroyed automatically like Lordzy said, and the playerid will remain in the item's player list so you can check for that player whenever you like.
To be safe, you could use Streamer_GetArrayData, since the player list is actually an array. But if only one player is in the array, it should be fine.
Reply
#6

Aha so I need to check the maximum upper bound with that function, and just after that checking to delete objects right?
Reply
#7

Quote:
Originally Posted by Sanady
Посмотреть сообщение
Aha so I need to check the maximum upper bound with that function, and just after that checking to delete objects right?
You need to loop up to the Uppder Bound of dynamic objects instead of ID 2000 like in the for() loop you posted in the first post.

Make sure you use <= and not < because the Upper Bound is a valid ID as well.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)