Jump to content

Can I use a function declared in fileA.lua in the fileB.lua?


Recommended Posts

Posted

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.

Posted

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 

  • MTA Team
Posted

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 
  

  • Moderators
Posted

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.

Posted

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.

Posted

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?

Posted

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

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