How do I create an instance of a UClass?

Hello, I’m looking for help in making an instance of a uclass in UE4.

My class looks something like this:

UCLASS(Abstract)
class  MYPROJECT_API AUpgrade : public AMyActor
{
    GENERATED_BODY()
public:
    int32 GetMyInteger(); // Returns MyInteger.
private:
    int32 MyInteger;
};

I’ve tried making pointers to the class like you would a UStaticMeshComponent, but that doesn’t work, when calling my function UE4 crashes. I’ve read a couple other threads, but they don’t seem to solve my problem.

Thanks everyone :slight_smile:

You need to actually create the object somehow. As this is an actor it could be something that’s already in the world via placing it in the editor, or perhaps it something you spawn with SpawnActor ( http://api.unrealengine.com/INT/API/Runtime/Engine/Engine/UWorld/SpawnActor/4/ )

Regardless of how it’s spawned, once it exists then you use pointers to access it like you would anything else. The trick is being able to access this specific actor whenever you want it. You could cache pointers to it when spawning or you can iterate through actors in the world to find it (the latter can be slow so I recommend caching instead).

If you’re still having trouble then post how your spawning or trying to access the actor and we might be able to help more.

You might also want to watch out got garbage collection. If Unreal things you no longer need this object / actor then it might clean it up, but it doesn’t seem like this is your problem at the moment.

You got Abstract specifier on it, you can’t make instance of it if it’s a abstract. It’s not abstract class in C++ sense (you need undefined virtual functions in it to do so), but if you mark it as abstract UE4 will keep a guard on that and prevent you to instantiate this class. you need to create other class that inhere from this one and create instance.