Exports

What are exports?

Exports are a way to make functions available externally from your resource. Both Lua and C# have different means of using exports, they mostly work the same. Below, there’s details on using them.

Lua exports

Lua exports are generally used to call a function declared in a resource in another separated resource. But, note that they could in theory even used to call Lua functions from C#, even if that is not as popular as the opposite, which we’ll talk about in a bit!

You can register/add Lua exports in the __resource.lua file, by adding either:

export 'FunctionName'

to register a CLIENT export, or:

server_export 'FunctionName'

to register a SERVER export.

note: another way to add exports directly from your code is using the exports function, eg:

exports("exportname",function(arg1,arg2,...)
-- code here
end)

using exports from Lua is pretty simple, just use the snippet below as a reference:

exports['ResourceName']:FunctionName(arg1,arg2...)

detailed explanation:

  • exports is a global table containing all the exports from all the resources
  • ['ResourceName'] is the key to get a reference to the exports of a resource from the global table (or .ResourceName if no weird characters are in it)
  • :FunctionName() is the syntax to use to call a certain function from the exports of the selected resource.

C# exports

C# exports are probably the most common exports use you will find in public resources you can find on the forum. Other than making other scripts able to call functions from your resource, you can also use c# exports as a way to call c# methods from LUA code!(BEWARE! Calling C# Exports from LUA, will take at least a server tick, so you should think about it before using them if your script needs to be really fast in making operations!) As an example, Here, is a resource that uses exports from c# to be able to call some of its methods in LUA.

How to add exports in c#?

again, let’s start by looking at a small snippet, then we’ll analyze it!


        
        public delegate void MyNewExport(string arg1, int arg2, List<dynamic> arg3);


        // constructor
        public YourResourceName()
        {
            MyNewExport mynewexport= new MyNewExport(MYNEWEXPORT);
            Exports.Add("MyNewExport", mynewexport) //add the export!
           
        }
        //method associated with your export
       public void MYNEWEXPORT(string arg1, int arg2, List<dynamic> arg3)
       {
       //do stuff here
       ...
       }

so, all that snippet does is it adds an export called MyNewExport, which can be called by any script at any time. To call an export in LUA it is always the same thing as shown above, but we still haven’t talked about how to call exports in c#. It’s actually pretty much the equivalent as in LUA! here is an example snippet:

Exports["ResourceName"].FunctionName(arg1,arg2...)

CONCLUSIONS!

if anything is incorrect/incomplete, let me know below, so i will be able to fix it. if you have any more questions feel free to ask! if it’s an intelligent question i may add it in this post as well.

note: i’m not a pro at c#, so the example was done using my knowledge, but if there’s a better way to do it, again, let me know down there.

15 Likes

great thanks a lot :smiley:

1 Like

And how to do it in JavaScript?

Is it also the case if I call C# exports from another C# module ?

i am pretty sure it is not, but i’ve never tested it, so if you find out smth please edit the post (it’s a wiki :smiley:)

I have to perform more tests but it seems that the first call made to any export of a module takes a quite large amount of time (resulting in a short game freeze in my case). After that, it’s performed inside a tick without any issues.