You can see that all of the player functions are within an object that is returned upon calling the Player.new() function. In looking at other peoples' code I've noticed that their layouts are quite different, involving the use of functions outside of objects to be created. Some concerns I have include the fact that there will only be one "player" in the game, and thus some of this code is redundant.
Lua doesn't have "objects" per se.
With the closures technique (where you copy the "methods" into each object),
it's more common to return a function that creates a new object.
That avoids the "Player" global and means better encapsulation.
Like pgimeno said, the closures will be GCed so this is good for long-lived objects,
and access should be faster since there are no metatables and passing around of "self" references.
Code: Select all
return function(x, y)
local player = {}
-- init code goes here
player.x = x
player.y = y
function player.destroy()
...
end
function player.draw()
...
end
return player
end
If you are absolutely sure that there can only be one "Player" object then I suggest removing/renaming the "new" function.
And you don't need to copy over the rest of the functions:
Code: Select all
player = {}
function player.init(x, y)
player.x = x
player.y = y
end
function player.draw()
...
end
Note that in both cases you don't need to use "self" and colon ":".
pgimeno wrote: ↑Sun Jun 10, 2018 1:08 am
On an unrelated note, if you want French players to be able to play your game without getting frustrated, use isScancodeDown instead of isDown, because in a French keyboard the keys in these positions are ZQSD, not WASD.
That's good to know, I didn't know that either.