Jump to content

Exporting table functions?


Guest The Mathias

Recommended Posts

SHORT QUESTION

How would I connect Resource A and Resource B, so they can use eachother's lua functions?

LONG QUESTION

Me and a friend just started work on converting most (if not all) of the MTA:SA lua functions, to a more Gmod-like type. Pretty much copying every function.

We just now realized that every resource works in it own lua instance, this is of course VERY bad for our script, since you won't be able to use it outside of the actual script.

We've tried using and all, but it doesn't seem to work. Are we missing something?

Here's a cutout of our meta file.

  
<script src="concommand.lua" type="server"/> 
        <export function="concommand.Add" type="server"/> 
        <export function="concommand.Remove" type="server" /> 
  

And concommand.lua:

  
local concommands = {} 
concommand = {} 
  
function concommand.Add( Command, Unique, Func ) 
    if Hook and Unique and Func then 
        concommands[tostring(Unique)] = { Command = Command, Func = Func } 
        addCommandHandler( Command, getRootElement(), concommand.Call ) 
    end 
end 
  

So, what are we doing wrong?

EDIT:

I've now tried using the command in another resource, trying to get it work, but it still doesn't seem to do anything. Any help?

Link to comment

Hello,

you can call functions from an other object by using the call method.

For example the following calls removeScoreboardColumn in scoreboard resource with one parameter: "kills".

call(getResourceFromName("scoreboard"), "removeScoreboardColumn", "kills") 

I am not sure if that works also with tables, have fun trying ;-).

Have fun coding,

Jan (DracoBlue)

Link to comment

Thanks for replying Draco, I probably should've said that we found an alternative way of doing it. Using the call function would kinda ruin the purpose of the resource, which is to make coding for MTA easier and faster. We decided to put all our stuff in one script, and then just throw it in with the resource you need it for.

We're working on fixing all the bugs and problems right now, aswell as adding more functions to the script. Thanks again :)

Link to comment
Thanks for replying Draco, I probably should've said that we found an alternative way of doing it. Using the call function would kinda ruin the purpose of the resource, which is to make coding for MTA easier and faster. We decided to put all our stuff in one script, and then just throw it in with the resource you need it for.

Basicly thats a good way, since what you are implementing is not really a resource and more a library (do I understand that correct?).

So looking forward to the first release ;-).

Have fun coding,

Jan (DracoBlue)

Link to comment

We figured that too. And yes, it is a library. I just finished the timers, so now a lot of the basic stuff is done, we still need to convert a bunch of the player and vehicle functions though, aswell as add blips, markers and pickups.

Here's how the timers are gonna work:

  
Timer = timer.Create( Name, Delay, Reps, Func, ... ) -- Create our timer. 
Timer:Pause() -- Pause the timer 
Timer:Start() -- Unpause if paused, otherwise do nothing 
Timer:Kill() -- Kill the timer, and keep it from executing. 
Timer:Execute() -- Execute before time, kill if it has reached its last repetition, otherwise subtract 1 from reps, and continue. 
  

That's basically how the whole thing works, using objects as the base all the way through.

Example I'm using in Aweroam, a freeroam gamemode made using only our library:

  
function Awe.Print( pl, txt ) 
    timer.Create( "TextTime_" .. pl:GetName(), 5000, 1, pl.ChatPrint, pl, "Hello there!" ) 
end 
  
concommand.Add( "hello", "Aweroam.Hello", Awe.Print ) 
  

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