external module problem with variable and function

Hi,

i would like to begin to use external module but that don’t work.

I would like to have a module with the variable and another with the function.

I have this example below, could you tell me where is my error ?

Thanks

---char.lua module(..., package.seeall) local Character={} Character.var=100 return Character --exmodule.lua local Character = require ("char") module(..., package.seeall) function testFunction1() print(Character.var) end --main.lua local exmod= require ("exmodule") local Character = require ("char") exmod.testFunction1() \>\>\>terminal attempt to index global Character a nil value

i found but could you tell me if is it the real good way to perform that ? I imagine that is very complicated if i have a lot of variable because I will express my functions whenever parameters…(exmodule.lua) this will become strong abstract

--exmodule.lua module(..., package.seeall)   function testFunction1(a)   print(a.var) end --char.lua module(..., package.seeall) local Character={}     Character.var=100 return Character --main.lua local exmod= require ("exmodule") local Character = require ("char") exmod.testFunction1(Character)

terminal : 100

Hi @espace3d,

First and foremost, you should stop using the “module(…, package.seeall)” notation. That has been deprecated for quite some time.

A better way to structure external modules with both functions and variables is to set up a table in the module which then “holds” the other functions and variables. Consider this very basic example:

[lua]

– exampleModule.lua

local M = {}

function M.myFunction( param1, param2 )

    print( param1, param2 )

end

M.myVariable1 = “test1”

M.myVariable2 = “test2”

return M

[/lua]

Now, from “main.lua”, you would require() the module and access the function(s) or variable(s) like this:

[lua]

local myModule = require( “exampleModule” )

myModule.myFunction( “testParam1”, “testParam2” )

print( myModule.myVariable1 )

print( myModule.myVariable2 )

[/lua]

Hope this helps,

Brent

Hi Brent,

Thanks it’s help me a lot.

Good day.

i found but could you tell me if is it the real good way to perform that ? I imagine that is very complicated if i have a lot of variable because I will express my functions whenever parameters…(exmodule.lua) this will become strong abstract

--exmodule.lua module(..., package.seeall)   function testFunction1(a)   print(a.var) end --char.lua module(..., package.seeall) local Character={}     Character.var=100 return Character --main.lua local exmod= require ("exmodule") local Character = require ("char") exmod.testFunction1(Character)

terminal : 100

Hi @espace3d,

First and foremost, you should stop using the “module(…, package.seeall)” notation. That has been deprecated for quite some time.

A better way to structure external modules with both functions and variables is to set up a table in the module which then “holds” the other functions and variables. Consider this very basic example:

[lua]

– exampleModule.lua

local M = {}

function M.myFunction( param1, param2 )

    print( param1, param2 )

end

M.myVariable1 = “test1”

M.myVariable2 = “test2”

return M

[/lua]

Now, from “main.lua”, you would require() the module and access the function(s) or variable(s) like this:

[lua]

local myModule = require( “exampleModule” )

myModule.myFunction( “testParam1”, “testParam2” )

print( myModule.myVariable1 )

print( myModule.myVariable2 )

[/lua]

Hope this helps,

Brent

Hi Brent,

Thanks it’s help me a lot.

Good day.