You just have to create the class of your custom BP function library and then you will be able to use those functions in your BPs, there is no need to create a new instance.
To create global structs I would encourage you to just create a simple header file in your public code, then include that header in your main project header to you will be able to have access to the structs from every where. This way you have visibility in both native code and BluePrints.
Native (C++) Blueprint Function Library
The next code is basically the same as in Ramas tutorial. We just add the header file (.h) and the implementation (.cpp). The next think is simple, compile your project.
//-----------------------------
// MyBlueprintFunctionLibrary.h
//-----------------------------
#pragma once
#include "MyBlueprintFunctionLibrary.generated.h"
UCLASS()
class UMyBlueprintFunctionLibrary : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
public:
UMyBlueprintFunctionLibrary(const FObjectInitializer& ObjectInitializer);
/**
* Saves text to filename of your choosing, make sure include whichever file extension
* you want in the filename, ex: SelfNotes.txt . Make sure to include the entire file
* path in the save directory, ex: C:\MyGameDir\BPSavedTextFiles
*
* Note: From Rama
*/
UFUNCTION(BlueprintCallable, Category = "GS Function Library")
static bool SaveStringToFile(FString SaveDirectory, FString FileName, FString SaveText, bool AllowOverWriting = false);
};
//-------------------------------
// MyBlueprintFunctionLibrary.cpp
//-------------------------------
#include "MyProject.h"
#include "MyBlueprintFunctionLibrary.h"
UMyBlueprintFunctionLibrary::UMyBlueprintFunctionLibrary(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
}
bool UMyBlueprintFunctionLibrary::SaveStringToFile(FString SaveDirectory, FString FileName, FString SaveText, bool AllowOverWriting)
{
//Dir Exists?
if (!IFileManager::Get().DirectoryExists(*SaveDirectory))
{
//create directory if it not exist
IFileManager::Get().MakeDirectory(*SaveDirectory);
//still could not make directory?
if (!IFileManager::Get().DirectoryExists(*SaveDirectory))
{
//Could not make the specified directory
return false;
}
}
//get complete file path
SaveDirectory += "\\";
SaveDirectory += FileName;
//No over-writing?
if (!AllowOverWriting)
{
//Check if file exists already
if (IFileManager::Get().GetFileAgeSeconds(*SaveDirectory) > 0)
{
//no overwriting
return false;
}
}
return FFileHelper::SaveStringToFile(SaveText, *SaveDirectory);
}
Now we have compiled the code (it hopefully went all ok :D). The next step is to simply launch the editor and you are ready to use your functions in your Blueprints. This might seam strange but you do not have to create a child of UMyBlueprintFunctionLibrary in fact the engine wont let you to do so. The next screenshot is directly after I compiled the engine, just right click in any Blueprint and search for your new functions.
Notice the ‘Save String to File’ function?
Native structs to be used in Blueprints
When you setup a C++ project Unreal will create a main header file for your project (normally it’s just the name of the project itself). You will have something like this:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "Engine.h"
What I normally do for structs that I consider global and so I will use them a lot in my code and in Blueprints (if it’s only for Blueprints you can add them nearly everywhere but the sake of clearness we will discuss the nice way here) is to add them in a separate header. The next code outlines the use of the types header and the projects main header file.
//
// MyProject.h
//
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "Engine.h"
// Your types that you in runtime (non-editor)
#include "MyTypes.h"
// Your types that needs editor stuff (done exactly the same way as MyTypes.h)
#if WITH_EDITOR
#include "MyEditorTypes.h"
#endif
//
// MyTypes.h
//
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
USTRUCT(BlueprintType)
struct MYPROJECT_API FNameItem
{
GENERATED_USTRUCT_BODY()
/** Name of the item */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Item)
FName ItemName;
#if CPP
FNameItem() :
ItemName(NAME_None)
{}
#endif
};
FNameItem is a struct you can use nearly from every where in your code and in Blueprint.