Stanley Sathler Posted December 24, 2014 Share Posted December 24, 2014 Yo guys, It's a doubt that I have. I have two files: fileA.lua and fileB.lua, both as server-side. If I declare a functioni in the fileA.lua, can I use it in the fileB.lua? Example: // file: fileA.lua function myFunction() return 10 end // file: fileB.lua function anyFunction() outputChatBox("Number: " + myFunction) end Thanks, Stanley Sathler. Link to comment
.:HyPeX:. Posted December 24, 2014 Share Posted December 24, 2014 If they are both server/client sided (Same), theorically yes, becouse they are being defined globally. Idk if there's any difference by calling the function g_functionName. Thought i usually do that for this purpouse. Test it thought. (Remember one file may load before the other, so add a timer.) Oh and you did a syntax error // file: fileB.lua function anyFunction() outputChatBox("Number: " + myFunction()) end Link to comment
MTA Team 0xCiBeR Posted December 24, 2014 MTA Team Share Posted December 24, 2014 Yes, you can use functions that are the same type (Client-Side with Client-Side // Server-Side with Server-Side). I think it doesn't work if you define the function localy. Example: local my = function() end Link to comment
Moderators IIYAMA Posted December 25, 2014 Moderators Share Posted December 25, 2014 When the "onResourceStart" event gets executed, all global variables are access able in all 'files'. You can start calling functions from the other side after this event has been occurred. It is possible to call them before this event, but you can only call loaded ones. This depends on which the meta.xml loads first. I won't recommend you this. Link to comment
Tete omar Posted December 25, 2014 Share Posted December 25, 2014 Summary: Whatever is globally defined within the same resource and within the same side (server/client) can be accessed by another file. But if you want to access a function from another resource, use: call. Link to comment
Castillo Posted December 25, 2014 Share Posted December 25, 2014 outputChatBox("Number: " + myFunction()) That should be: outputChatBox ( "Number: ".. myFunction ( ) ) Link to comment
Stanley Sathler Posted December 25, 2014 Author Share Posted December 25, 2014 Guys, I'm sorry about syntax errors, since I've been studying JS and the things there are a bit different. Mainly with concatenate. Anyway: so, you don't recommend this? It's better to call only functions that were declared on the same file, instead of all resource? Link to comment
Moderators IIYAMA Posted December 25, 2014 Moderators Share Posted December 25, 2014 Of course I recommend you it, but not before the event("onResourceStart") has been occurred. Link to comment
Stanley Sathler Posted December 25, 2014 Author Share Posted December 25, 2014 IIYAMA, got it! The reasons that I'm asking this is because I want to start apply some SOLID concepts on my code (SRO, properly). So, I want to "divide" my files by "responsabilities". Thank you all, guys. ;D 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