## Little help lua table ( one more lol )!

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Neolitik
Citizen
Posts: 55
Joined: Sun Jun 28, 2009 3:13 pm

### Little help lua table ( one more lol )!

hello all ,

i ' don't understand this : function Me.Draw(bla,bla) just it exemple.

they are some way to doing this ? :

Code: Select all

function Me.new(tx,ty)

table.insert(Me,{Img=love.graphics.newImage("love_ball.png");
x=tx;
y=ty})
end

Me:new(200,200)
Me:new(70,500)
Me:new(200,400)
end


thank for all !

Arthurio
Prole
Posts: 8
Joined: Sun Jul 12, 2009 7:12 pm

### Re: Little help lua table ( one more lol )!

Perhaps you could try explaining what you are trying to do?

Code: Select all

--declare the variable before trying to use it
images = {}

--load images only once
loveball = love.graphics.newImage("images/love-ball.png")

end

function newImage(tx,ty)

--use the image reference
table.insert(images, {img=loveball; x=tx; y=ty})
end

newImage(200,200)
newImage(70,500)
newImage(200,400)

--print stuff into log (check the stdout.txt)
for key, value in ipairs(images) do
print(key .. " " .. value.x .. " " .. value.y)
end
end

function draw()

--draw images
for key, value in ipairs(images) do
love.graphics.draw(value.img, value.x, value.y)
end

end


Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

### Re: Little help lua table ( one more lol )!

Neolitik: the table:func() notation uses an implicit self, and is equivalent to table.func(self). However, you are using it inconsistently: in the function declaration, you use . but every function call you use :

In this case, you're not using the self argument, so you can either change it to

Code: Select all

function Me.new(tx,ty)

table.insert(Me,{Img=love.graphics.newImage("love_ball.png");
x=tx;
y=ty})
end

Me.new(200,200)
Me.new(70,500)
Me.new(200,400)
end


or

Code: Select all

function Me:new(tx,ty)

table.insert(self,{Img=love.graphics.newImage("love_ball.png");
x=tx;
y=ty})
end