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()
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
I implemented the LSDD changepoint detection method decribed in [1] in Julia, to see if I could make it faster than the existing python implementation [2], which is based on a grid search that looks for the optimal parameters.
I obtain the desired results but despite my best efforts, my grid search version of it takes about the same time to compute as the python one, which is still way too long for real applications.
I also tried using the Optimize package which only makes things worse (2 or 3 times slower).
Here is the grid search that I implemented :
using Random
using LinearAlgebra
function squared_distance(X::Array{Float64,1},C::Array{Float64,1})
sqd = zeros(length(X),length(C))
for i in 1:length(X)
for j in 1:length(C)
sqd[i,j] = X[i]^2 + C[j]^2 - 2*X[i]*C[j]
end
end
return sqd
end
function lsdd(x::Array{Float64,1},y::Array{Float64,1}; folds = 5, sigma_list = nothing , lambda_list = nothing)
lx,ly = length(x), length(y)
b = min(lx+ly,300)
C = shuffle(vcat(x,y))[1:b]
CC_dist2 = squared_distance(C,C)
xC_dist2, yC_dist2 = squared_distance(x,C), squared_distance(y,C)
Tx,Ty = length(x) - div(lx,folds), length(y) - div(ly,folds)
#Define the training and testing data sets
cv_split1, cv_split2 = floor.(collect(1:lx)*folds/lx), floor.(collect(1:ly)*folds/ly)
cv_index1, cv_index2 = shuffle(cv_split1), shuffle(cv_split2)
tr_idx1,tr_idx2 = [findall(x->x!=i,cv_index1) for i in 1:folds], [findall(x->x!=i,cv_index2) for i in 1:folds]
te_idx1,te_idx2 = [findall(x->x==i,cv_index1) for i in 1:folds], [findall(x->x==i,cv_index2) for i in 1:folds]
xTr_dist, yTr_dist = [xC_dist2[i,:] for i in tr_idx1], [yC_dist2[i,:] for i in tr_idx2]
xTe_dist, yTe_dist = [xC_dist2[i,:] for i in te_idx1], [yC_dist2[i,:] for i in te_idx2]
if sigma_list == nothing
sigma_list = [0.25, 0.5, 0.75, 1, 1.2, 1.5, 2, 2.5, 2.2, 3, 5]
end
if lambda_list == nothing
lambda_list = [1.00000000e-03, 3.16227766e-03, 1.00000000e-02, 3.16227766e-02,
1.00000000e-01, 3.16227766e-01, 1.00000000e+00, 3.16227766e+00,
1.00000000e+01]
end
#memory prealocation
score_cv = zeros(length(sigma_list),length(lambda_list))
H = zeros(b,b)
hx_tr, hy_tr = [zeros(b,1) for i in 1:folds], [zeros(b,1) for i in 1:folds]
hx_te, hy_te = [zeros(1,b) for i in 1:folds], [zeros(1,b) for i in 1:folds]
#h_tr,h_te = zeros(b,1), zeros(1,b)
theta = zeros(b)
for (sigma_idx,sigma) in enumerate(sigma_list)
#the expression of H is different for higher dimension
#H = sqrt((sigma^2)*pi)*exp.(-CC_dist2/(4*sigma^2))
set_H(H,CC_dist2,sigma,b)
#check if the sum is performed along the right dimension
set_htr(hx_tr,xTr_dist,sigma,Tx), set_htr(hy_tr,yTr_dist,sigma,Ty)
set_hte(hx_te,xTe_dist,sigma,lx-Tx), set_hte(hy_te,yTe_dist,sigma,ly-Ty)
for i in 1:folds
h_tr = hx_tr[i] - hy_tr[i]
h_te = hx_te[i] - hy_te[i]
#set_h(h_tr,hx_tr[i],hy_tr[i],b)
#set_h(h_te,hx_te[i],hy_te[i],b)
for (lambda_idx,lambda) in enumerate(lambda_list)
set_theta(theta,H,lambda,h_tr,b)
score_cv[sigma_idx,lambda_idx] += dot(theta,H*theta) - 2*dot(theta,h_te)
end
end
end
#retrieve the value of the optimal parameters
sigma_chosen = sigma_list[findmin(score_cv)[2][2]]
lambda_chosen = lambda_list[findmin(score_cv)[2][2]]
#calculating the new "optimal" solution
H = sqrt((sigma_chosen^2)*pi)*exp.(-CC_dist2/(4*sigma_chosen^2))
H_lambda = H + lambda_chosen*Matrix{Float64}(I, b, b)
h = (1/lx)*sum(exp.(-xC_dist2/(2*sigma_chosen^2)),dims = 1) - (1/ly)*sum(exp.(-yC_dist2/(2*sigma_chosen^2)),dims = 1)
theta_final = H_lambda\transpose(h)
f = transpose(theta_final).*sum(exp.(-vcat(xC_dist2,yC_dist2)/(2*sigma_chosen^2)),dims = 1)
L2 = 2*dot(theta_final,h) - dot(theta_final,H*theta_final)
return L2
end
function set_H(H::Array{Float64,2},dist::Array{Float64,2},sigma::Float64,b::Int16)
for i in 1:b
for j in 1:b
H[i,j] = sqrt((sigma^2)*pi)*exp(-dist[i,j]/(4*sigma^2))
end
end
end
function set_theta(theta::Array{Float64,1},H::Array{Float64,2},lambda::Float64,h::Array{Float64,2},b::Int64)
Hl = (H + lambda*Matrix{Float64}(I, b, b))
LAPACK.posv!('L', Hl, h)
theta = h
end
function set_htr(h::Array{Float64,1},dists::Array{Float64,2},sigma::Float64,T::Int16)
for (CVidx,dist) in enumerate(dists)
for (idx,value) in enumerate((1/T)*sum(exp.(-dist/(2*sigma^2)),dims = 1))
h[CVidx][idx] = value
end
end
end
function set_hte(h::Array{Float64,1},dists::Array{Float64,2},sigma::Array{Float64,1},T::Int16)
for (CVidx,dist) in enumerate(dists)
for (idx,value) in enumerate((1/T)*sum(exp.(-dist/(2*sigma^2)),dims = 1))
h[CVidx][idx] = value
end
end
end
function set_h(h,h1,h2,b)
for i in 1:b
h[i] = h1[i] - h2[i]
end
end
The set_H, set_h and set_theta functions are there because I read somewhere that modifying prealocated memory in place with a function was faster, but it did not make a great difference.
To test it, I use two random distribution as input data :
x,y = rand(500),1.5*rand(500)
lsdd(x,y) #returns a value around 0.3
Now here is the version of the code where I try to use Optimizer :
function Theta(sigma::Float64,lambda::Float64,x::Array{Float64,1},y::Array{Float64,1},folds::Int8)
lx,ly = length(x), length(y)
b = min(lx+ly,300)
C = shuffle(vcat(x,y))[1:b]
CC_dist2 = squared_distance(C,C)
xC_dist2, yC_dist2 = squared_distance(x,C), squared_distance(y,C)
#the subsets are not be mutually exclusive !
Tx,Ty = length(x) - div(lx,folds), length(y) - div(ly,folds)
shuffled_x, shuffled_y = [shuffle(1:lx) for i in 1:folds], [shuffle(1:ly) for i in 1:folds]
cv_index1, cv_index2 = floor.(collect(1:lx)*folds/lx)[shuffle(1:lx)], floor.(collect(1:ly)*folds/ly)[shuffle(1:ly)]
tr_idx1,tr_idx2 = [i[1:Tx] for i in shuffled_x], [i[1:Ty] for i in shuffled_y]
te_idx1,te_idx2 = [i[Tx:end] for i in shuffled_x], [i[Ty:end] for i in shuffled_y]
xTr_dist, yTr_dist = [xC_dist2[i,:] for i in tr_idx1], [yC_dist2[i,:] for i in tr_idx2]
xTe_dist, yTe_dist = [xC_dist2[i,:] for i in te_idx1], [yC_dist2[i,:] for i in te_idx2]
score_cv = 0
Id = Matrix{Float64}(I, b, b)
H = sqrt((sigma^2)*pi)*exp.(-CC_dist2/(4*sigma^2))
hx_tr, hy_tr = [transpose((1/Tx)*sum(exp.(-dist/(2*sigma^2)),dims = 1)) for dist in xTr_dist], [transpose((1/Ty)*sum(exp.(-dist/(2*sigma^2)),dims = 1)) for dist in yTr_dist]
hx_te, hy_te = [(lx-Tx)*sum(exp.(-dist/(2*sigma^2)),dims = 1) for dist in xTe_dist], [(ly-Ty)*sum(exp.(-dist/(2*sigma^2)),dims = 1) for dist in yTe_dist]
for i in 1:folds
h_tr, h_te = hx_tr[i] - hy_tr[i], hx_te[i] - hy_te[i]
#theta = (H + lambda * Id)\h_tr
theta = copy(h_tr)
Hl = (H + lambda*Matrix{Float64}(I, b, b))
LAPACK.posv!('L', Hl, theta)
score_cv += dot(theta,H*theta) - 2*dot(theta,h_te)
end
return score_cv,(CC_dist2,xC_dist2,yC_dist2)
end
function cost(params::Array{Float64,1},x::Array{Float64,1},y::Array{Float64,1},folds::Int8)
s,l = params[1],params[2]
return Theta(s,l,x,y,folds)[1]
end
"""
Performs the optinization
"""
function lsdd3(x::Array{Float64,1},y::Array{Float64,1}; folds = 4)
start = [1,0.1]
b = min(length(x)+length(y),300)
lx,ly = length(x),length(y)
#result = optimize(params -> cost(params,x,y,folds),fill(0.0,2),fill(50.0,2),start, Fminbox(LBFGS(linesearch=LineSearches.BackTracking())); autodiff = :forward)
result = optimize(params -> cost(params,x,y,folds),start, BFGS(),Optim.Options(f_calls_limit = 5, iterations = 5))
#bboptimize(rosenbrock2d; SearchRange = [(-5.0, 5.0), (-2.0, 2.0)])
#result = optimize(cost,[0,0],[Inf,Inf],start, Fminbox(AcceleratedGradientDescent()))
sigma_chosen,lambda_chosen = Optim.minimizer(result)
CC_dist2, xC_dist2, yC_dist2 = Theta(sigma_chosen,lambda_chosen,x,y,folds)[2]
H = sqrt((sigma_chosen^2)*pi)*exp.(-CC_dist2/(4*sigma_chosen^2))
h = (1/lx)*sum(exp.(-xC_dist2/(2*sigma_chosen^2)),dims = 1) - (1/ly)*sum(exp.(-yC_dist2/(2*sigma_chosen^2)),dims = 1)
theta_final = (H + lambda_chosen*Matrix{Float64}(I, b, b))\transpose(h)
f = transpose(theta_final).*sum(exp.(-vcat(xC_dist2,yC_dist2)/(2*sigma_chosen^2)),dims = 1)
L2 = 2*dot(theta_final,h) - dot(theta_final,H*theta_final)
return L2
end
No matter, which kind of option I use in the optimizer, I always end up with something too slow. Maybe the grid search is the best option, but I don't know how to make it faster... Does anyone have an idea how I could proceed further ?
[1] : http://www.mcduplessis.com/wp-content/uploads/2016/05/Journal-IEICE-2014-CLSDD-1.pdf
[2] : http://www.ms.k.u-tokyo.ac.jp/software.html
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).