Tencent / UnLua

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

Dynamic Binding not working correctly v2.3.6, UE5.3 #678

Open V2iDanAbend opened 10 months ago

V2iDanAbend commented 10 months ago

Hi,

The Dynamic Binding functionality is not working correctly anymore. For example, in this test, the code is selecting a random test module to bind to the BP_GravitySphere actor from the "DynamicBinding" tutorial.

local testModules = {
    "Core.Tests.TestActions.BP_TestAction_01",
    "Core.Tests.TestActions.BP_TestAction_02",
    "Core.Tests.TestActions.BP_TestAction_03",
    "Core.Tests.TestActions.BP_TestAction_04"
}

function M:LeftMouseButton_Pressed()
    local World = self:GetWorld()
    local SpawnClass = self.SpawnClass
    local Transform = self.SpawnPointActor:GetTransform()
    local AlwaysSpawn = UE.ESpawnActorCollisionHandlingMethod.AlwaysSpawn
    -- World:SpawnActor(SpawnClass, Transform, AlwaysSpawn, self, self, "Tutorials.GravitySphereActor")
    local LuaModulePath = testModules[math.random(#testModules)]
    print(LuaModulePath)
    World:SpawnActor(SpawnClass, Transform, AlwaysSpawn, self, self, LuaModulePath):Run()
end

The Test modules are just a simple print:

function BP_TestAction_01:Run()
    local msg = [[
      Successfully Ran Test Action 01
    ]]
    print(msg)
end

The expected behavior is, each time Left Mouse is pressed, a random Module is selected, bound to the spawned actor and function "Run" is done.

This is not the case, in all cases, the Spawn Actor will be bound to the first module that was bound each time the mouse is clicked:

LogUnLua: Core.Tests.TestActions.BP_TestAction_04
LogUnLua:       Successfully Ran Test Action 04

LogUnLua: Core.Tests.TestActions.BP_TestAction_03
LogUnLua:       Successfully Ran Test Action 04

LogUnLua: Core.Tests.TestActions.BP_TestAction_03
LogUnLua:       Successfully Ran Test Action 04

LogUnLua: Core.Tests.TestActions.BP_TestAction_01
LogUnLua:       Successfully Ran Test Action 04

LogUnLua: Core.Tests.TestActions.BP_TestAction_02
LogUnLua:       Successfully Ran Test Action 04

LogUnLua: Core.Tests.TestActions.BP_TestAction_02
LogUnLua:       Successfully Ran Test Action 04

LogUnLua: Core.Tests.TestActions.BP_TestAction_03
LogUnLua:       Successfully Ran Test Action 04

LogUnLua: Core.Tests.TestActions.BP_TestAction_03
LogUnLua:       Successfully Ran Test Action 04

LogUnLua: Core.Tests.TestActions.BP_TestAction_03
LogUnLua:       Successfully Ran Test Action 04

LogUnLua: Core.Tests.TestActions.BP_TestAction_04
LogUnLua:       Successfully Ran Test Action 04

LogUnLua: Core.Tests.TestActions.BP_TestAction_02
LogUnLua:       Successfully Ran Test Action 04

LogUnLua: Core.Tests.TestActions.BP_TestAction_01
LogUnLua:       Successfully Ran Test Action 04

LogUnLua: Core.Tests.TestActions.BP_TestAction_01
LogUnLua:       Successfully Ran Test Action 04
jacobhinds commented 8 months ago

Same issue for 2.2.4, UE 4.27. NewObject() created objects only use the first file dynamically bound to them.

jozhn commented 7 months ago

You cannot bind different lua modules to one UClass, because UFunctions are stored in the UClass. UnLua will only use the first binding, other bindings will be ignored.

If you really need to do this, try generated different classes from the SpawnClass dynamically and then bind different lua modules to them. See my reply in https://github.com/Tencent/UnLua/issues/658