lune-org / lune

A standalone Luau runtime
https://lune-org.github.io/docs
Mozilla Public License 2.0
348 stars 80 forks source link

Creating a new DataModel and setting it as a global called game errors #236

Open kalrnlo opened 1 month ago

kalrnlo commented 1 month ago

the following code errors with

local roblox = require("@lune/roblox")
game = roblox.Instance.new("DataModel")
thread 'main' panicked at crates\lune-roblox\src\instance\registry.rs:44:17:
cannot mutably borrow app data container
kalrnlo commented 1 month ago

I will note I did a quick test of changing the global name to something else, and it didn't error so this weirdly seems to be specific to setting the game global?

AshleyFlow commented 2 weeks ago

the issue that youre having is not exactly from those 2 lines of code.

the issue is that in another script (which is being required) youre either trying to get or set a property of game which causes lune to call this function right here https://github.com/lune-org/lune/blob/main/crates/lune-roblox/src/instance/registry.rs#L42 which needs to borrow the app container if the function is called for the first time

[!NOTE] not all properties will cause lune to call this function, so something as simple as game.Name will not call it

the cause of the issue: when a script is in the process of being required, the app data container is borrowed by that require process, so if you use a library that needs the app container, you will get the error.

workaround

the workaround is to somehow call https://github.com/lune-org/lune/blob/main/crates/lune-roblox/src/instance/registry.rs#L42 in your very first script that isnt in the process of being required

the easiest way to do that, is to get/set a property that doesnt exist at all which causes lune to check all alternatives (one of them being the function that we need to call)

local roblox = require("@lune/roblox")
game = roblox.Instance.new("DataModel")

pcall(function()
    return game.Shutdown
end)

-- now you can continue