External modules

Hey!

Don’t get this!

I have two tests:

main.lua

[lua]local dog = require (“em”)

local dog1 = dog.new ()[/lua]

em.lua

[lua]local dog = {}
local dog_mt = { __index = dog } – metatable

————————————————-
– PUBLIC FUNCTIONS
————————————————-

function dog.new() – constructor

local newDog = {

}

return setmetatable( newDog, dog_mt )
end

return dog[/lua]

and the other one :

main.lua

[lua]local dog = require (“ufo”)

local dog1 = ufo.new ()[/lua]

ufo.lua

[lua]local ufo = {}
local ufo_mt = { __index = ufo }

function ufo.new()

local newUfo = {

}

return setmetatable(newUfo, ufo_mt)

end

return ufo[/lua]

what am i doing wrong the first one works the second complains that :

attempt to index global ‘ufo’ (a nil value)

please if someone can help me i would love it, i want to continue with my game now not struggling with external modules:S

Kind regards Isak [import]uid: 90942 topic_id: 16271 reply_id: 316271[/import]

Looks to be a copy/paste error. Shouldn’t this line:

local dog = require (“ufo”)  

Be this instead:

local ufo = require (“ufo”) [import]uid: 5833 topic_id: 16271 reply_id: 60586[/import]

thanks it did the trick!

Now another question, sprite sheets is it possible to use them in an external module like that? because i use zwoptex and i don’t get that to work if i put it in an external module?

[import]uid: 90942 topic_id: 16271 reply_id: 60596[/import]

@isak.christenson

Yeah it’s possible.

Maybe post up how you are attempting it and we can see what’s wrong :slight_smile: [import]uid: 84637 topic_id: 16271 reply_id: 60706[/import]

@Danny

Sorry of course here are the code for ufo.lua:

[lua]local ufo = {}
local ufo_mt = { __index = ufo }

local zwoptexData = require (“testsheet2”)
local data = zwoptexData.getSpriteSheetData()
local spriteSheet = sprite.newSpriteSheetFromData( “testsheet2.png”, data )
local spriteSet = sprite.newSpriteSet(spriteSheet,1,33)
local spriteInstance = sprite.newSprite(spriteSet)

function ufo.new()

local newUfo = {
sprite.add(spriteSet, “ufo”, 1, 1, 300, 0),
sprite.add(spriteSet, “explosion”, 2, 33, 300, 1),
spriteInstance.timeScale = 1.5,
spriteInstance.x = 200,
spriteInstance.y = 100

}

return setmetatable(newUfo, ufo_mt)
end

return ufo[/lua]

my thought was to have the variables outside the constructor(also tried to have them inside) but i can’t get it to work? is this the right way to work with sprite sheets?

To then prepare and play it i guess i just put up functions but first of all i need to get the creation of the sprite sheet to work=)

Thanks

/Isak [import]uid: 90942 topic_id: 16271 reply_id: 60766[/import]