Jump to content

Call functions in other directories?


Recommended Posts

Posted

Hi! I know about the "call", and "exports" function, but is there an other way, like in C++, that i can include an other script, from an other directory? As i know "require" is not allowed in MTASA. 

Posted

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

 

Posted
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.

Posted

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>

 

Posted
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.

Posted
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.

  • Scripting Moderators
Posted

You can reenable the "require" by loading module in serverside.

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...