Page 2 of 3
Re: Creating new instances
Posted: Sun Nov 17, 2013 2:33 pm
by Roland_Yonaba
Miken1 wrote:This is amazing! thank you soooo much.
Just one last question, why do you use local functions instead of just normal functions ?
Since Lua functions are
first class citizens, maybe the question can be restated this way:
why do you use local variables instead of just global variables ?
In Lua, variables are
global by default. Each time you create a global variable, it is stored in the global environment table, named _G (in Lua5.1). Accessing this variable later on the code triggers a table lookup.
On the other hand, local variables are stored in registers. Accessing them way more faster than a table lookup. Yet, locals have a limited scope, while globals once declared, can be accessed from everywhere in the code. And this is actually a downside, since when you are messing with multiple globals accross different files, debugging can become painful.
So, a general and common habit of Lua programmers is to use locals everytime it is possible (not just because they are fast, but because they are clean.), and prevent the global environment to be modified.
In that way, Lua's standard library functions (table, string, etc) remain untouched in the global environment.
Here are some complementary reading on the same topic that's worth reading.
Re: Creating new instances
Posted: Sun Nov 17, 2013 6:49 pm
by Miken1
Code: Select all
obj = {}
collectionOfObject = {}
if love.keyboard.isDown("E") then
local function spawnObject(x , y, width, height)
local newObject = {x = x, y = y, width = 50, height = 50}
table.insert(collectionOfObject, newObject)
return newObject
end
end
function DRAW_OBJECT()
for i,v in ipairs(collectionOfObject) do
obj.pic = love.graphics.newImage("box.png")
love.graphics.draw(obj.pic, v.x, v.y)
end
end
function UPDATE_OBJECT(dt)
if love.keyboard.isDown("E") then
spawnObject(love.mouse.getX, love.mouse.getY, 50, 50)
end
end
Okey, so I have tried to implement it but it will not work! I think it's a problem with the for loop, that's my guess anyways.
Sorry for these newbie questions that I am throwing at you people all the time
Re: Creating new instances
Posted: Sun Nov 17, 2013 7:43 pm
by Roland_Yonaba
Just make use of
love callbacks (interesting reading).
Break your code into little subsets. Each subset should go into the right callback.
Code: Select all
local collectionOfOject, spawnObject
local pic
function love.load()
collectionOfObject = {}
pic = love.graphics.newImage("box.png")
function spawnObject(x , y, width, height, pic)
local newObject = {x = x, y = y, width = 50, height = 50, pic = pic}
table.insert(collectionOfObject, newObject)
return newObject
end
end
function love.draw()
for i,v in ipairs(collectionOfObject) do
love.graphics.draw(obj.pic, v.x, v.y)
end
end
function love.keypressed(k, u)
if k == "e" then
spawnObject(love.mouse.getX(), love.mouse.getY(), 50, 50)
end
end
Note1: Do not load an image each time you spawn an object. SInce the image is the same, load it once, and then set it through your factory function.
Note2: I do not recommend to create ne objects when
love.keyboard.isDown (unless you know what you are doing).
Thing is,
love.keypressed is triggerred only once. Even if the key remains down, it will trigger only once, until you release the key and press it again. On the other hand,
love.keyboard.isDown is likely to spawn 10, 20, 30 objects even if you press and release the key very quickly.
Note3:
love.mouse.get(X/Y) are functions, so add parentheses.
That should do it.
Re: Creating new instances
Posted: Mon Nov 18, 2013 7:17 am
by Miken1
Damn you are helpful, thanks for everything!
(Can i return here if I have more questions instead of making a new thread? I have spammed this forumed with enough questions for a while
)
EDIT: have another problem!
I have implemented everything now but it seems like it's overwriting everything else in the code except the background
(Will post my code as soon as i get home from work)
Re: Creating new instances
Posted: Mon Nov 18, 2013 7:42 am
by Roland_Yonaba
Miken1 wrote:Damn you are helpful, thanks for everything!
That's the bliss of Löve. You will feel it at some point, too.
Miken1 wrote:(Can i return here if I have more questions instead of making a new thread? I have spammed this forumed with enough questions for a while
)
Sure.
Miken1 wrote:
EDIT: have another problem!
I have implemented everything now but it seems like it's overwriting everything else in the code except the background
(Will post my code as soon as i get home from work)
That's better. You can also post your *.love.
Re: Creating new instances
Posted: Mon Nov 18, 2013 2:58 pm
by Miken1
Here is my .love file
It is REAAAALLLY messy so beware.
Re: Creating new instances
Posted: Mon Nov 18, 2013 7:05 pm
by Roland_Yonaba
Hi,
Mea culpa. I provided a code in my previous post was full of typos (I guess I shouldn't write code after a certain hour).
Anyway, here are the fixes:
In
love.draw, the correct for loop should be:
Code: Select all
function love.draw()
for i,obj in ipairs(collectionOfObject) do
love.graphics.draw(obj.pic, obj.x, obj.y)
end
end
Then, in
love.keypressed, you should also pass the picture
pic to the function call:
Code: Select all
function love.keypressed(k, u)
if k == "e" then
spawnObject(love.mouse.getX(), love.mouse.getY(), 50, 50, pic)
end
end
Hope this solves the problem.
Re: Creating new instances
Posted: Mon Nov 18, 2013 8:59 pm
by Miken1
My fault,
bad practice to copy code but I am going to rewrite everything when I get this fixed, just wanna see it in action!
To the problem, I am still getting the overwrite error after your fix! and bad parameter when i press E
Re: Creating new instances
Posted: Mon Nov 18, 2013 10:02 pm
by Roland_Yonaba
In the factory function, you are creating a newObject this way:
Code: Select all
local newObject = {x = x, y = y, width = 50, height = 50, pic}
The pic attribute is stored at newObject[1]. You might want to store it at a named key.
Replace it with:
Code: Select all
local newObject = {x = x, y = y, width = 50, height = 50, pic = pic}
Re: Creating new instances
Posted: Mon Nov 18, 2013 11:57 pm
by Miken1
Just getting a gray screen, is it working for you?