Jump to content

[QUESTION/HELP] Scoping with invokeServer/ClientFunction


JayNic

Recommended Posts

Hey all, I'm having a bit of a problem - maybe someone can help me out

I can't seem to figure out how to get the invokeServerFunction/invokeClientFunction to recognize if I am inside a module

 

--[[file: myFile.lua
container module for this and that
]]
local M = {}

function M.requestFromServer()
    invokeServerFunction("M.serverFunction")
end

function M.serverFunction()
    print("Server function!")
end

return M

 

the server function is never called... presumably something with scoping?

any ideas on how to get around it?

 

Maybe there is a better way to build my modules that will work with the calls?

 

thanks

 

EDIT:

I consume the file like so:

--[[file: someinterface.lua
]]

package.path ....
local myUtility = require("myFile.lua")

function initialize()
  myUtility.requestFromServer()
end

 

 

ALSO: Still think it would be great to have a modding help sub-forum...

Link to comment
Share on other sites

The problem is pretty simple and it lays in the dot and the way the functions are selected. They are selected by name - you give function name that should be invoked to invokeServerFunction.

 

You can use wrapper to use it:

-- lib/cmd/foo.lua
local M = {}
function M.request (script)
invokeServerFunction(script)
end

function M.print ()
print("M.print")
end

return M

-- player/cmd/foo.lua
m = require "data.scripts.lib.cmd.foo"
wrapped = m.print

function initialize ()
if onClient() then
	m.request("wrapped")
end
end

 

It should be also possible to unpack module into wrapper functions, or just set global functions in required file (what's the point of module then, though, heh), or some other, similar, solutions.

 

If we could get the function that is called when the invoke request comes, then it would be pretty simple to change its behaviour to properly follow tables. Not suggesting anything, cough.

 

Link to comment
Share on other sites

Thanks for the nice explanation  :)

 

and..

 

ALSO: Still think it would be great to have a modding help sub-forum...

 

+1 for the moddinghelp subforum! would really help to seperate "technical" discussions and mod releases / general stuff...

Link to comment
Share on other sites

Thanks Aki!

 

With your tip, I was able to put together a module that is self contained as follows:

 

--[[file: lib/mylib.lua
]]

local M = {}

--private function
local function doThingOnServer()
   print("I'm on the server")
end

--public function
local function doThing()
   invokeServerFunction("doThingOnServer")
end
M.doThing = doThing

return M

 

consumed:

--[[file: player/myPlayerScript.lua
]]
local MyLib = require("mylib")

function initialize()
  MyLib.doThing()
end

Link to comment
Share on other sites

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
×
×
  • Create New...