Download

Object Reference, Class References, and Wrapper Classes

I find myself needing to provide generic functions that have specific implementations for specific objects. So I tried to use wrapper classes like I’m used to in Android programming: a base class that specifies the function signature, and instantiating a specific implementation for what I need at the moment. As luck would have it, my first go at this requires iterating through an array of generic functions.

I thought either an Array of Object References or Class References would allow me to do this; the problem is, while I can apply the desired Class Reference at design time, this does not allow me to actually call the function in the class; if I instead use Object References, I can call the functions, but I can’t figure out how to provide an instance.

So I thought I would combine the two: expose the Array of Class References to the designer, then instantiate an Array of Object References from that Array in Begin Play, but I am getting the error: “Cannot construct objects of type ‘MyType_C’ in Construct Object from Class” when I try.

What could be causing this error; or ideally, what is the best way to handle my need to iterate over a list of glorified function pointers?

Can you provide a Pic of the BP Code so we can get a better understanding of what you are trying to do?

I might not be understanding what your trying to do but when you combine them maybe try a Delay .001 node between them.

I’m trying to make a system where it does different actions at different health levels. This is the DoDamage function, where the HealthEventsInstances are iterated to check whether they triggered:

And this is the BeginPlay Event, where I am constructing the Instances from a list of the subclasses:

Can you post a pic of “Construct Health Level Event”? Also what exact error are you getting on it?

Maybe try a Switch on int or a Switch on Enumerator with the Enum as the health events. Might be easier then a for loop. Not 100% sure it will work for what you are trying to do.

ConstructHealthLevelEvent is an engine-provided function. The error is “Cannot construct objects of type ‘(path)HealthLevelEvent.HealthLevelEven_C’ in Construct Object from Class”.

No, a switch statement would not work.

Gotcha … never used the Construct object from class node.

How about using an Actor Component class. Have that class construct the health event object references, pass to an array. Add this class to your character.

Tried to recreate your bug and could not.

I know from previous experience that while changing things around with arrays I had to rewrite the code in order to get the reference to work again. Try placing a new array reference then dragging a new for each loop from your array then a new construct node from the array element. Also the make sure the object is an object class you will get an error when trying to construct an object from an actor class.

Both the class containing this code and the HealthComponent class are already ActorComponents.

I tried it with a new Class Reference array and still get the same error. What was the base class you were using? Could it be because I’m creating an ActorComponent inside an ActorComponent? I’ve previously tried passing it the Owner for the Outer argument, as well as constructing the objects as needed instead of BeginPlay, but none of those seem to work. I think I’m going to try reducing the HealthEvent to an Object (which will destroy other functionality, but it might prove useful for diagnosis) and see what happens.

…Aaaand I’m having trouble changing it back to Object, looks like this going to take some research.

Okay, made a new class that derives from Object instead; it looks like it being an ActorComponent was the problem. Now I need to figure out a way to support either constructing an ActorComponent from within an ActorComponent, or Resetting the Level from within an Object (fortunately I don’t have a whole lot of Blueprint to copy from class to class).

If you create an Actor Component you can just add it to any actor form the component menu or you can add it at runtime by right clicking on the BP graph and searching for the components name. The node “Open Level” will load the named level and should reset/reload your level.