Tencent / UnLua

A feature-rich, easy-learning and highly optimized Lua scripting plugin for UE.
Other
2.24k stars 614 forks source link

UMaterialInstanceDynamic? #687

Closed jvukovich closed 5 months ago

jvukovich commented 9 months ago

How can I use UMaterialInstanceDynamic in UnLua?

I tried a bunch of variations, but nothing worked (always got nil errors trying to call Create()):

UE.UMaterialInstanceDynamic:Create(MaterialAsset) UE.UMaterialInstanceDynamic.Create(MaterialAsset) UMaterialInstanceDynamic.Create(MaterialAsset)

And a bunch of other variations, but no luck.

I tried looking at the UnLua code, but wasn't able to figure it out.

Thanks!

jozhn commented 5 months ago

You can't create UMaterialInstanceDynamic in lua directly, because Create is not a ufunction in Unreal Engine source code.

static UMaterialInstanceDynamic* Create(class UMaterialInterface* ParentMaterial, class UObject* InOuter);

But you can write a wrapper ufunction for Create in your own UClass:

In c++

// in your UClass
UFUNCTION()
static UMaterialInstanceDynamic* Create(UMaterialInterface* ParentMaterial, UObject* InOuter);

// skip implementation

In lua:

local Ret = UE.YourUClassName.Create(MaterialAsset)
jvukovich commented 5 months ago

Ok, cool, thank you! I did ultimately create a wrapper to get around it; thank you for confirming and the explanation!