From 1a52db619592b382ff5db9367f46c029165491f4 Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Sun, 8 Jul 2018 01:25:52 -0700 Subject: [PATCH 1/2] Make Script schedule itself to run when parented to a DataModel --- lib/instances/Game.lua | 4 +++- lib/instances/LocalScript.lua | 15 +++------------ lib/instances/Script.lua | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/lib/instances/Game.lua b/lib/instances/Game.lua index 3ebc945..122f71a 100644 --- a/lib/instances/Game.lua +++ b/lib/instances/Game.lua @@ -21,7 +21,7 @@ local Workspace = import("./Workspace") local Game = BaseInstance:extend("DataModel") -function Game:init(instance) +function Game:init(instance, habitat) AnalyticsService:new().Parent = instance ContentProvider:new().Parent = instance CoreGui:new().Parent = instance @@ -40,6 +40,8 @@ function Game:init(instance) UserInputService:new().Parent = instance VirtualInputManager:new().Parent = instance Workspace:new().Parent = instance + + getmetatable(instance).instance.habitat = habitat end function Game.prototype:GetService(serviceName) diff --git a/lib/instances/LocalScript.lua b/lib/instances/LocalScript.lua index 3bcd8b6..f446870 100644 --- a/lib/instances/LocalScript.lua +++ b/lib/instances/LocalScript.lua @@ -1,18 +1,9 @@ --[[ - Serves as just a source container right now. + Currently exactly the same as Script. ]] -local BaseInstance = import("./BaseInstance") -local InstanceProperty = import("../InstanceProperty") +local Script = import("./Script") -local LocalScript = BaseInstance:extend("LocalScript", { - creatable = true, -}) - -LocalScript.properties.Source = InstanceProperty.normal({ - getDefault = function() - return "" - end, -}) +local LocalScript = Script:extend("LocalScript") return LocalScript \ No newline at end of file diff --git a/lib/instances/Script.lua b/lib/instances/Script.lua index 23c8054..b40de82 100644 --- a/lib/instances/Script.lua +++ b/lib/instances/Script.lua @@ -9,6 +9,41 @@ local Script = BaseInstance:extend("Script", { creatable = true, }) +function Script:init(instance) + local internal = getmetatable(instance).instance + + internal.hasRun = false + + instance.AncestryChanged:Connect(function() + if internal.hasRun then + return + end + + local game = nil + + local now = instance + while now do + now = now.Parent + + if now.ClassName == "DataModel" then + game = now + break + end + end + + if game == nil then + return + end + + internal.hasRun = true + + -- TODO: Correctly handle errors + + local co = coroutine.create(loadstring(instance.Source)) + getmetatable(game).instance.habitat.taskScheduler:schedule(0, co) + end) +end + Script.properties.Source = InstanceProperty.normal({ getDefault = function() return "" From 80a5725d78b4bc2f4d6b1e35c808b330f778dc2b Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Wed, 11 Jul 2018 12:10:56 -0700 Subject: [PATCH 2/2] Switch to FindFirstAncestorOfClass --- lib/instances/Script.lua | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/lib/instances/Script.lua b/lib/instances/Script.lua index b40de82..97828c1 100644 --- a/lib/instances/Script.lua +++ b/lib/instances/Script.lua @@ -19,17 +19,7 @@ function Script:init(instance) return end - local game = nil - - local now = instance - while now do - now = now.Parent - - if now.ClassName == "DataModel" then - game = now - break - end - end + local game = instance:FindFirstAncestorOfClass("DataModel") if game == nil then return