Hi I’ve been trying to get this guide to work https://github.com/rdeioris/LuaMachine/blob/master/Docs/TipsAndTricks.md#mapping-blueprintfunctionlibrary-to-userdata but no matter what I do I can’t get it to work. The problem is when ever I try to call one of the BlueprintFunctionLibrary functions it says attempted to call a nil value. below is what I did while following the guide. Is there some extra step I have to do? If anyone was able to get this to work please let me know what I’m doing wrong.
The error
My setup
C++ code
BPFLLuaBlueprintPackage.h
#pragma once
#include "CoreMinimal.h"
#include "LuaBlueprintPackage.h"
#include "UBPFLLuaBlueprintPackage.generated.h"
/**
*
*/
UCLASS()
class LUAMACHINE_TEST_API UBPFLLuaBlueprintPackage : public ULuaBlueprintPackage
{
GENERATED_BODY()
public:
UBPFLLuaBlueprintPackage();
UFUNCTION()
FLuaValue RequireBlueprintFunctionLibrary(FLuaValue LibraryName);
};
.cpp
UBPFLLuaBlueprintPackage::UBPFLLuaBlueprintPackage()
{
Table.Add("require", FLuaValue::Function(GET_FUNCTION_NAME_CHECKED(UBPFLLuaBlueprintPackage, RequireBlueprintFunctionLibrary)));
}
FLuaValue UBPFLLuaBlueprintPackage::RequireBlueprintFunctionLibrary(FLuaValue LibraryName)
{
FWorldContext* world = GEngine->GetWorldContextFromGameViewport(GEngine->GameViewport);
UWorld* W = world->World();
ULuaState* L = FLuaMachineModule::Get().GetLuaState(GetLuaState(), W->GetWorld());
if (L != nullptr) {
if (GEngine)
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, L->GetClass()->GetName());
}
UClass* FoundClass = Cast<UClass>(StaticFindObject(UClass::StaticClass(), ANY_PACKAGE, *LibraryName.ToString()));
if (FoundClass)
{
ULuaUserDataBPFL* UserDataBPFL = NewObject<ULuaUserDataBPFL>(L);
UserDataBPFL->InitializeWithClass(FoundClass);
return FLuaValue(UserDataBPFL);
}
return FLuaValue();
}
LuaUserDataBPFL.h
#pragma once
#include "CoreMinimal.h"
#include "LuaUserDataObject.h"
#include "ULuaUserDataBPFL.generated.h"
/**
*
*/
UCLASS()
class LUAMACHINE_TEST_API ULuaUserDataBPFL : public ULuaUserDataObject
{
GENERATED_BODY()
public:
void InitializeWithClass(UClass* InClass);
virtual FLuaValue ReceiveLuaMetaIndex_Implementation(FLuaValue Key) override;
protected:
UPROPERTY()
UClass* BPFLClass;
};
.cpp
void ULuaUserDataBPFL::InitializeWithClass(UClass* InClass)
{
BPFLClass = InClass;
}
FLuaValue ULuaUserDataBPFL::ReceiveLuaMetaIndex_Implementation(FLuaValue Key)
{
UFunction* Function = BPFLClass->FindFunctionByName(*Key.ToString());
if (Function)
{
return FLuaValue::FunctionOfObject(BPFLClass->GetDefaultObject(), Function->GetFName());
}
return FLuaValue();
}
Lua code
Lua state config