Create a globally accessible reference to my Day/Night cycle.

I am needing to constantly access my Day/Night cycle blueprint from tons of other blueprints, and it is rather annoying to constantly pass references, or cast to it (Or cast to other things to get a reference to it). What are my options in terms of accessing my Day/Night Cycle blueprint without all of this casting or reference passing? Is there any way that I can create a function in a function library that casts the first time, and spits out a reference without casting every other time? Is this something that will require a little bit of C++ work?

I could’ve sworn I posted a thread just like this earlier, but can’t seem to find it, so forgive me if it is a duplicate thread. If anyone knows any sort of solution to my problem, that would be great!

Depends on the form of your blueprint. How are you getting the first reference to it currently? Is it an actor blueprint, that’s placed in your level?

We use Dynamic Sky and there is a var for Night and Day. Now inside the level bp we check on tick (delay 5) if the referenced DS is day or night. When its night all torches start burning. Actually checking for time of day which is in float, so we can tweak the D/N cylce in regards to the current visibility.

I need something like this for airplanes to run calculations regarding state of atmosphere. The idea was to create a blueprint that manages atmosphere, spawn it using game mod - to be sure that it’s a singleton.
Then build a custom script component that will look for and store a reference to atmosphere manager in Begin Play event. Then you just add this component to anything that needs to talk to atmosphere manager.

Right now it’s just an actor blueprint placed in the level. For some items, I just expose a day/night reference, and set it, but for others, I am forced to use ‘Get All Actors of Class’ if they are spawned in the level at run-time.

Yeah, the ‘GetAllActorsOfClass’ approach is kind of ugly, and also not very efficient. That said, the simplest way to solve your issue would be to just do that within the BeginPlay of a blueprint, and store the result in a variable on the blueprint. Then everywhere else in the blueprint, you just access the cached version in the variable.

Another approach would involve storing the master reference to your object somewhere global, such as in a variable on GameMode/GameState. That variable could be set on level start using ‘GetAllActorsOfClass’, or alternatively, you could not place the actor at all and just have the GameMode spawn it in its BeginPlay, and store the result.
Then you would use a blueprint function library, which you can create either in blueprint or in C++, to provide easy access. Just add a function called ‘GetNightAndDay’, which calls GetGameMode -> Cast To MyGameMode -> Get Night & Day Variable. You can then invoke that function from any blueprint, and won’t need to cast.

So I can set up a function in a function library that pure casts, and it will only cast the first time due to it being pure?

Sorry, when I said you won’t need to cast, I meant that you won’t have to add a cast node in every blueprint from which you want to access the object, since it will be casted in the function library and returned as the specific type you are after. The cast node will still have to be processed on each call though, since you can’t store any state in a function library.

I thought you were asking from an ease of use point of view. Maybe I misunderstood, are you in fact concerned about performance? If so, you really shouldn’t be. Doing a cast is pretty light, you’d have to be accessing the object inside a loop inside a tick function for it to even start to be an issue. In that unlikely case, you’d just cache it in a local variable first.

I am indeed concerned about performance, as there can be thousands or possible tens of thousands of items being placed. There are also many things pre-placed in the level, and it can be a hassle to go through each one of them and pick the day/night actor from the level as a reference. Optimally, I would have a ‘Get Day/Night’, just like ‘Get Player Controller’ that returns a reference. I am willing to work in C++ in order to accomplish what I need, I just don’t know where to look.

I’d create a custom GameInstance class, add a static var and assign the target object to it once; right after scene is loaded.
That way I’d have to call *GetAllActorsOfClass() + cast a single time when the level is loaded.

Well, if performance is genuinely an issue, there won’t be any other approach that can compete with just caching a reference in a blueprint’s BeginPlay.

That said, I think if you have thousands of items all needing to access the night and day object in their tick (or a high frequency timer) then you probably have a more fundamental design issue. Casting is not going to be the cause of your performance bottleneck.

Anyway, if you want to do this in C++, just create a static UFUNCTION, either in a UBlueprintFunctionLibrary or any other C++ UCLASS you have. See here for an example. You can see though that it’s inevitable you’ll have to do a bit of lookup each time this is called (GetWorldFromContextObject) if you want to get a reference to something in the world from a static function that you can call from any blueprint.

I’m in the same situation! The variable has to be accessible from multiples levels…
My problem is my game is based on the unreal shootergame exemple, it’s in C++ and I don’t understand C++.
I need to create a variable in the ShooterGameInstance.h? .cpp? that store the value (true/false) of my BP variable “IsDay”.
Where and which code i have to add?

just create a static UFUNCTION => A real challenge when starting at 0!

First, make sure you store any references in a variable of the exact same type as the day/night object, not just the actor type. This will prevent unnecessary casts.

As others have said, it is easiest to store the reference in a globally accessible object like GameInstance. It might even be best to have the day/night system in the GameInstance object. That will make it even easier to access.