x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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.

Product Version: UE 4.11
Tags:
more ▼

asked Jul 06 '16 at 06:20 AM in C++ Programming

avatar image

GhostRavenstorm
66 15 14 21

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

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 :)

Good Luck

more ▼

answered Jul 06 '16 at 08:18 AM

avatar image

Cloudy McStrife
405 11 7 25

avatar image GhostRavenstorm Jul 06 '16 at 05:41 PM

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.

https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/GameFramework/AActor/index.html

I tried this example: https://answers.unrealengine.com/questions/21911/setstaticmesh-and-setmaterial.html 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.

avatar image GhostRavenstorm Jul 06 '16 at 05:52 PM

alt text

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

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

untitled.png (36.6 kB)
avatar image GhostRavenstorm Jul 06 '16 at 06:08 PM
 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;
     }
 }
avatar image GhostRavenstorm Jul 06 '16 at 06:14 PM

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

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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

  • for (TObjectIterator 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.

more ▼

answered Jul 06 '16 at 08:16 AM

avatar image

NymKappa
637 21 11 41

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question