Only the last element of a table being rendered
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 2
- Joined: Mon Oct 21, 2024 2:35 am
Only the last element of a table being rendered
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
- Attachments
-
- CottonWater_LOVE.love
- (225.76 KiB) Downloaded 104 times
-
- Party member
- Posts: 559
- Joined: Wed Oct 05, 2016 11:53 am
Re: Only the last element of a table being rendered
In your main.lua, you have the following code:
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:
And then inside main.lua:
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.
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
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
Code: Select all
for i = 1, MAX_PLATFORMS, 1 do
local platform = Platform.new () -- returns a new platform
-- ...the rest
end
Last edited by MrFariator on Mon Oct 21, 2024 7:38 am, edited 1 time in total.
- BrotSagtMist
- Party member
- Posts: 659
- Joined: Fri Aug 06, 2021 10:30 pm
Re: Only the last element of a table being rendered
Using objects is a shortcut to failure.
You where looking to make a metatable prototype, not objects.
Also
You are constantly recreating resources.
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")
obey
Re: Only the last element of a table being rendered
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:
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
My boat driving game demo: https://dusoft.itch.io/captain-bradley- ... itius-demo
-
- Prole
- Posts: 2
- Joined: Mon Oct 21, 2024 2:35 am
Re: Only the last element of a table being rendered
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
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
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
Wrong topic..?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
Re: Only the last element of a table being rendered
Heh nope, that's what I wanted to say to someone that, from what I understood, isn't much familiar with Lua yet.
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.
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.
...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()).
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.
Who is online
Users browsing this forum: No registered users and 12 guests