Jump to content

sharing script parts


Recommended Posts

Hi guys, I'm a bit noob in the field, so could someone help?

If I have a resource with multiple scripts (let's say they are all serverside) than how can those scripts use the same codebase?

So (in practice): should I use "require" to import matrix.lua or should I just copy it into the script or can other scripts use it's functions from the resource?

ps.: I've read something about modules can't be loaded, but this one is published by the team, so I'm sure it will work somehow

thx

Link to comment
You just want to use 1 script into another script? You can try to use the triggerEvent function? n_n
triggerEvent 

1st: thanks for the fast reply :)

2nd: I know about that one, but that requires an event for every damn function. And I'm lazy :D

3rd: Won't that cause lag? I didn't mention, but the functions will possibly be used very often, so I want to make everything as resource efficient as possible. (like every half second but even more on big updates)

ps.: also didn't mention: it's for an inventory script, if anyone can help with that, here will be a link:(edit) viewtopic.php?f=91&t=56867

Edited by Guest
Link to comment
You just want to use 1 script into another script? You can try to use the triggerEvent function? n_n
triggerEvent 

1st: thanks for the fast reply :)

2nd: I know about that one, but that requires an event for every damn function. And I'm lazy :D

3rd: Won't that cause lag? I didn't mention, but the functions will possibly be used very often, so I want to make everything as resource efficient as possible. (like every half second but even more on big updates)

ps.: also didn't mention: it's for an inventory script, if anyone can help with that, here will be a link:

triggerEvent will cause lag if you use it multiply times. You can try to combine those scripts into 1 big script and just use them as a normal function;

functionName(parameterOne, parameterTwo); 

Link to comment

All scripts of the same resource on the same side (server or client) share the same global variables. That means you can call functions or just use variables in the whole resource as long as they are not defined as local.

You can also call the exported functions of the resource from another resource. More information

call 

Link to comment
You can also use exports.

You can only use exports if they are not in a different resource. (correct me if I'm wrong!) and his scripts are in 1 resource. :D

Your thinking of something else, this is how u could use an export:

Resource 1:

-- This is resource_1 (server.lua) 
function killHim(who) 
    killPed(who) 
end 

-- Meta for resource 1

<meta>  
<script src="server.lua" type="server" />  
<export function="killHim" type="server" />  
</meta> 

-- Resource 2

-- THis is resource 2 
addEventHandler("onPlayerJoin",root, 
    function () 
        exports['resource_1']:killHim(source) 
    end 
) 

Link to comment

Each resource has its own space (called environment), thus all code is shared between files, except when it is explicitly declared as local.

Note: 'require' function has been disabled in Multi Theft Auto for security reasons.

Edited by Guest
Link to comment

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