Jump to content

Call functions in other directories?


Recommended Posts

You can put <include resource="resname" /> or use fileOpen, fileRead, pcall and loadstring functions.

local fileHandler = fileOpen(":resfolder/otherScript.lua")
if fileHandler then
  local fileContent = fileRead(fileHandler, fileGetSize(fileHandler))
  fileClose(fileHandler)
  pcall(loadstring(fileContent))
end

 

Link to comment
9 minutes ago, Jayceon said:

You can put <include resource="resname" /> or use fileOpen, fileRead, pcall and loadstring functions.


local fileHandler = fileOpen(":resfolder/otherScript.lua")
if fileHandler then
  local fileContent = fileRead(fileHandler, fileGetSize(fileHandler))
  fileClose(fileHandler)
  pcall(loadstring(fileContent))
end

 

--script1.lua
function output()
    print ("ok")
end

--script1 meta.xml
<meta>
    <script src="script1.lua" type="server"/>

    <export function="output"/>
</meta>
  
--script2.lua
output()
  
--script2 meta.xml
<meta>
    <include resource="script1"/>
    <script src="script2.lua" type="server"/>
</meta>

Still does not work.

Link to comment

When you export function and call it in another resource you use those formats:

exports.resname:functionName(arguments)

or

exports["resname"]:functionName(arguments)

Then:

--script1.lua
function output()
    print ("ok")
end

--script1 meta.xml
<meta>
    <script src="script1.lua" type="server"/>

    <export function="output"/>
</meta>
  
--script2.lua
exports.script1:output()
  
--script2 meta.xml
<meta>
    <include resource="script1"/>
    <script src="script2.lua" type="server"/>
</meta>

 

Link to comment
2 minutes ago, Jayceon said:

When you export function and call it in another resource you use those formats:

exports.resname:functionName(arguments)

or

exports["resname"]:functionName(arguments)

Then:


--script1.lua
function output()
    print ("ok")
end

--script1 meta.xml
<meta>
    <script src="script1.lua" type="server"/>

    <export function="output"/>
</meta>
  
--script2.lua
exports.script1:output()
  
--script2 meta.xml
<meta>
    <include resource="script1"/>
    <script src="script2.lua" type="server"/>
</meta>

 

I think he didn't want to use exports for some reason.

Link to comment
3 hours ago, Biistamais said:

I think he didn't want to use exports for some reason.

Script 1

<meta>
    <script src="script1.lua" type="server"/>
    <export function="output"/>
</meta>
function output()
    outputChatBox('Exports Confirmed')
end

Script 2

<meta>
    <include resource="script1"/>
    <script src="script2.lua" type="server"/>
</meta>
local e = exports.script1

e:output()

Note that both have to be on the same side, server or client. Exported functions MUST be exported inside the meta of the same script where the function is located.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...