I just wondered wheter a function defined in Mod X can be accessed from Mod Y...
Here's what I mean:
Mod X
- Code: Select all
function testfunc()
print("Hello World")
end
Mod Y:
- Code: Select all
testfunc()
function testfunc()
print("Hello World")
endtestfunc()
function testfunc()
print("Hello World")
end
ModX.testfunc()
Traxie21 wrote:Yes. It is in this format:
ModX:Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
function testfunc()
print("Hello World")
end
ModY:Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
ModX.testfunc()
Traxie21 wrote:Yes. It is in this format:
ModX:Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
function testfunc()
print("Hello World")
end
ModY:Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
ModX.testfunc()
PilzAdam wrote:Wrong. The version in the first post is correct. All mods run in the same Lua environment, so they share a global namespace. One cant access functions/variables that are local, tough.
webdesigner97 wrote:Uhm, now I'm confused... Which is the right way to access this "external" function?
modA = {}
function modA.myEpicFunction()
print("Works")
endmodA.myEpicFunction()PilzAdam wrote:webdesigner97 wrote:Uhm, now I'm confused... Which is the right way to access this "external" function?
The right way is simply call the function (as in the first post).
But its recommended to give the function a unique name, so that you dont conflict with other mods.
E.g. put all mods into one table:
ModAYour phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
modA = {}
function modA.myEpicFunction()
print("Works")
end
ModB:Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
modA.myEpicFunction()
But again, this is only a recommendation.
ok. But I never worked with namespaces before. how do I declare them?
-- Completely equivalent:
function object:func(a, b, c, ...)
-- do stuff
end
object.func = function(self, a, b, c, ...)
-- do stuff
end
-- Also completely equivalent:
object:func(x, y, z)
object.func(object, x, y, z)
-- Yes, "minetest" is an object. However, since we always use the same object to make these calls,
-- we don't need to pass a reference to it around:
local playerlist = minecraft.get_connected_players()
local status = minecraft.get_server_status()
local inv = minecraft.get_inventory({ type = "player", name = "myname"})
-- However, "inv" here points to some inventory (InvRef) object, and when we call
-- a method on that object the method HAS to know WHICH inventory it is dealing with!
local empty = inv:is_empty()
local my_func = function(a, b, c)
-- do stuff
end
my_func(x, y, z)
Users browsing this forum: No registered users and 6 guests