tipforeveryone Posted August 4, 2016 Share Posted August 4, 2016 As the title, I wanna pass a variable value to an entity right after apply SetScript() to it This is my code in CreateBox.lua box = Model:Box() box:SetScript("myscript.lua") box.script.height = 10 And this is code in myscript.lua function Script:Start() System:Print(self.height) end When I run the game, it shows me that self.height has nil value Then I found a solution in myscript.lua Script.codeActivated = false function Script:UpdateWorld() if self.codeActivated == false then self.codeActivated = true System:Print(self.height) end end This solution can bring me the value passed from createbox.lua but it is not really effective for my project How can I pass variable value in the proper way ? Please show me Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted August 4, 2016 Share Posted August 4, 2016 I would define an Init function in your myscript.lua function Script:Start() self:Init() end function Script:Init() System:Print(self.height) end and when you set a script to entity you can do this: box = Model:Box() box:SetScript("myscript.lua") box.script.height = 10 box.script:Init() Additionally, your Init() function can also have arguments: Since lua doesn't require for those arguments to filled when calling the Init function, you can leave it open at the start: Script.height = 0 function Script:Start() self:Init() end function Script:Init(height) self.height = height System:Print(self.height) end and when you set a script to entity you can do this: box = Model:Box() box:SetScript("myscript.lua") box.script:Init(10) 1 Quote Link to comment Share on other sites More sharing options...
tipforeveryone Posted August 4, 2016 Author Share Posted August 4, 2016 Thank you so much Quote Link to comment Share on other sites More sharing options...
Rick Posted August 4, 2016 Share Posted August 4, 2016 I think you can also tell SetScript to not call the Start function, via a param, and then call it yourself after SetScript and pass values in that way. 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.