tommymaster

Call functions in other directories?

Recommended Posts

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. 

Share this post


Link to post

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

 

Share this post


Link to post

Why don't you like the exports tho? You can use an event aswell, but things get  bit different there.

Share this post


Link to post
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.

Share this post


Link to post

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>

 

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post

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

Share this post


Link to post
1 hour ago, thisdp said:

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

and how can i do it?

Share this post


Link to post

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.