Class Best Practices
Posted: Sat Jan 26, 2019 12:54 am
Hi Everyone,
In my game I have 1000's of zombies that move around the level. I want to push as many as I can on screen as possible. I realize since I'm new to lua I might not be doing things as efficiently as possible. This has led me to the following clarifying questions that I need help on:
The below is how class objects are setup in my game.
a) Is it a problem that none of the supporting functions are local?
b) Is it bad to always use self.x or self.y etc when referring to this object's variables in the update and render functions?
c) Should I put in the Particle.lua file at the top local references of things like math.floor, or love.graphics.draw?
d) If I do c) will that increase the memory significantly for the 1000's of particle objects I make? Does every object have its own local reference of math.floor for etc, or does it share that one reference will all particle objects? Same question for its related functions. If I make 1000 particle objects is it make 1000's of copies of my update and render code in memory?
e) Currently, in my gameworld update function I loop through all these particle objects and go particle:update(dt) then do the same for rendering them. Is this the best practice or should I be doing things differently?
f) Should I put local floor = math.floor in every lua file just to make it faster? Will this make more problems as every module will have its own local copy of math.floor called floor?
Sorry for all the questions but I want to iron this out before my code gets any bigger than it is. Thanks
In my game I have 1000's of zombies that move around the level. I want to push as many as I can on screen as possible. I realize since I'm new to lua I might not be doing things as efficiently as possible. This has led me to the following clarifying questions that I need help on:
The below is how class objects are setup in my game.
a) Is it a problem that none of the supporting functions are local?
b) Is it bad to always use self.x or self.y etc when referring to this object's variables in the update and render functions?
c) Should I put in the Particle.lua file at the top local references of things like math.floor, or love.graphics.draw?
d) If I do c) will that increase the memory significantly for the 1000's of particle objects I make? Does every object have its own local reference of math.floor for etc, or does it share that one reference will all particle objects? Same question for its related functions. If I make 1000 particle objects is it make 1000's of copies of my update and render code in memory?
e) Currently, in my gameworld update function I loop through all these particle objects and go particle:update(dt) then do the same for rendering them. Is this the best practice or should I be doing things differently?
f) Should I put local floor = math.floor in every lua file just to make it faster? Will this make more problems as every module will have its own local copy of math.floor called floor?
Sorry for all the questions but I want to iron this out before my code gets any bigger than it is. Thanks
Code: Select all
--Particle.lua
Particle = Class{}
function Particle:create(x,y,width,height, lenLife)
local this = {
x = x,
y = y,
width = width,
height = height,
xOffSet = width/2,
yOffSet = height/2,
angle = 0, --in radiants
animation = Animation:init(GAME_OBJECT_DEFS['cloud'].animations['walk left']),
life = lenLife,
timer = 0
}
setmetatable(this, self)
return self
end
function Particle:update(dt)
--whatever logic
self.x = self.x + self.velX*dt
self.x = self.y + self.velY*dt
end
function Particle:render(dt)
--Draw the object to the screen
love.graphics.draw(gTextures[curAnim.texture], gQuadFrames[curAnim.texture][curAnim:getCurrentFrame()],
math.floor((self.x - cameraXPos) +self.xOffSet), math.floor((self.y - cameraYPos)+self.yOffSet),
self.angle, 1, 1, self.xOffSet, self.yOffSet)
end