Page 2 of 3
Re: Lua Generic for
Posted: Mon Dec 19, 2011 2:09 am
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.
Re: Lua Generic for
Posted: Mon Dec 19, 2011 7:42 am
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.
Re: Lua Generic for
Posted: Mon Dec 19, 2011 8:50 am
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
Re: Lua Generic for
Posted: Mon Dec 19, 2011 10:36 am
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.
Re: Lua Generic for
Posted: Mon Dec 19, 2011 12:30 pm
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
Re: Lua Generic for
Posted: Mon Dec 19, 2011 4:44 pm
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.
Re: Lua Generic for
Posted: Mon Dec 19, 2011 4:55 pm
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.
Re: Lua Generic for
Posted: Mon Dec 19, 2011 5:13 pm
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.
Re: Lua Generic for
Posted: Mon Dec 19, 2011 5:39 pm
by Robin
Jasoco wrote:If you really need to, you can sort the table.
That still doesn't make a difference for
pairs.
Re: Lua Generic for
Posted: Tue Dec 20, 2011 1:38 am
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.