CARLA
Loading...
Searching...
No Matches
CarlaLightSubsystem.h
Go to the documentation of this file.
1// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
7#pragma once
8
9#include "Carla.h"
10#include "CarlaLight.h"
11
15
16#include <util/ue-header-guard-begin.h>
17#include "CoreMinimal.h"
18#include "Subsystems/WorldSubsystem.h"
19#include <util/ue-header-guard-end.h>
20
21#include <vector>
22
23#include "CarlaLightSubsystem.generated.h"
24
25/**
26 *
27 */
28
29DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FDayTimeChanged, bool, bIsDay);
30
31UCLASS(Blueprintable, BlueprintType)
32class CARLA_API UCarlaLightSubsystem : public UWorldSubsystem
33{
34 GENERATED_BODY()
35
36 //using cr = carla::rpc;
37
38public:
39
40 // Begin USubsystem
41 void Initialize(FSubsystemCollectionBase& Collection) override;
42 // End USubsystem
43 void Deinitialize() override;
44
45 void RegisterLight(UCarlaLight* CarlaLight);
46
47 void UnregisterLight(UCarlaLight* CarlaLight);
48
49 UFUNCTION(BlueprintCallable)
50 bool IsUpdatePending() const;
51
52 UFUNCTION(BlueprintCallable)
53 int32 NumLights() const {
54 return Lights.Num();
55 }
56
57 std::vector<carla::rpc::LightState> GetLights(FString Client);
58
59 void SetLights(
60 FString Client,
61 std::vector<carla::rpc::LightState> LightsToSet,
62 bool DiscardClient = false);
63
64 UCarlaLight* GetLight(int Id);
65
66 TMap<int, UCarlaLight* >& GetLights()
67 {
68 return Lights;
69 }
70
71 void SetDayNightCycle(const bool active);
72
73 bool GetDayNightCycle() const
74 {
75 return bDayNightCycle;
76 }
77
78 /// The day/night state last broadcast, if any.
79 bool GetDayTimeState(bool &bOutIsDay) const
80 {
81 bOutIsDay = bLastIsDay;
82 return bHasDayTimeState;
83 }
84
85 UPROPERTY(BlueprintCallable, BlueprintAssignable, Category = "DayTimeChangeEvent")
86 FDayTimeChanged DayTimeChangeEvent;
87
88 /// Broadcast a day/night change and then re-apply the UE4->UE5 component
89 /// intensity conversion on every registered light. Use this instead of
90 /// broadcasting DayTimeChangeEvent directly: blueprint-bound handlers push
91 /// raw authored intensities into the light components, undoing any
92 /// conversion applied earlier in the same broadcast.
93 void NotifyDayTimeChange(bool bIsDay);
94
95 /// Flag every registered client (except ClientThatUpdate) so their
96 /// LightManager re-queries light states. Must be called after any
97 /// server-side state change made outside a client request (day/night
98 /// broadcast, recorder replay), or clients keep serving stale cached
99 /// values.
100 void SetClientStatesdirty(FString ClientThatUpdate);
101
102private:
103
104 // UPROPERTY so the GC can see these references: a bare map dangles when
105 // World Partition streams out a lamp before its EndPlay unregisters it.
106 UPROPERTY()
107 TMap<int, UCarlaLight* > Lights;
108
109 // Last state NotifyDayTimeChange broadcast, applied (next tick) to lights registering later.
110 void ScheduleDayTimeState(UCarlaLight* CarlaLight);
111 bool bHasDayTimeState = false;
112 bool bLastIsDay = true;
113 // set_day_night_cycle from the client; off, day/night changes are ignored.
114 bool bDayNightCycle = true;
115 TSet<TWeakObjectPtr<UCarlaLight>> PendingDayTimeLights;
116
117 // Flag for each client to tell if an update needs to be done
118 TMap<FString, bool> ClientStates;
119 // Since the clients doesn't have a proper id on the simulation,
120 // I use the host : port pair.
121
122};
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FDayTimeChanged, bool, bIsDay)
TMap< int, UCarlaLight * > & GetLights()
bool GetDayTimeState(bool &bOutIsDay) const
The day/night state last broadcast, if any.