SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 30/03/2019, 11:47 PM   #1
m4karow
Huge Clucker
 
Join Date: Oct 2015
Posts: 302
Reputation: 37
Default Vehicle passenger iterator

Hey

Is it a good idea to save the passengers to an iterator for faster loop through passengers or finding the vehicle driver? I'll have about 1700 vehicles.

Code:
static Iterator:VehiclePassengers[MAX_VEHICLES]<MAX_PLAYERS>;
and under OnPlayerStateChange add-remove them.

What is your advice?
m4karow is offline   Reply With Quote
Old 31/03/2019, 05:34 AM   #2
Crayder
Banned
 
Join Date: Sep 2013
Location: Flames of Hell
Posts: 3,331
Reputation: 652
Default Re: Vehicle passenger iterator

Yeah this is actually exactly what you probably want to do! Nice usage of an iterator... however, you may want to use "multi-iterators" instead of an array of iterators.

See here for more info, check the first part under "New Features".

This will do the same thing, but much more efficiently and with less usage.
Crayder is offline   Reply With Quote
Old 31/03/2019, 12:17 PM   #3
m4karow
Huge Clucker
 
Join Date: Oct 2015
Posts: 302
Reputation: 37
Default Re: Vehicle passenger iterator

Quote:
Originally Posted by Crayder View Post
Yeah this is actually exactly what you probably want to do! Nice usage of an iterator... however, you may want to use "multi-iterators" instead of an array of iterators.

See here for more info, check the first part under "New Features".

This will do the same thing, but much more efficiently and with less usage.
I'm using it like that.

Code:
foreach(new passenger : VehiclePassengers[vehicleid])
{
}
Code:
Iter_Add(VehiclePassengers[vehicleid], playerid);
Code:
Iter_Remove(VehiclePassengers[vehicleid], playerid);
Code:
hook OnVehicleCreated(vehicleid)
{
	Iter_Init(VehiclePassengers[vehicleid]);
	return Y_HOOKS_CONTINUE_RETURN_1;
}
m4karow is offline   Reply With Quote
Old 31/03/2019, 12:20 PM   #4
m4karow
Huge Clucker
 
Join Date: Oct 2015
Posts: 302
Reputation: 37
Default Re: Vehicle passenger iterator

Quote:
Originally Posted by m4karow View Post
I'm using it like that.

Code:
foreach(new passenger : VehiclePassengers[vehicleid])
{
}
Code:
Iter_Add(VehiclePassengers[vehicleid], playerid);
Code:
Iter_Remove(VehiclePassengers[vehicleid], playerid);
Code:
hook OnVehicleCreated(vehicleid)
{
	Iter_Init(VehiclePassengers[vehicleid]);
	return Y_HOOKS_CONTINUE_RETURN_1;
}
Or the init isn't correct? Should init that under OnScriptInit?
m4karow is offline   Reply With Quote
Old 31/03/2019, 12:22 PM   #5
raydx
Huge Clucker
 
raydx's Avatar
 
Join Date: Feb 2017
Posts: 205
Reputation: 73
Default Re: Vehicle passenger iterator

Init only once under ongamemodeinit (without vehicleid parameter).
raydx is offline   Reply With Quote
Old 31/03/2019, 12:39 PM   #6
m4karow
Huge Clucker
 
Join Date: Oct 2015
Posts: 302
Reputation: 37
Default Re: Vehicle passenger iterator

Quote:
Originally Posted by raydx View Post
Init only once under ongamemodeinit (without vehicleid parameter).
Oh I corrected, thanks
m4karow is offline   Reply With Quote
Old 31/03/2019, 06:28 PM   #7
m4karow
Huge Clucker
 
Join Date: Oct 2015
Posts: 302
Reputation: 37
Default Re: Vehicle passenger iterator

Quote:
Originally Posted by ****** View Post
The multi iterator version Crayder mentioned would be better. Your code could have a player in multiple vehicles at once, which can't happen. Instead:

PHP Code:
new Iterator:Passenger<MAX_VEHICLESMAX_PLAYERS>;

foreach (new 
playerid Passenger<vehicleid>)
{

Basically just replace [vehicleid] with <vehicleid>.
Alright
Thanks for your help
m4karow 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
How to put NPC to passenger in a vehicle ATomas Scripting Help 4 01/07/2018 09:54 AM
Vehicle ownership - foreach iterator Sjn Scripting Help 2 07/07/2016 05:57 PM
You can't sit as a passenger in a Resticed vehicle lulo356 Scripting Help 1 23/10/2014 03:22 PM
foreach Vehicle iterator Dubya Scripting Help 5 23/03/2014 03:25 AM
Exiting vehicle as passenger Rac3r Bug Reports 0 07/04/2012 05:41 PM


All times are GMT. The time now is 12:07 AM.


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