Stanley Sathler Posted December 24, 2014 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. Software Engineer & Entrepreneur • Running Lustrel and VilarikA • Highly engaged on open source community
.:HyPeX:. Posted December 24, 2014 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 My ingame nick is ~HyPeX~ BF3 Gamemode Progress: ~30% - Currently working on AI & MapManager
MTA Team 0xCiBeR Posted December 24, 2014 MTA Team 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 DevOps Engineer, Cloud Advocate & Security Engineer(Red Team) | Coffee, Containers & Burp
Moderators IIYAMA Posted December 25, 2014 Moderators 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. Do you want to improve your Lua programming skills and make less mistakes? Start with Lua Language Server! Useful functions 3x Spoiler checkPassiveTimer getScreenStartPositionFromBox getPedGender Tutorials 4x Spoiler Scaling DX Events Attach an addEventHandler on a group of elements Debugging
Tete omar Posted December 25, 2014 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.
Castillo Posted December 25, 2014 Posted December 25, 2014 outputChatBox("Number: " + myFunction()) That should be: outputChatBox ( "Number: ".. myFunction ( ) ) San Andreas Utopia RPG (SAUR) Owner & Developer. Education is the most powerful weapon which you can use to change the world.
Stanley Sathler Posted December 25, 2014 Author 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? Software Engineer & Entrepreneur • Running Lustrel and VilarikA • Highly engaged on open source community
Moderators IIYAMA Posted December 25, 2014 Moderators Posted December 25, 2014 Of course I recommend you it, but not before the event("onResourceStart") has been occurred. Do you want to improve your Lua programming skills and make less mistakes? Start with Lua Language Server! Useful functions 3x Spoiler checkPassiveTimer getScreenStartPositionFromBox getPedGender Tutorials 4x Spoiler Scaling DX Events Attach an addEventHandler on a group of elements Debugging
Stanley Sathler Posted December 25, 2014 Author 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 Software Engineer & Entrepreneur • Running Lustrel and VilarikA • Highly engaged on open source community
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