Log In  


can someone tell me why my objs.add() function isn't working?

its supposed to let the user pass in a table or object struct and create and store a instance of it.
however it seems to overwrite the instances with the most recent call so they always get created with the last call's paramaters.

--class tab
local objs={}
local obj =
{
	 --team
	 team=0,

		--state
  state=1,
  level=0,
  timer=0,

  --stats
 	shield=100,
		energy=100,
		weapon={},

	 --tile
	 t =0, --tile
	 to=0, --tile offset
	 ts=0, --tile skip
		tw=1, --tile width
	 th=1, --tile height

		--movement
		x=0,
		y=0,
		r=0,
		v=0,
		vx=0,
		vy=0,
		vr=0,
		vv=0,
		fx=1,
		fy=1,
		fr=1,
		fv=1,
		maxvel=3,

	 --shoot
		firetimer=0,
		firedelay=4,
}

--obj manager
function objs.add(o,...)
	o=setmetatable(o or {}, {__index = obj})
	o.base = obj
	if o.init != nil then o:init(...) end
	add(objs, o)
 return o
end

function objs.tick()
	for i=1, #objs do 
	 o=objs[i]
	 o.base:tick()
		if o.tick != nil then o:tick() end
	end
end

function objs.draw()
	for i=1, #objs do
	 o=objs[i] 
	 o.base:draw()
		if o.draw != nil then o:draw() end
	end
end

--base obj
function obj.tick(o)

	--fric
	--o.vx*=min(o.fx,1)
	--o.vy*=min(o.fy,1)
	--o.vr*=min(o.fr,1)
	--o.vv*=min(0,fv,1)

	--vx and vy
 o.vx=cos(o.r/360)*o.v
 o.vy=sin(o.r/360)*o.v
	o.vx=mid(o.vx,-o.maxvel,o.maxvel)
	o.vy=mid(o.vy,-o.maxvel,o.maxvel)

	--vel
	o.x+=o.vx
	o.y+=o.vy
	o.r+=o.vr
	o.v+=o.vv

	--wrap
	o.x=wrap(o.x, 0, 127)
	o.y=wrap(o.y, 0, 127)
end

function obj.draw(o)
 local t=o.t+(o.to*(o.ts+1))
	sprr(t,o.x,o.y,o.tw,o.th,o.r)
	--rint(o.x..", "..o.y,63,63,7)
end


nvm i fixed it by using a deep copy function



[Please log in to post a comment]