Sync Traffic Lights

Hello, i am not sure if this was asked before or not , couldnt find anything related

I am working on something currently that need the traffic lights to be changed as i want synced with NPCs…

i know there is already SetEntityTrafficlightOverride - FiveM Natives @ Cfx.re Docs native to do it client side, but it will not affect NPCs and they will ignore this change

is there anything that can be done to sync them with NPCs aswell ?

will there be another function like ChangeTrafficlightState to just switch between different light states ?

thanks !

Hi @ali_exacute,

Unfortunately I don’t think that’s possible natively (reference).

You could try to force NPCs to stop by playing a task if they’re near a traffic light which is Red, but to make such a concept work properly you’d have to save traffic light states or check for a certain flag to trigger the task… could get messy really quickly. (reference)

Hello @akeem thank you for your reply

It probably is not a good example as they are not the same , but i have seen this native on other GTA V multiplayer modifications that can actually sync it to NPCs as well

so that is why i asked about it, as it would be really useful for a lot of scenarios

but trying to change NPC flags to force them to start and stop, as you said , will get really messy and complicated for no reason , and could possibly also break any other script that could potentially change these flags or vice versa

@ali_exacute

If some other platform has been able to achieve it, then it will also be possible for us to achieve it, we just need to dig the information out and, at the current moment in time, it seems nobody has.

I am sure FiveM devs can pull this off , as i am sure they are capable of it

i just hope this is gonna be on a todo list as i would love to release some scripts utilizing it

A getter for the traffic light state would be really handy too. This way, you could check for players running a red light for example.

yeah exactly ,
you can do a lot of things with them
really hope they look into it !

I am of the opinion that I know a possible option that implements the whole thing better. The problem with setting the state is that the visibility of this is very limited by OneSync, because in GTA the traffic lights are behind the intersection is the player at one or the other traffic light already on the intersection until the status is then displayed correctly, unless you let the thread really run with wait(0) which I do not recommend.

However, there is an .xml file in the game files that contains all the intersections, it seems visually as if you can also manually change the phases there. I don’t know exactly.

In order to implement this correctly only on the client side, a native like GetObjectsInArea is missing, we have the function for vehicles, but not for objects, traffic lights are objects and so we can always detect only the closest object to which one is closest.

My third suggestion that might work would be:

  • Write a function that automatically inserts intersections based on chat command or even better a thread that detects traffic lights in a certain radius against checks whether already exists and if not created. so must be driven to the data collection only once the whole map.
  • Then all traffic lights with the native CreateModelHide ensured that all vanilla traffic lights are not visible.
  • Copy all traffic lights from gta v and rename then, so they are not hided. But I also don’t know if in that case the natives to set the status of the traffic light still work.
  • On this basis one could OneSync compatible the traffic lights in a certain radius around the players spawn let with the hope that the radius of the correct traffic light status with it further visibly is, but it is a XXL project to place all traffic lights again in such a way that they are defined as crossings and are switched suitably, advantage here would be that this approach but in itself server-sided with a thread the Spawn and delete would be solvable and client-sided only the Sync works with events. This saves resource timing at the client. One would have to understand the mentioned xml file better and know its effect. Likewise one should find from the code a method to recognize what distinguishes the crossings. In this case you could write a script which reads the xml and rewrites it into a lua or json config according to the script, that would be the optimal way.

I have tried to look at the xml, if someone has more knowledge of it and can say more precisely what affects how and how to recognize which traffic light to which intersection for which interval is I could tackle the whole thing.

From the server side it would also be conceivable to manage to stop ai vehicles I think. I recommend in this case, however, to turn off the AI to save performance and instead write an AI that sends itself vehicles through the world because these would be easier to control.

Otherwise you could also load traffic lights as custom objects that don’t show anything and then start to define where on the model the light should go and then work with further objects, which might work again over even greater distance if the client spawns the light but the server spawns the traffic light itself.

Translated with DeepL Translate: The world's most accurate translator (free version)

1 Like

Your third suggestion is actually pretty cool idea, but also really really hard to get working perfectly as so many things have to be perfect
like, spawning and despawning the lights
handling the lights timings and when it should be green , yellow or red exactly to not mess anything up
manage AIs to work with this aswell

kinda cool though !

thank you for writing this , the CreateModelHide is actually pretty cool , i didnt know that exist !

I have just to go into it again a server-side Ki for my project ready, but no AI for individual traffic but so far only for fixed routes. The technology goes so far that the Onesync radius hardly plays a role, I can also represent traffic outside of 421 meters around the players if I want. To save performance in this distance range but no smooth driving is possible but rather a forward jump, traffic lights are currently already simulated, the Ki would be extended accordingly when creating routes, the vehicles would also consider the traffic lights.

The traffic lights can be developed in my opinion with relatively manageable effort, I could pull this through myself, but I would need help:

a) several testers who adjust an xml file several times and then observe it ingame, thereby recognizing the logic and then helping me with the findings so that I can write the code that would write configs for all traffic lights automatically.
b) I would need a 3D designer to create traffic lights, dead models without a displayed status, I would need objects to put on the traffic light to display its status. This is technically possible, there are other scripts that use exactly this technique.

I can rebuild and implement this as I said but by no means alone.

Also, I don’t recommend anyone to use the default scripts for spawning. I have also worked on it for a while and now a spawn system which can spawn multiple vehicles simultaneously asyncron, no matter whether object, ped or vehicle, the performance is higher, although 10 vehicles pop up simultaneously.

Translated with DeepL Translate: The world's most accurate translator (free version)

You guys sound absolutely insane with these wild ideas and theories. This is already possible with a little code. I do it with my xnTrafficLights script. You can set the colour of the light then create a “Speed Zone” AddRoadNodeSpeedZone with a speed of 0. Then later remove it with RemoveRoadNodeSpeedZone. These speed zones cause NPCs to stop right at them, they don’t even complain about being stopped, you can leave them stopped as long as you want without them speeding off or whatever, you could even place a big one right in the middle of the intersection to stop all cars if you set the radius large enough.

3 Likes

Surely a solution, but my inner perfectionist comes through, why create everything manually if it could be technically generated automatically and then delivered with the release.

I’m not sure off the top of my head, but there is an .xml file in the game data of GTA v where all the traffic lights are defined and their location, the first test I took there coordinates and then stood exactly where the traffic light is. Either that was trafficlights.xml or intersections.xml somehow so was the name. I write for example also in PHP, I can look for a xml class, read the file which is supplied by the game and then the entire traffic lights automatically set up.

Also no offense is too little code on the server for my taste