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

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
Hexanix
Prole
Posts: 2
Joined: Sun Aug 26, 2018 1:13 am

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

Post by Hexanix » Sun Aug 26, 2018 1:33 am

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!

User avatar
zorg
Party member
Posts: 2596
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

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

Post by zorg » Sun Aug 26, 2018 4:22 am

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).
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

randomnovice
Citizen
Posts: 72
Joined: Sat May 09, 2015 9:15 pm

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

Post by randomnovice » Sun Aug 26, 2018 7:02 am

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))

Hexanix
Prole
Posts: 2
Joined: Sun Aug 26, 2018 1:13 am

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

Post by Hexanix » Sun Aug 26, 2018 10:22 am

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.

User avatar
bartbes
Sex machine
Posts: 4944
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

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

Post by bartbes » Sun Aug 26, 2018 10:39 am

That code should work, do you have other code that may affect this? Have you tried running without mobdebug?

Post Reply

Who is online

Users browsing this forum: Exabot [Bot], ReFreezed and 9 guests