How do you make a new instance of a class? In c++ it’s pretty easy considering all you’ve got to do is just var = new class(); but how do you do it in blueprint?
To give an example for what I mean by that, is if I have class A with variable B, let’s say for example that var B is from the class “animal”, and now I want to put inside an instance of a cat for example. How do I create the new instance?
UObject::NewObject is a pure C++ function, but you’ve already found it’s BP equivalent - Construct Object from Class, and that’s the right one to use.
This node does a lot of checking to validate the passed data.
Probably, your “Ability BP” Class is “illegal” to use with it.
Could you provide more info about your class?
I noticed that You unticked the Context Sensitive box (see screenshot below).
Is this node appearing when this box is ticked on?
After taking a look at this source code, your Class could be either Abstract, Depricated, isn’t BlueprintType or “has a newer version” (whatever this one means).
That’s not really true. You can use this aproach in standard C++ but not in Unreal.
UE supports Reflection system and Garbage Collection, and the new and delete kewords can mess with those, so they are pretty much illegal.
For the info about the class, what would you like me to show?
I’m just really not sure where the problem can come from…
Its variables so far are only floats, integers & one name-to-bool map.
It has multiple events and no functions (the events only changes the variables’ values and some call other events but no recursion)
If I just try to create the node by right-click the blank board then yes, but if I try to connect the “SET ability1” to blank-board it then does not show the construct ability as an option which is why I tried turning the “context Sensitive” off
Tbh I didn’t know about the ‘new’ problem. I was aware of using ‘destroy’ instead of delete but not that new had a different keyword. thank you for the new information!
There’s noting to apologize for, I’m always glad to learn new things, especially in c++ considering that there’s really not a lot of information compared to the amount of info about blueprints.
As I remember, new keyword is overloaded in Unreal, so it’s not an equivalent of standard new.
Still you should be using safer functions to instantiate, like NewObject, UWorld::SpawnActor or UActorFactory::CreateActor
ahh, sorry, I could precise it right away…
I’ll need the name of base Engine class that you’ve used to inherit your “Ability BP”.
Also, if you changed any default values of this class, tell me which ones.
If this class is implemented in C++, you can paste the full snipped of .h file and its constructor from .cpp.
A blueprint that has all the same functionality as the parent but with its own settings sounds like what you need right?
Edit: I read OP again. It doesn’t even seem like you want parent/child. You want to just make another version of that class. That sounds like you could just duplicate it.
Edit again: I see, you seem to be trying to do this as part of the coding. Would spawn actor from class node work?
Edit 3rd: Ah, dzem below seems to have gotten the node idea 2 seconds before me.
Okey, now I think I understand what the problem is.
You see, unlike regular UObjects, AActors are expected to live within Game World.
To instantiate any subclass of AActor, use Spawn Actor from Class instead.
However, I belive your “Ability BP” is not suppoused to be something that actually lives in the Game World, right? If I understood correctly, it is only ment to hold some data (like atributes) and perform simple logic based on this data?
In this case, choosing the AActor as parent of this class is wrong, you should use UObject instead. Then using this node will be possible.
Still I’m not understanding the full concept behind this, so I’m maybe suggesting wrong here.
Sorry for not being clear enough, I"ll try to explain what I’m trying to get in order to make things more clear.
The player in my project has 4 different abilities - each “ability” is an instance of a child class of ability (for example one ability can heal the player, one can shoot projectile etc… all are abilities but they are child class of ability), however I do not know what ability the player has since it can change its abilities.
so let’s say I want to give the player an ability from the class abilityB (child of the class Ability).
I need to create an instance of AbilityB and put in the variable ability1.
You do understand right. and I’m glad to finally understand the reason behind the function not working.
My only problem and the reason I chose actor and not object, is the usage of delay.
Since abilities have recovery and cooldown I need to have access to the delay node, something normal objects sadly don’t have (at least from my experiments).
Is there an option to get a normal class that has access to delay?
The Delay Node is just a fancy way of using Timers.
Timers need access to Game World, so that explains why Delay node is not accessible for UObjects.
There are few ways how you can fix it:
If you don’t want to restructure your code futher, you can keep using AActor as parent for “AbilityBP”, and spawn it via Spawn Actor from Class. This aproach will be messy though. You’ll need to keep reference to these actors and destroy them afterwards. This just wouldn’t be too fancy
You can change parent Class of “AbilityBP” to UActorComponent. Components live on specified Actors and they have access to the same World, so it will work well in your case, and will provide the access to Delay Node. You can spawn actors at runtime via Add Component by Class node.
You can still prefer using UObjects, but for this to work, You’ll have to access World instance manually and setup Timers on your own. However, I’m not sure if this is possible to do in BP. This will also produce a lot of spaghetti code tbh.
Imo, go with option 2. If you don’t know anything about Components, it’s a greit opportunity to learn about them.
Option 2 sounds really good, however I’m not sure how can I reparent my AbilityBP to UActorComponent.
I can create a new UActorComponent and also reparent my AbilityBP (but not to a UActorComponent).
Is there a way to reparent the AbilityBP to UActorComponent and/or copy the variables & custom event of the current AbilityBP actor?
Edit: I did manage to copy the values and the functions to the new class. However I’m still curious if there’s a way to reparent an actor to an actorComponent.
Thank you very much for all the help!!