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.

Software Engineer & Entrepreneur Running Lustrel and VilarikA • Highly engaged on open source community

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 

My ingame nick is ~HyPeX~

BF3 Gamemode Progress: ~30% - Currently working on AI & MapManager

gKhdyRJ.png

  • 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 
  

DevOps Engineer, Cloud Advocate & Security Engineer(Red Team) | Coffee, Containers & Burp

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

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

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.

F4MZM4.gif

Posted
outputChatBox("Number: " + myFunction()) 

That should be:

outputChatBox ( "Number: ".. myFunction ( ) ) 

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

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?

Software Engineer & Entrepreneur Running Lustrel and VilarikA • Highly engaged on open source community

  • Moderators
Posted

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 

  Tutorials  4x 

 

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

Software Engineer & Entrepreneur Running Lustrel and VilarikA • Highly engaged on open source community

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