Lua OOP multiple instances of class are being ignored, why? - oop

I have a class called "Edit"
function Edit:new(x,y,w,h,text,fnt)
o = {}
setmetatable(o,EditMt)
self.__index = self
self.x = x or 0
self.y = y or 0
self.width = w or 10
self.height = h or 10
self.text = text or ""
self.active = false
self.font = fnt or font
self.yo = -(self.height - self.font:getHeight()) / 2
return o
end
and that class has a function called draw(made with Löve2d)
function Edit:draw( )
if self.active then
love.graphics.setColor(255,255,255,255)
love.graphics.rectangle("fill",self.x,self.y,self.width,self.height)
love.graphics.setColor(0,0,0,255)
love.graphics.printf(self.text,self.font,self.x,self.y,self.width, "center",0,1,1,0,self.yo)
else
love.graphics.setColor(255,255,255,255)
love.graphics.rectangle("line",self.x,self.y,self.width,self.height)
love.graphics.printf(self.text,self.font,self.x,self.y,self.width, "center",0,1,1,0,self.yo)
end
end
and in my main i create 2 of them
edit1 = Edit:new(10,10,60,50,"1")
edit2 = Edit:new(80,10,60,50,"2")
and draw them in the callback
function love.draw( )
edit1:draw()
edit2:draw()
end
but it only draws edit2? if i switch the places in draw it still only draws edit2 but if i switch their places while creating them in the main it now only draws edit1?

This is the most common beginners mistake when they get into OOP in Lua.
You're assigning all those values to self which is Edit. But if you want to change your instance you need to assign them to o.
Otherwise each call to Edit.new will overwrite the values.
The second problem is that your instance o is a global. You need it to be local! Or you will overwrite your instances each time.
function Edit:new (x,y,w,h,text,fnt)
local o = {}
setmetatable(o, self)
self.__index = self
o.x = x or 0
o.y = y or 0
-- and so forth
return o
end
Read this:
https://www.lua.org/pil/16.html
http://lua-users.org/wiki/ObjectOrientedProgramming

You initiates your object not quite correct.
In new function you initiates your object and self in this function is your metatatable.
The other problem that you creates global object o.
So your new function must be:
function Edit:new(x,y,w,h,text,fnt)
local o = {}
setmetatable(o, self)
self.__index = self
o.x = x or 0
o.y = y or 0
o.width = w or 10
o.height = h or 10
o.text = text or ""
o.active = false
o.font = fnt or font
o.yo = -(o.height - o.font:getHeight()) / 2
return o
end

Related

Looping trough a list of objects in lua

I'm writing lua to script inside a videogame called Dual Universe, I'm trying to loop trough an array filled with objects and call their methods but it seems to turn my objects into numbers for some reason.
Here is the error message: attempt to index a number value (local 'projectile')
it's in the for loop of ProjectileGroup:move(), where I call projectile:move().
--Projectile class
Projectile = {x = 0, y = 0, z = 0, six = 0, siy = 0, siz = 0, spx = 0, spy = 0, spz = 0}
function Projectile:new (o,x,y,z,six,siy,siz,spx,spy,spz)
o = o or {}
setmetatable(o, self)
self.__index = self
self.x = x
self.y = y
self.z = z
self.six = six
self.siy = siy
self.siz = siz
self.spx = spx
self.spy = spy
self.spz = spz
return o
end
function Projectile:move ()
self.x = self.x + self.spx
self.y = self.y + self.spy
self.z = self.z + self.spz
end
function Projectile:log ()
system.print('x: '..tostring(self.x)..'|y: '..tostring(self.y)..'|z: '..tostring(self.z)..'|six :'..tostring(self.six)..'|siy :'..tostring(self.siy)..'|siz :'..tostring(self.siz)..'|spx :'..tostring(self.spx)..'|spy :'..tostring(self.spy)..'|spz :'..tostring(self.spz))
end
--ProjectileGroup
ProjectileGroup = {projectiles=0}
function ProjectileGroup:new (o,projectiles)
o = o or {}
setmetatable(o,self)
self.__index = self
self.projectiles = projectiles
return o
end
function ProjectileGroup:add_projectile(projectile)
table.insert(self.projectiles, projectile)
end
function ProjectileGroup:move()
for projectile in pairs(self.projectiles) do
projectile:move()
end
end
function ProjectileGroup:log()
for projectile in pairs(self.projectiles) do
projectile:log()
end
end
projectileA = Projectile:new(nil,0,0,0,10,10,10,0.1,0.3,0.4)
projectileB = Projectile:new(nil,0,0,0,10,10,10,0.4,0.2,-0.4)
projectileC = Projectile:new(nil,0,0,0,10,10,10,0.1,0.05,0.1)
projectileGroup = ProjectileGroup:new(nil,{projectileA,projectileB,projectileC})
projectileGroup:move()

When returning a table get {}

I am trying to create a maze generation script using the module listed below. I'm having this strange problem were the grid variable has a value, but when return grid is called, it returns and empty table? Here is the code to the module script.
local ServerStorage = game:GetService("ServerStorage")
local cellTemplate = ServerStorage:WaitForChild("Cell")
local Maze = {}
local cellObject = {}
local grid = {}
Maze.Size = 25
Maze.CellWidth = 9
Maze.CellLength = 9
Maze.Offset = Vector3.new(0,0,0)
function Maze.new(x,z) -- Create a new cell
local newCell = {}
setmetatable(newCell,{ -- Allows us to create functions for a cell easier
__index = cellObject
})
newCell.X = math.clamp(x,1,Maze.Size)
newCell.Z = math.clamp(z,1,Maze.Size)
newCell.Visited = false
newCell.Model = cellTemplate:Clone()
newCell.Base = newCell.Model:WaitForChild("Base")
newCell.Model.Name = newCell.Model.Name.. "_".. newCell.X.. "-".. newCell.Z
newCell.Walls = {
["Forward"] = newCell.Model:WaitForChild("Forward");
["Backward"] = newCell.Model:WaitForChild("Backward");
["Left"] = newCell.Model:WaitForChild("Left");
["Right"] = newCell.Model:WaitForChild("Right");
}
if not grid[x] then grid[x] = {} end -- We might not have anything on that x axis yet; inserts it into the table
grid[x][z] = newCell
print(grid)
return newCell
end
function Maze.RenderAll() -- Render every cell; exists for more readibility
for _,cellRow in pairs(grid) do -- Loop through every cell row
for _,cell in pairs(cellRow) do -- Loop through every cell in the row
cell:Render() -- Render the cell
end
end
end
function Maze.Grid() -- Allows other scripts to get the grid but not modify it
return grid
end
function Maze.FilterUnvisited(cells) -- Takes in a table and returns one with only the cells in the table that are unvisited
local unvisited = {}
for _,cell in pairs(cells) do -- Loop through every cell in the table passed
if not cell.Visitied then -- The cell hasn't been visited
table.insert(unvisited,cell)
end
end
return unvisited
end
function Maze.GetCell(x,z)
local cell
if grid[x] and grid[x][z] then
cell = grid[x][z]
else
cell = nil
end
return cell
end
function cellObject:Render() -- Render the cell
self.Model:SetPrimaryPartCFrame(CFrame.new(Vector3.new(self.X * Maze.CellLength,0,self.Z * Maze.CellWidth) + Maze.Offset)) -- Move the cell to the correct position
if self.Visited then -- We have gone through the cell
self.Model.PrimaryPart.Color3 = Color3.new(0,1,0) -- Show that the cell has been visited; used for debugging
end
self.Model.Parent = workspace
end
function cellObject:Neighbours() -- Returns the cell's neigbours
local neighbours = {}
-- Order: Right Left Up Down
if grid[self.X + 1] and grid[self.X + 1][self.Z] then -- A cell with +1 X exists
table.insert(neighbours,grid[self.X + 1][self.Z])
end
if grid[self.X - 1] and grid[self.X - 1][self.Z] then -- A cell with -1 X exists
table.insert(neighbours,grid[self.X - 1][self.Z])
end
if grid[self.X][self.Z + 1] then -- A cell with +1 Z exists
table.insert(neighbours,grid[self.X][self.Z + 1])
end
if grid[self.X][self.Z - 1] then -- A cell with -1 Z exists
table.insert(neighbours,grid[self.X][self.Z - 1])
end
return neighbours
end
function cellObject:RandomNeighbour()
local neighbours = self:Neighbours() -- Gets the neigbours of the current cell
if #neighbours > 0 then
return neighbours[math.random(1,#neighbours)] -- Returns a random neigbour
else
return nil
end
end
function cellObject:WallArray() -- Returns an array of the walls instead of a table
local wallArray = {}
wallArray[1] = self.Walls.Forward
wallArray[2] = self.Walls.Right
wallArray[3] = self.Walls.Left
wallArray[4] = self.Walls.Backward
return wallArray
end
function cellObject:Join(cell) -- Joins 2 cells together
local wallPosition = self.Base.Position:Lerp(cell.Base.Position,.5) -- This will return the position of the wall (excluding the Y)
local cell1Array = self:WallArray()
local cell2Array = cell:WallArray()
for wallIndex,wall in pairs(cell1Array) do
if wall.Position.X == wallPosition.X and wall.Position.Z == wallPosition.Z then -- Its the right wall
wall.Transparency = 1
wall.CanCollide = false
cell2Array[4 - (wallIndex - 1)].Transparency = 1
cell2Array[4 - (wallIndex - 1)].CanCollide = false
break -- We don't need to loop anymore, since we've already removed the walls
end
end
end
function cellObject:Unjoin(cell) -- Unjoins 2 cells
local wallPosition = self.Base.Position:Lerp(cell.Base.Position,.5) -- This will return the position of the wall (excluding the Y)
local cell1Array = self:WallArray()
local cell2Array = cell:WallArray()
for wallIndex,wall in pairs(cell1Array) do
if wall.Position.X == wallPosition.X and wall.Position.Z == wallPosition.Z then -- Its the right wall
wall.Transparency = 0
wall.CanCollide = true
cell2Array[4 - (wallIndex - 1)].Transparency = 0
cell2Array[4 - (wallIndex - 1)].CanCollide = true
break -- We don't need to loop anymore, since we've already added the walls
end
end
end
return Maze
Below is the piece of code I'm having diffuculty with.
function Maze.Grid() -- Allows other scripts to get the grid but not modify it
return grid
end
I have tested printing the grid variable and it's not {} but when I call the function in the script below, I always get {}! I have no idea why this is.
Is this a bug in roblox or am I being stupid?
local ServerScriptService = game:GetService("ServerScriptService")
local Maze = require(ServerScriptService:WaitForChild("Maze"))
local latestX = 0
local latestZ = 0
function CreatePath(cell)
local nextCell = cell:RandomNextCell(false)
if nextCell then -- We have a cell next to the current one
print("Joining cells:",cell,nextCell)
cell:Join(nextCell)
cell.Visited = true
cell:Render()
wait(.01)
CreatePath(nextCell)
else -- It must be the end of the maze
print("Path generated!")
print("Maze end: ".. cell.Model.Name)
cell.Base.Color = Color3.new(1,0,0)
end
end
function GenerateMaze()
local cell = Maze.new(latestX + 1,latestZ + 1) -- Create a new cell
cell:Render() -- Render it
print("Created cell")
latestZ += 1
if latestZ > Maze.Size then -- It has exceeded the max size, move on to a new row
latestX += 1
latestZ = 0
end
if #Maze.Grid() < Maze.Size ^ 2 then
wait(.01)
GenerateMaze()
else
print("Grid completed, generating path...")
CreatePath(Maze.GetCell(1,1))
end
end
GenerateMaze()
You are returning a cached value, or well i don't find out other reason.
You may do (and it's not editable):
function Maze.Grid(i, l)
local passed = 1
local response = {}
for index, v in pairs(grid) do
response[index] = i and passed >= i and l and passed <= l and v or not (i or l) and v
-- basically get more than `i` if it, and minor than `l` if it passed
i = i + 1
end
return response
end
It should work, I don't tested it sorry.

Why doesn't this method get called?

I'm talking about MovingGround:update(). It doesn't crash; It just doesn't do anything that's in the method.
I did other things in the method. I set the player pos to 100 100 but that didn't happen so there's (probably) no error in the method itself—at least not that makes it do nothing. The method just doesnt get called!! I think the question is pretty much says the rest! Sooooo.... Here's the code!
-- Grounds
Ground = {}
Ground.__index = Ground
function Ground:new(x,y,width,height)
grd = {}
setmetatable(grd, Ground)
grd.x = x
grd.y = y
grd.w = width
grd.h = height
grd.moving = false
return grd
end
function Ground:draw(r,g,b)
love.graphics.setColor(r,g,b)
love.graphics.rectangle("line",self.x,self.y,self.w,self.h)
end
function Ground:update()
end
MovingGround = {}
MovingGround.__index = MovingGround
function MovingGround:new(x,y,w,h,spdx,spdy,stepsx,stepsy)
grd = {}
setmetatable(grd, Ground)
grd.x = x
grd.y = y
grd.w = w
grd.h = h
grd.moving = true
grd.spdx = spdx
grd.spdy = spdy
grd.stepsxmax = stepsx
grd.stepsymax = stepsy
grd.stepsx = 0
grd.stepsy = 0
grd.xdir = 1
grd.ydir = 1
return grd
end
function MovingGround:draw(r,g,b)
love.graphics.setColor(r,g,b)
love.graphics.rectangle("line",self.x,self.y,self.w,self.h)
love.graphics.rectangle("fill",300,self.y,self.w,self.h)
end
function MovingGround:update()
if self.stepsx > self.stepsxmax or self.stepsx < 0 then self.spdx = -self.spdx self.dirx = -self.dirx end
if self.stepsy > self.stepsymax or self.stepsy < 0 then self.spdy = -self.spdy self.diry = -self.diry end
self.x = self.x + self.spdx
self.y = self.y + self.spdy
self.stepsx = self.stepsx + self.dirx
self.stepsy = self.stepsy + self.diry
end
-- Main
require"functions"
require"player"
require"grounds"
grounds= {}
width = love.graphics.getWidth()
height = love.graphics.getHeight()
function love.load()
test = Player:new(100,100,"w","a","s","d")
grounds[5] = Ground:new(2,2,25,595) --links
grounds[2] = Ground:new(2,2,795,25) --oben
grounds[3] = Ground:new(772,2,25,595) --rechts
grounds[4] = Ground:new(2,572,795,25) --unten
grounds[1] = MovingGround:new(50,400,100,20,0,3,0,15)
end
function love.draw()
test:draw(255,0,255)
love.graphics.print("y : " .. tostring(test.y),10,10)
love.graphics.print("x : " .. tostring(test.x),10,30)
love.graphics.print(test.spdy,10,60)
love.graphics.print(gd,10,90)
love.graphics.print(1000 / gd,10,150)
love.graphics.print(booltoString(test.onGround),10,120)
love.graphics.print(grounds[1].stepsy,10,210)
for i,v in ipairs(grounds) do
grounds[i]:draw(255,255,255)
end
end
function love.update(d)
gd = d
test:update(d)
for i,v in ipairs(grounds) do
grounds[i]:update()
end
end
function love.keypressed(key,code)
if key == "space" then test:jump(-700) end
end
I really recommend using a debugger to determine the cause of this issue. Here's a walkthrough:
Find a place where the update method is being called on a MovingGround object and set a breakpoint there.
Run your program with debugging enabled, until it reaches the breakpoint.
Step into the call to the update method. What method do you end up getting taken to? Is it the method you were expecting?
Now that you're inside of the update method, look at the metatable of self. Is the metatable MovingGround?
The metatable was set inside of MovingGround:new, so set a breakpoint at the beginning of MovingGround:new and restart the program. Run until it reaches the breakpoint.
Step forward (using "step over", not "step into") until you see that the metatable of grd has been set as you saw in step 4.
Look at the line that you just stepped over. What needs to be changed?
Spoiler: in MovingGround:new, the metatable is getting set to Ground instead of MovingGround.
You are supposed to pass in dt into any update function.
In this case, the function should be MovingGround:update(dt) (ground.lua) so that it updates every frame.
Similarly, in main.lua update function, the call should be grounds[i]:update(dt).

Lua OOP not finding variables

I'm trying to do OOP in Lua, but it's not letting me change the vel_y value in the checkInput{} method. Any ideas how i can get this to work? By the way I'm using Love2D for this input stuff.
Player = {x = 100, y = 20, vel_x = 0, vel_y = 0}
function Player:new(o, x, y, vel_x, vel_y)
o = o or {} -- create object if user does not provide one
setmetatable(o, self)
self.__index = self
length = 0
return o
end
function Player:getX()
return self.x
end
function Player:getY()
return self.y
end
function Player:update( dt )
--update velocity
self.x = self.x + self.vel_x
self.y = self.y + self.vel_y
checkInput()
end
function checkInput( dt )
if love.keyboard.isDown("w") and length < 5 then --press the right arrow key to push the ball to the right
length = length + 1
self.vel_y = 5
print("bruhddddddddddddddddddddddd")
elseif love.keyboard.isDown("a") then
elseif love.keyboard.isDown("s") then
elseif love.keyboard.isDown("d") then
end
end
I assume your system call player:update() firs? If so you should pass self and dt to checkInput
function Player:update( dt )
--update velocity
self.x = self.x + self.vel_x
self.y = self.y + self.vel_y
checkInput(self, dt) --<--
end
...
function checkInput( self, dt )
...
if you define checkInput as local (of course before Player:update) this may be similar to private method.
Player = {x = 100, y = 20, vel_x = 0, vel_y = 0} do
Player.__index = self -- we can do this only once
function Player:new(o, x, y, vel_x, vel_y)
o = setmetatable(o or {}, self) -- create object if user does not provide one
-- init o here
return o
end
function Player:getX() end
function Player:getY() end
-- Private method
local function checkInput(self, dt) end
function Player:update( dt )
...
checkInput(self, dt) -- call private method
end
end -- end clsss defenitioin

matlab getter/setter methods. why do they make my program super slow?

Just adding getter/setter methods on my program makes my program super slow. Is there any explanation for this?
function set.x(obj,newx)
obj.x = newx;
end
function x = get.x(obj)
x = obj.x;
end
That's how I define them under my handle class.
Or have I just not implemented them correctly?
Edit
class definition goes...
classdef sensorlocest < handle
properties(GetAcess = 'public', SetAccess = 'private')
sensorId; % sensor id
X; % true x-coordainate
Y; % true y-coordinate
x; % estimate of X
y; % estimate of Y
end
methods
function sesnors = sensorlocest(x,y)
if nargin ~= 0
sesnors(49,1) = sensorlocest;
for k = 1:length(sensors)
sensors(k).sesnorId = k;
sensors(k).X = x.*rand;
sensors(k).Y = y.*rand;
end
end
function init(sensors,x,y)
N = length(sensors);
for i = 1:N
sensors(i).x = x.*rand;
sensors(i).y = y.*rand;
end
end
function set.x(sensors,newx)
sensors.x = newx;
end
function set.y(sensors,newy)
sensors.y = newy;
end
end
end
Nope, you are correct. They indeed have an overhead, check out this thread:
Is MATLAB OOP slow or am I doing something wrong?