tommymaster Posted May 4, 2018 Share Posted May 4, 2018 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. Link to comment
Jayceon Posted May 4, 2018 Share Posted May 4, 2018 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
keymetaphore Posted May 4, 2018 Share Posted May 4, 2018 Why don't you like the exports tho? You can use an event aswell, but things get bit different there. Link to comment
tommymaster Posted May 4, 2018 Author Share Posted May 4, 2018 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
Jayceon Posted May 4, 2018 Share Posted May 4, 2018 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
keymetaphore Posted May 4, 2018 Share Posted May 4, 2018 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
Zorgman Posted May 4, 2018 Share Posted May 4, 2018 Never tried it myself, but won't dofile or loadfile do the job? Link to comment
ShayF2 Posted May 4, 2018 Share Posted May 4, 2018 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
Scripting Moderators thisdp Posted May 4, 2018 Scripting Moderators Share Posted May 4, 2018 You can reenable the "require" by loading module in serverside. Link to comment
tommymaster Posted May 4, 2018 Author Share Posted May 4, 2018 1 hour ago, thisdp said: You can reenable the "require" by loading module in serverside. and how can i do it? Link to comment
Scripting Moderators thisdp Posted May 4, 2018 Scripting Moderators Share Posted May 4, 2018 https://wiki.multitheftauto.com/wiki/Modules Learn from here. If you think it too difficult or too complicated, just ignore me. Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now