SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 11/01/2020, 03:43 PM   #21
Calisthenics
Gangsta
 
Join Date: May 2018
Posts: 956
Reputation: 167
Default Re: Looping through enum

pawn Code:
new slot_found = -1;

for(z = 0; z < MAX_OWNED_VEHICLES; z++) //find empty slot
{
    if(Vehicles[playerid][z][veh_model] == 0) //check if modelid is 0
    {
        slot_found = z;
        break;
    }
}

if (slot_found != -1) // found empty slot
{
    // assign variables
}
or
pawn Code:
for(z = 0; z < MAX_OWNED_VEHICLES; z++) //find empty slot
{
    if(Vehicles[playerid][z][veh_model] == 0) //check if modelid is 0
    {
        // assign variables
        break;
    }
}

If you only want to check if a created vehicle belongs to a player, `SpawnedVehicles` iteration will do fine. The iterator in general is useful when you want to loop through player vehicles or know if it belongs to the player quickly without loops. Alternative would be to store the in-game vehicleid to the enumeration and loop to compare.
__________________
Calisthenics is offline   Reply With Quote
Old 11/01/2020, 08:27 PM   #22
FunnyBear
Gangsta
 
Join Date: Mar 2013
Posts: 512
Reputation: 15
Default Re: Looping through enum

Quote:
Originally Posted by Calisthenics View Post
If you only want to check if a created vehicle belongs to a player, `SpawnedVehicles` iteration will do fine. The iterator in general is useful when you want to loop through player vehicles or know if it belongs to the player quickly without loops. Alternative would be to store the in-game vehicleid to the enumeration and loop to compare.
Iím not home right now but Iím guessing I could use

pawn Code:
Iterator:SpawnedVehicles<MAX_VEHICLES, MAX_PLAYER_VEHICLES>;

Then when creating a vehicle just

pawn Code:
Iter_Add(SpawnedVehicles<vehicles>, veh_id); //veh_id being the unique id of the vehicle

And then when a player enters a vehicle, loop through SpawnedVehicles, check if the vehicleidís match and then check the owner of the veh_id and see if they match?
FunnyBear is offline   Reply With Quote
Old 11/01/2020, 10:35 PM   #23
Calisthenics
Gangsta
 
Join Date: May 2018
Posts: 956
Reputation: 167
Default Re: Looping through enum

Like this: https://forum.sa-mp.com/showpost.php...85&postcount=9 and `Iter_Contains`.
__________________
Calisthenics is offline   Reply With Quote
Old 11/01/2020, 11:38 PM   #24
FunnyBear
Gangsta
 
Join Date: Mar 2013
Posts: 512
Reputation: 15
Default Re: Looping through enum

Quote:
Originally Posted by Calisthenics View Post
As I've said previously, I don't store the vehicleid in the Vehicles enum so I have nothing to compare the iter with
FunnyBear is offline   Reply With Quote
Old 12/01/2020, 10:45 AM   #25
Calisthenics
Gangsta
 
Join Date: May 2018
Posts: 956
Reputation: 167
Default Re: Looping through enum

Quote:
Originally Posted by FunnyBear View Post
As I've said previously, I don't store the vehicleid in the Vehicles enum so I have nothing to compare the iter with
You do not need to store the vehicleid in the array, this is why we used the multi-iterator. `Iter_Contains` is enough to know if a vehicle is owned by a player, nothing extra to compare against.
__________________
Calisthenics is offline   Reply With Quote
Old 12/01/2020, 04:53 PM   #26
FunnyBear
Gangsta
 
Join Date: Mar 2013
Posts: 512
Reputation: 15
Default Re: Looping through enum

Quote:
Originally Posted by Calisthenics View Post
You do not need to store the vehicleid in the array, this is why we used the multi-iterator. `Iter_Contains` is enough to know if a vehicle is owned by a player, nothing extra to compare against.
Oh, right. I get you know. But what if I wanted to display the name of the owner of the vehicle?
FunnyBear is offline   Reply With Quote
Old 12/01/2020, 05:19 PM   #27
Calisthenics
Gangsta
 
Join Date: May 2018
Posts: 956
Reputation: 167
Default Re: Looping through enum

Quote:
Originally Posted by FunnyBear View Post
Oh, right. I get you know. But what if I wanted to display the name of the owner of the vehicle?
This is a nice question. I remember it was added as a new feature but I had to look it up.
pawn Code:
new ownerid = Iter_GetMulti(OwnedVehicles<>, vehicleid);

if (ownerid != INVALID_ITERATOR_SLOT) // vehicleid exists in multi-iterator
{
    new player_name[MAX_PLAYER_NAME];

    GetPlayerName(ownerid, player_name, MAX_PLAYER_NAME);
    printf("Player %s (%d) owns vehicle %d", player_name, ownerid, vehicleid);
}
__________________
Calisthenics is offline   Reply With Quote
Old 12/01/2020, 05:32 PM   #28
Student6
Little Clucker
 
Join Date: Nov 2019
Posts: 2
Reputation: 0
Default Re: Looping through enum

Quote:
Originally Posted by Calisthenics View Post
This is a nice question. I remember it was added as a new feature but I had to look it up.
pawn Code:
new ownerid = Iter_GetMulti(OwnedVehicles<>, vehicleid);

if (ownerid != INVALID_ITERATOR_SLOT) // vehicleid exists in multi-iterator
{
    new player_name[MAX_PLAYER_NAME];

    GetPlayerName(ownerid, player_name, MAX_PLAYER_NAME);
    printf("Player %s (%d) owns vehicle %d", player_name, ownerid, vehicleid);
}
That won't work if `ownerid` isn't a connected player.
Student6 is offline   Reply With Quote
Old 12/01/2020, 05:40 PM   #29
Calisthenics
Gangsta
 
Join Date: May 2018
Posts: 956
Reputation: 167
Default Re: Looping through enum

Quote:
Originally Posted by Student6 View Post
That won't work if `ownerid` isn't a connected player.
Quote:
Originally Posted by Calisthenics View Post
Do not forget to reset the variables when you destroy the vehicle (on player disconnect for example, depending on your system) and remove vehicle from the iterator for the player.
As long as the vehicle is removed from the multi-iterator when the vehicle is destroyed/player disconnects, it will never return wrong data. Sure you can double check it but I feel it is unnecessary:
pawn Code:
new ownerid = Iter_GetMulti(OwnedVehicles<>, vehicleid),
    player_name[MAX_PLAYER_NAME];

if (ownerid != INVALID_ITERATOR_SLOT && GetPlayerName(ownerid, player_name, MAX_PLAYER_NAME))
{
    printf("Player %s (%d) owns vehicle %d", player_name, ownerid, vehicleid);
}
__________________
Calisthenics is offline   Reply With Quote
Old 12/01/2020, 08:45 PM   #30
FunnyBear
Gangsta
 
Join Date: Mar 2013
Posts: 512
Reputation: 15
Default Re: Looping through enum

Quote:
Originally Posted by Calisthenics View Post
As long as the vehicle is removed from the multi-iterator when the vehicle is destroyed/player disconnects, it will never return wrong data. Sure you can double check it but I feel it is unnecessary:
pawn Code:
new ownerid = Iter_GetMulti(OwnedVehicles<>, vehicleid),
    player_name[MAX_PLAYER_NAME];

if (ownerid != INVALID_ITERATOR_SLOT && GetPlayerName(ownerid, player_name, MAX_PLAYER_NAME))
{
    printf("Player %s (%d) owns vehicle %d", player_name, ownerid, vehicleid);
}
pawn Code:
new vehicleid = GetPlayerVehicleID(playerid);
new ownerid = Iter_GetMulti(SpawnedVehicles<>, vehicleid),
    player_name[MAX_PLAYER_NAME];

if(ownerid != INVALID_ITERATOR_SLOT && GetPlayerName(ownerid, player_name, MAX_PLAYER_NAME))
{
    SendClientMessage(playerid, SERVER_MESSAGE, "You have entered your privately owned vehicle, you can drive it.");
}
else if(ownerid == INVALID_ITERATOR_SLOT && GetPlayerName(ownerid, player_name, MAX_PLAYER_NAME))
{
    format(str, sizeof(str), "This vehicle is owned by %s, you cannot drive it.", player_name);
    SendClientMessage(playerid, SERVER_MESSAGE, str);
    RemovePlayerFromVehicle(playerid);
}

Works fine if the player enters their own vehicle, but if you enter any other vehicle (including server vehicles), I get the This vehicle is owned by ___, you cannot drive it. message.
FunnyBear is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Looping through an enum zsoolt997 Scripting Help 4 17/01/2018 08:42 AM
Looping through enum with switch MotherDucker Scripting Help 8 12/09/2017 02:11 PM
Storing in a ENUM interfears with another enum justjamie Scripting Help 9 14/08/2016 08:20 AM
Pawn: enum type in enum sagosagi1 Scripting Help 6 19/05/2014 09:34 AM
Multiple instances of an enum inside an enum Enforcer501 Scripting Help 4 18/06/2013 03:31 PM


All times are GMT. The time now is 02:54 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.