Call UpdateSunDirection function in the SkySphereBP from C++, is it possible?

Hi everyone I am just getting into Unreal C++, and I was wondering if it is possible to call a blueprint function using c++.

I am writing a fairly advanced day/night cycle and it would be inefficient to do all the calculations in blueprints, so I’m writing the calculations in c++ and then want to rotate the light source and call the update skySphere function.

Is this possible, if yes then how?

FOutputDeviceNull ar;
mSkySphere->CallFunctionByNameWithArguments(TEXT(“UpdateSunDirection”), ar, NULL, true);

but i still dont now how to set the actor pointer into spawned blueprint actor:

//UObjectProperty* ObjectProp = FindField<UObjectProperty>(mSkySphere->GetClass(), FName("Directional light actor"));
UObjectPropertyBase* ObjectProp = FindField<UObjectPropertyBase>(mSkySphere->GetClass(), FName("Directional light actor"));

ObjectProp->SetObjectPropertyValue_InContainer(mSunLight, mSkySphere); // FAILED

okay, found the right method:

UObjectPropertyBase* ObjectProp = FindField<UObjectPropertyBase>(mSkySphere->GetClass(), FName("Directional Light Actor")); // not case sensitive

if(ObjectProp) {
	ObjectProp->SetObjectPropertyValue_InContainer(mSkySphere, mSunLight); // first parameter is sky sphere and second the directional light

	//// check only
	void* ObjectPtr = ObjectProp->GetObjectPropertyValue_InContainer(mSkySphere);

	if(ObjectPtr) {
		ADirectionalLight* actor = static_cast<ADirectionalLight*>(ObjectPtr);
		ULightComponent* component = actor->GetLightComponent();
		
		// compare xy
	}
	
	// calculate
	FOutputDeviceNull ar;
	mSkySphere->CallFunctionByNameWithArguments(TEXT("UpdateSunDirection"), ar, NULL, true);
}

Hi!

How can i define mSkySphere and mSunLight?

If your SkySphere and directional light (sun) are already created in the editor, you can search for them in your c++ code (in BeginPlay for example) and store the reference to it there.

Define the reference like this:

UPROPERTY()
AActor* mSkySphere = nullptr;

UPROPERTY()
ADirectionalLight* mSunLight = nullptr;

Search for the actors like this:

for (TActorIterator ActorItr(world); ActorItr; ++ActorItr)
{
	if ((*ActorItr)->GetName().Equals("SkySphere")) // Adapt to the name of your Sky Sphere
	{
		mSkySphere = *ActorItr;

		// quits the loop if both actors are found
		if (mSunLight != nullptr)
		{
			break;
		}
	}

	if ((*ActorItr)->GetName().Equals("DirectionalLightSun")) // Adapt to the name of your directional light (your sun)
	{
		mSunLight = (ADirectionalLight*)(*ActorItr);

		// quits the loop if both actors are found
		if (mSkySphere != nullptr)
		{
			break;
		}
	}
}