Page 1 of 1
Only the last element of a table being rendered
Posted: Mon Oct 21, 2024 2:40 am
by LiroSphere
Im making a doodle jump clone, my method of rendering the platforms are generating all the starting platforms with their positions and insert all of them into a table and rendering them later, but it seems only the last platform of the table is being rendered, this is probably a very newbie question but, how do i resolve this? also any criticism regarding my code is greatly appreciated, i just started using love yesterday :p
Re: Only the last element of a table being rendered
Posted: Mon Oct 21, 2024 7:27 am
by MrFariator
In your main.lua, you have the following code:
Code: Select all
for i = 1, MAX_PLATFORMS, 1 do
local platform = Platform -- note this line in particular
platform.position.x = math.ceil(love.math.random(0, SCREEN_WIDTH - platform.dimension.width))
platform.position.y = (#platformTable + 1) * math.ceil(love.math.random(40, 120))
table.insert(platformTable, platform)
end
In this code, you're not creating new platforms, but rather just taking the same platform over and over again, and randomly giving it a different position. Because all table entries in your platformTable point to the same platform, they share the same position. This is why it might look like you're only drawing a single platform from a list, when in fact you're just drawing the same one many times over.
You could fix this by making a constructor function for creating new platforms, something simple like:
Code: Select all
-- inside entities.lua
Platform = {
sprite = love.graphics.newImage("assets/wood.png"),
}
function Platform.new ( )
local newPlatform = {
dimension = {
width = 160,
height = 25
},
position = {
x = 0,
y = 0
},
sprite = Platform.sprite,
}
return newPlatform
end
And then inside main.lua:
Code: Select all
for i = 1, MAX_PLATFORMS, 1 do
local platform = Platform.new () -- returns a new platform
-- ...the rest
end
Of course, you may also consider using a library that implements object orientation for you.
Awesome-love2d github repo includes some. Classic is a small, simple one, while something like middleclass gives more robust capabilities.
Re: Only the last element of a table being rendered
Posted: Mon Oct 21, 2024 7:37 am
by BrotSagtMist
Using objects is a shortcut to failure.
You where looking to make a metatable prototype, not objects.
Also
Code: Select all
function love.draw()
local background = love.graphics.newImage("assets/bg.png")
You are constantly recreating resources.
Re: Only the last element of a table being rendered
Posted: Mon Oct 21, 2024 8:53 am
by dusoft
Standard issue of people coming from other languages to Lua. In other languages you usually create copies by assigning one variable to another variable. In Lua, you just create a pointer/reference, so the same origin variable is used by all other variables.
I recommend using something like deep table copy mentioned here:
http://lua-users.org/wiki/CopyTable
BTW, this code is my usual go-to to include as a helper:
Code: Select all
function table.copy(orig)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in next, orig, nil do
copy[table.copy(orig_key)] = table.copy(orig_value)
end
setmetatable(copy, table.copy(getmetatable(orig)))
else -- number, string, boolean, etc
copy = orig
end
return copy
end
Re: Only the last element of a table being rendered
Posted: Mon Oct 21, 2024 2:16 pm
by LiroSphere
My goodness, didn't expect to get this many replies, thanks all for the suggestions and just to point out, yeah this is also the first time i touched Lua, it is quite an interesting language, to say the least.
Re: Only the last element of a table being rendered
Posted: Tue Oct 22, 2024 3:05 pm
by RNavega
In this link you can find the binaries for LuaJIT.
Running it will open a console window with an interactive Lua session where you can type code and run it immediately. It's very useful for getting familiar with some Lua things like tables and the API, and debugging simple loops etc.
It's like running Python.exe, it enters the shell mode.
If you're on Windows you want the mingw64 folder and you need to place luajit.exe and lua51.dll in the same folder:
https://github.com/luapower/luajit/tree/master/bin
Re: Only the last element of a table being rendered
Posted: Tue Oct 22, 2024 6:02 pm
by steVeRoll
RNavega wrote: ↑Tue Oct 22, 2024 3:05 pm
In this link you can find the binaries for LuaJIT.
Running it will open a console window with an interactive Lua session where you can type code and run it immediately. It's very useful for getting familiar with some Lua things like tables and the API, and debugging simple loops etc.
It's like running Python.exe, it enters the shell mode.
If you're on Windows you want the mingw64 folder and you need to place luajit.exe and lua51.dll in the same folder:
https://github.com/luapower/luajit/tree/master/bin
Wrong topic..?
Re: Only the last element of a table being rendered
Posted: Tue Oct 22, 2024 6:25 pm
by RNavega
Heh nope, that's what I wanted to say to someone that, from what I understood, isn't much familiar with Lua yet.
LiroSphere wrote: ↑Mon Oct 21, 2024 2:16 pm
this is also the first time i touched Lua, it is quite an interesting language, to say the least.
RNavega wrote: ↑Tue Oct 22, 2024 3:05 pm
Running it will open a console window with an interactive Lua session where you can type code and run it immediately. It's very useful for getting familiar with some Lua things like tables and the API, and debugging simple loops etc.
...I also forgot to say, running your LÖVE programs with
lovec.exe instead of love.exe (and binding each of these to a hotkey in your favorite text editor, like Notepad++), the former creates an additional window for a console for stdout / stderr when running your program, and that lets you print anything you send to Lua's print() function (not to be confused with LÖVE's love.graphics.print()).
- temp.png (17.03 KiB) Viewed 1818 times
In some cases the print() text gets buffered and doesn't show up immediately, you can remedy this by adding a "io.stdout:setvbuf('no')" line at the top of your program to disable stdout buffering so each print() call flushes at once.