Page 1 of 1

Lua/Löve2D newbie - need help understanding problem with loading graphics

Posted: Sun Aug 26, 2018 1:33 am
by Hexanix
Lua code link (It is the only file in the project currently, aside from a config): https://pastebin.com/9xrw1CDf

Hello wonderful people of the Löve2D forum,

I decided it's time to sit down and learn LÖVE2D development for good, so I started up a simple, small project, aided by an online tutorial - just to get the gist of it. I'm new to game development as well.

Anyways, a day in and I'm faced with the following issue:

1. I load a sprite of a bullet in love.load()
2. When I press a key, I make a new bullet object and add it to a global bulletTable{}. This is done in love.update()

When I debugged this, however, I noticed that after the first entry all the other bullet copies had a 'nil' img value. To test out further, I loaded the same image to a different variable and then added it to the table twice, before attempting to spawn a bullet with the first image variable twice as well. Both times the first entry had a valid img value, and the second - a 'nil'.

I do not understand why this happens and browsing the Net has yielded no results. If anyone has the time to explain this I would be very thankful. Cheers!

Re: Lua/Löve2D newbie - need help understanding problem with loading graphics

Posted: Sun Aug 26, 2018 4:22 am
by zorg
Hi and welcome to the forums!

To be honest, it does sound like a weird bug... after i checked your code, the only thing i saw that i'd change is that you check if the 0th entry in bulletTable is not nil... but it will be, if you're using table.insert, since lua indexes from 1 (you can "force" a table to have a key that's the number zero, but all inbuilt functions like ipairs and table.insert will not care about that key).

Re: Lua/Löve2D newbie - need help understanding problem with loading graphics

Posted: Sun Aug 26, 2018 7:02 am
by randomnovice
Just a shot in the dark here. Does it matter that you create a local table then insert it? (line 66).

What happens if you do that table insert to your global table as one action?
i.e.

Code: Select all

table.insert(bulletTable, bullet:newBullet(Player.x + Player.img:getWidth()/2, Player.y, Player.shotspeed, globalImages.bulletPlayerImg2, Player.srcTag))

Re: Lua/Löve2D newbie - need help understanding problem with loading graphics

Posted: Sun Aug 26, 2018 10:22 am
by Hexanix
Oh, I see, thanks Zorg! Fixing that actually makes the game run perfectly with the code so far, spawining multiple "bullet" objects with valid sprites. So I guess my debugger was playing tricks on me...?

I hadn't really thought about looking up what index tables start off of in Lua, sincce 0 just seemed logical, hm.

Re: Lua/Löve2D newbie - need help understanding problem with loading graphics

Posted: Sun Aug 26, 2018 10:39 am
by bartbes
That code should work, do you have other code that may affect this? Have you tried running without mobdebug?