Only the last element of a table being rendered

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
LiroSphere
Prole
Posts: 2
Joined: Mon Oct 21, 2024 2:35 am

Only the last element of a table being rendered

Post 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
Attachments
CottonWater_LOVE.love
(225.76 KiB) Downloaded 104 times
MrFariator
Party member
Posts: 559
Joined: Wed Oct 05, 2016 11:53 am

Re: Only the last element of a table being rendered

Post 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.
Last edited by MrFariator on Mon Oct 21, 2024 7:38 am, edited 1 time in total.
User avatar
BrotSagtMist
Party member
Posts: 659
Joined: Fri Aug 06, 2021 10:30 pm

Re: Only the last element of a table being rendered

Post 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.
obey
User avatar
dusoft
Party member
Posts: 654
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Only the last element of a table being rendered

Post 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
LiroSphere
Prole
Posts: 2
Joined: Mon Oct 21, 2024 2:35 am

Re: Only the last element of a table being rendered

Post 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.
RNavega
Party member
Posts: 385
Joined: Sun Aug 16, 2020 1:28 pm

Re: Only the last element of a table being rendered

Post 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
User avatar
steVeRoll
Party member
Posts: 140
Joined: Sun Feb 14, 2016 1:13 pm

Re: Only the last element of a table being rendered

Post 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..?
RNavega
Party member
Posts: 385
Joined: Sun Aug 16, 2020 1:28 pm

Re: Only the last element of a table being rendered

Post 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
temp.png (17.03 KiB) Viewed 1756 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests