How to reference a UObject in scene and modify its properties at runtime in C++?

I have custom classes I made and I’m doing all the logic in C++, no blueprints. I can’t figure out how to get the components on an object Unity3D style and modify certain properties like visibility or material color. UMG seems to be its own thing too because I have no clue how to reference anything in there in a custom Pawn class at runtime. Would I need a UOject or UClass variable to do this? The documentation isn’t clear enough for me to understand.

Pseudo code, I basically want to do this…

//SomeClass.h

UPROPERTY( <all the stuff> )
UObject *thingy;

//SomeClass.cpp
SomeClass::DoAThing(){
    thingy->materialColor = Color.Red;
}

The Unity way would be

//SomeClass.cs

[SerializeField]
GameObject thingy;

void DoAThing(){
   thingy.GetComponent<Renderer>().material.color = Color.Red;
}

And that’s basically what I’m trying to do.

I know two way to reference scene objects in C++ right now:

  • Using TObjectIterator. For example here, I will get all APOLICE_VIVE_Character currently in my world and iterate on them

    for (TObjectIterator<APOLICE_VIVE_Character> Itr; Itr; ++Itr)
    {
    APOLICE_VIVE_Character* VIVECharacter = *Itr;
    // Do stuff with you reference
    }

  • Reference the object directly from the editor. For example your class SomeClass could have a member:

    class SomeClass
    {
    UPROPERTY(EditDefaultsOnly, Category = “WhateverYouWant”)
    UObject *thingy;
    }
    Then create a derived blueprint in the editor and reference this blueprint in your project (whether by using it somewhere in the editor or by spawning it in your level, depending of what your class is designed for).
    In this blueprint, in the Class Defaults section you will be able to set the thingy reference you need.

Hope this help.
Good luck! Don’t surrender, I’m also quite new in Unreal Engine and I know how hard it is to get into.

Best regards.

1 Like

Hey Ghost

In Unreal it’s the same as in Unity.

You have a public Variable which can be edited in Editor.

In Unreal you have to specify the visibility of a Variable through the UPROPERTY macro.

So:

//.h
void DoAThing();

UPROPERTY(EditAnywhere, Category = "MyCategory")
UObject* ObjectToReference;

//.cpp
void Class::DoAThing()
{
    if(IsValid(ObjectToReference)
    {
        //...Do stuff with ObjectToReference. 
        //(GetComponents etc. is only available when UObject is an AActor)
    }
}

You can reference “ObjectToReference” by dragging the Object which implements DoAThing into the Scene and filling
“Object To Reference” in the Details Panel.

But be aware that UObject don’t have any Components except they are AActor.
So if you just want a reference to an actual Actor use instead of UObject* AActor*

I hope I could help you :slight_smile:

Good Luck

AActor, that’s what I was looking for, Thanks.

But I still don’t know how to get what I want. Would I use AActor::GetcomponentByClass()? What are the names of the components I can get? I see “Static Mesh” and “Materials” in the details panel, but I don’t know what those are in code. I can’t seem to find example code in the documentation.

I tried this example:

But it didn’t work for me because its referencing the mesh directly where I want to reference the whole actor, get the mesh component, then set material.

97398-untitled.png

Is this the name in the heierachry of the component I want? StaticMeshComponent?

AActor *actor;
actor->GetComponentByClass(TSubclassOf >StaticMeshComponent< meshcomp);

void AMyActor::Tick(float DeltaSeconds){
Super::Tick(DeltaSeconds);

	int rand = rand() % 4;
	UStaticMeshComponent *comp = actor->GetComponentByClass(TSubclassOf<UStaticMeshComponent> meshcomp);

	switch(rand){
		case 0:
		comp->SetMaterial(0, Material_Red.Object);
		break;

		case 1:
		comp->SetMaterial(0, Material_Blue.Object);
		break;

		case 2:
		comp->SetMaterial(0, Material_Green.Object);
		break;

		case 3:
		comp->SetMaterial(0, Material_Yellow.Object);
		break;

		default:
		break;
	}
}

I’m not sure what to put for the second argument of SetMaterial(); I have 4 materials made.

Hey Ghost
loook into my answer in your other Question :slight_smile:

is probably what you are looking for