Lua Generic for

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Lua Generic for

Post by Jasoco »

Nixola wrote:Thank you, you just told me that, if I want to solve an annoying bug from my game (if you shoot for more than 5 seconds on a netbook it will start to get slower), I've to use something that I hate... :(
Since I still don't understand it, I think I'll wait before solving that bug
You're probably not destroying old bullets when they either hit something or go off screen which means every bullet you're shooting is making the table bigger and the for loop needs to run even bigger and bigger and bigger every second.

So what you need to do is set every dead bullet to nil when it "dies". Like if it goes far enough off screen or hits an enemy or wall.
User avatar
Ensayia
Party member
Posts: 399
Joined: Sat Jun 12, 2010 7:57 pm

Re: Lua Generic for

Post by Ensayia »

Just so you know, if you do make a habit of using ipairs instead of numeric for it's not really a bad thing. I myself am a habitual ipairs user. One thing to be aware of is that if you use ipairs and have any gaps in your indexing ipairs will stop when a gap is encountered.

Code: Select all

sometable = {}
sometable[1] = 'hello'
sometable[2] = 'my'
sometable[3] = 'name'
sometable[4] = 'is'
sometable[6] = 'ensayia'

for k, v in ipairs(sometable) do
    print(v..' ')
end
This code will return 'hello my name is' because ipairs stopped at the gap the rest of the table is not iterated through.

Just something to keep in mind.
User avatar
slime
Solid Snayke
Posts: 3170
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Lua Generic for

Post by slime »

Well, if you use a numeric for then you're probably going to be using the # operator on the table, and that will also not like gaps in the array part. Just try to either not have gaps in arrays (use table.remove), or discard the notion of iterating over the whole array sequentially if there are gaps. :p
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: Lua Generic for

Post by coffee »

A bit late I know, but if you imagine LOVE as an optimized game/multimedia extension of Lua is quite easy then understand Lua/LOVE share and when you need to look for Lua or Love side. You will still need read Lua PIL to use Lua core commands to deal with vars, tables but when you need to deal with input (mouse/keyboard) or output (deal with images, fonts, sound) you have the more faster and optimized Love commands (http://love2d.org/wiki/love). I hope that this abstract concept helped you and good coding. Welcome.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Lua Generic for

Post by Nixola »

@kikito: I know you are very helpful, but after opening a topic in Projects and Demos (SCR Move) I want to try to do something on my own, without any help. Think it as a challenge...
@Jasoco: I know, I destroy every "dead" item but only if it is the last one in the table (to prevent gaps in the table), so if I stop shooting for 2/3 seconds the game will return as fast as before. Thank you anyway ;)
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Lua Generic for

Post by Jasoco »

Gaps are not bad. Just use pairs and you don't have to worry about it. What does everyone have against pairs? (Not ipairs) It's the only way to traverse a table that isn't numbered in a solid unbroken line. And that's going to be a LOT when you make games with stuff being destroyed and added all the time.

Like BULLETS.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Lua Generic for

Post by Robin »

Jasoco wrote:What does everyone have against pairs? (Not ipairs) It's the only way to traverse a table that isn't numbered in a solid unbroken line. And that's going to be a LOT when you make games with stuff being destroyed and added all the time.
pairs is amazing, but the order of the elements are undefined with pairs, which is not always what you want.
Help us help you: attach a .love.
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Lua Generic for

Post by Jasoco »

Robin wrote:
Jasoco wrote:What does everyone have against pairs? (Not ipairs) It's the only way to traverse a table that isn't numbered in a solid unbroken line. And that's going to be a LOT when you make games with stuff being destroyed and added all the time.
pairs is amazing, but the order of the elements are undefined with pairs, which is not always what you want.
If you really need to, you can sort the table.

In other cases, it doesn't make a difference. Like bullets. Obviously they're added in order anyway, but it doesn't matter if one bullet is updated before another. You're usually not checking bullets against themselves anyway.

In other words, who cares if a bullet table has gaps. Use pairs. Else you're just making it harder and slower for yourself.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Lua Generic for

Post by Robin »

Jasoco wrote:If you really need to, you can sort the table.
That still doesn't make a difference for pairs.
Help us help you: attach a .love.
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Lua Generic for

Post by Jasoco »

Robin wrote:
Jasoco wrote:If you really need to, you can sort the table.
That still doesn't make a difference for pairs.
It has in my experience of sorting my objects by Y.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 9 guests