C++ class in visual script (Use C++ class with constructor arguments)

Hello, good day! Recently, I have started tinkering with Unreal Engine 4.12 and C++. It is very exciting indeed! For your discretion I started learning the engine and C++ about 5 days now so don’t expect too much. I apologize if there are things I might have done wrong.

I did my research about this but did not find the most appropriate answer to my question (or maybe the problem is just me).

Let me just give a quick run through what I want to do:

1.) I want to create a class named ItemManager.
2.) This class will have 2 integer arguments in its constructor: (int itemID) (int itemType)
3.) I created a C++ class in Unreal Engine and set Actor as its parents class. Note: This class as an actor because this will also have its own static mesh and texture materials which will be used when this item will be spawned in the World. What I’m thinking is if an item is dropped on the ground, it will use that static mesh to display how it would look - well - when it’s on the ground. (For some reason I do not prefer an item looking exactly how it looks when spawned in the world. Moving on…)

4.) In ItemManager.h I have this:

#pragma once

#include "GameFramework/Actor.h"
#include "ItemManager.generated.h"

UCLASS(Blueprintable)
class MYPROJECT_API AItemManager : public AActor
{
	GENERATED_BODY()

public:

	//For some reason it requires this I don't know why
	AItemManager(const FObjectInitializer& ObjectInitializer);
	
	// Construct that I want
	AItemManager(int, int);

	// Item Manager Properties
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Item Properties")
	int itemID;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Item Properties")
	int itemType;

	UFUNCTION(BlueprintCallable, Category = "Item Manager Methods")
	int getItemID();
	
}; 

5 .) In my ItemManager.cpp is this:

#include "MyProject.h"
#include "ItemManager.h"


//For some reason it requires this I don't know why
AItemManager::AItemManager(const FObjectInitializer& ObjectInitializer): Super(ObjectInitializer){}

//Overload constructor
AItemManager::AItemManager(int x, int y)
{
	itemID = x;
	itemType = y;
}

int AItemManager::getItemID() {
	return itemID;
}

6 .) It compiles well and the class shows up in Unreal Engine.

96979-check.jpg

7 .) Now the real question is, how will I be able to use this in my blueprints? I tried Cast to I’m not sure what that does.

I have a different person (also learning) working on picking up the item from the ground and sending them to the inventory. We are using begin overlap, what I want to know is How to incorporate a C++ class in Visual Scripting.

Thank you so much!

You can’t instantiate UObjects (which includes actors) using constructors because it skips entire code required to manage this object in UE4 systems, reflection system and memoery managment. UObject have specific use conventions, you use constructor only to set defaults, it will be used to create class default object which will hold default values and to create new object and you can instantiate UObject only via NewObject function for non-Actor classes and SpawnActor (in UWorld so use ())for Actor classes (you put class name as a template in that function, or UClass* as argument), so you never call you never call constructor by yourself.

If you want to make spawning with arguments make a static function that will do so, UE4 it self does that with SpawnActor having spawn settings argument, some classes also have own create functions, remember to make that function return object it spawn so you can actully control it.

Note this still standard C++, so you can normally make and use non-UObject classes and also structs, but reflection system won’t support those so you can’t use those in blueprints or else you create wrapper (like UMG for Slate), blueprints also don’t support struct functions, but you can still use them in C++ too