C++ Constructors /w Blueprints

Hello.

I’ve been searching for how to do constructors in c++, I used them a lot in c# when I used Unity.
I found this: Constructors (C++) | Microsoft Docs but I can’t figure out how I’d make this usable in blueprint.
Anyone got any ideas? :slight_smile:

I’m sorry, but I don’t understand your question.

What does the constructor have to do with blueprint?
If you want to do some initialization in Blueprints, use the ‘Begin Play’ event.
In C++ the equivalent will be PostInitCompoenents (and there is PostBeginPlay IIRC). This is the place where all references will be valid. Doing initialization in constructor may not work all the time.

Mindfanes confusion is understandable but I think you’re looking for the Construction script.

…but wont they get triggered every time you move the BP instance in the map?

They will but the behavior is more like removing the instance and placing a new one at the new position (or whatever was changed) and less like having an event function trigger whenever you touch something.

Depends on the definition of danger but I tried a construction script to spawn a 10x10 grid of actors around the script owners location. I would spawn ActorComponents and store them in an array. Each time the script ran it would kill the old copies, clear the array and create new ones. It worked but sooner or later the garbage collector will place a bounty on your head :frowning:

Thanks. I was worried about this. So there is no danger of re-initialization of existing subcomponents and variables then? Great.

I’m not talking about the blueprint constructor, but I’d like to make a class constructor in c++, and make it usable in blueprint.
Thanks for the replies, but I can post an example of what I used constructors for in Unity.

    public class Item
    {
        public string name = "";
        public ItemType type = ItemType.None;
        public float damage = 0f;
        public float armor = 0f;
        public float speed = 0f;
        public float movespeed = 0f;
        public float strength = 0f;
        public float agility = 0f;
        public float intellect = 0f;
        public int id = 0;

        public Item(string Name, ItemType Type, float Damage, float Armor, float Speed, float Movespeed, float Strength, float Agility, float Intellect, int ID)
        {
            name = Name;
            type = Type;
            damage = Damage;
            armor = Armor;
            speed = Speed;
            movespeed = Movespeed;
            strength = Strength;
            agility = Agility;
            intellect = Intellect;
            id = ID;
        }

This is my Item class constructor for my inventory etc. and I’d like to make something like this which I can use in blueprint. Sorry for any confusion.
And by use it in blueprint I mean like making an array of Item and using it for my inventory etc.

I looked at structs, and they seem very similar to c# constructors, could anyone give me some hints at structs which are usable in blueprint? :slight_smile:

I figured it out, constructors in c# are basically the same as structs.

If you solved your problem that’s great but this is wrong and makes no sense. Constructors are the same in all languages and they have nothing to do with structs.

@Wrekk.
I’m gald you solved the issue. But like the commenter above me, I think you’ve got it all wrong. Constructors have nothing to do with structs.

Here is a small description of both in terms of UE4 (and sure this is applicable to most Programming languages)
Constructor:
A constructor is a special member function of a class which will get called at the time of creation of an object of that class. Usually constructors are used to initialize member variables and resources. Most constructor allow parameters which can be passed from where the Object is being created. This is useful to initilize the Object with some custom values.

Eg: vector = FVector(0.0, 10f, 0.0)

This is supported in UE4 C++. But in blueprints, the closest you can get is the construction script. However they do not have any parameter support. Instead you can achieve the same using the ‘Default properties’ of the instantiated blueprint object which is easier.

Struct:
A struct is short for structure. They are constructs where we can define more than one components of different types. They are NOT functions. They are very similar to classes ; but less powerful and more efficient. Structs are used for storing related data together into a pack. Their main use is to store data. They are not supposed to contain any logic. However structs can have constructors. Just like a normal constructor, this is used to initialize the member properties to custom values. And in UE4 blueprints, structs can be initialized without any fuss.

I hope I am clear.

I’m not sure it will have the same principles with constructor but you can assign variables on instanced blueprints when you create them. For example you created a blueprint with a name of item and three int variables (x,y,z). You can instance another item variable on another blueprint and set new variables on them by using set command. Here is a basic one. You can call your functions from instanced blueprint too.

Here is better solution for your example. If you are going to make a lot of instances from item, you can make a function for constructing and call it when you instance the class.

As far as I know, there is no c-tors (in terms of C++) in Blueprints. ConstructionScript (in BP) = OnConstruction (in C++). The only functionality BP has from c-tors is to set default values for primitive types.