Jump to content

[LUA] Pass a variable value to an entity which uses SetScript()


tipforeveryone
 Share

Recommended Posts

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 ? smile.png Please show me

Link to comment
Share on other sites

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)

  • Upvote 1
Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...