Depends on what objects.lua returns. The answer by lost-in-code will work if it returns a table with all the objects.
You have to understand that no matter the class library you're using, they will work the same way with respect to class and self passing:
- Colon syntax is a Lua thing, which is what implicitly passes a `self` parameter to methods. All class libraries will use it.
- Classes themselves are tables, and you need to make these tables available to the program somehow if you want to use them. So you need to pass them like you would any other value.
So, in the case of a library file, if you are using local variables for the classes (highly recommended), you need to return them at the end of the library, so that when you use require() in main, the return value of the require function itself contains them.
So, for example, if you have a library that returns 4, you would do this in the library:
four.lua:
main.lua:
Code: Select all
local four = require('four')
print(four) -- prints 4
It may sound like a stupid example, but the fact is that with tables, and hence with classes, it's the same thing. If you have a library that implements a single class:
player.lua:
Code: Select all
require [[ your class library of choice ]]
local Player = [[ definition of Player using your library of choice ]]
[[ methods for Player ]]
return Player -- return it to the main program
and when you require it from main, you read the returned value:
main.lua:
Code: Select all
local Player = require('player')
local player = Player:new(0, 0)
But in your case you have multiple classes in the same file, so you need to return them all. You would do that like you do with any other values.
You could do this, for example:
objects.lua:
Code: Select all
local Player = [[ definition of the player class ]]
local Enemy = [[ definition of the enemy class ]]
return {Player = Player, Enemy = Enemy}
main.lua:
Code: Select all
local objects = require('objects')
local player = objects.Player:new(0, 0)
...
That's one way to do it, but not the only one. You could also do this:
objects.lua:
Code: Select all
local Player = [[ definition of player ]]
local Enemy = [[ definition of enemy ]]
return {Player, Enemy}
and in main.lua:
Code: Select all
local Player, Enemy = unpack(require('objects'))
Or whatever other means. Of course you could also declare the classes as global variables in objects.lua, and then they will be available throughout your program without you needing to even require the objects from each source file that uses them. I don't recommend this but maybe it's the simplest for you.