Doing LD 14 and I've hit a problem with lists...

BlitzMax Forums/BlitzMax Programming/Doing LD 14 and I've hit a problem with lists...

Arowx(Posted 2009) [#1]
Hi Gents, is there a know problem with list management?

P-Code
TBUllet
Global mag:TList ' of unfired bullets
Global fired:TList ' of fired bullets
...

Method fire()
mag.Remove(Self)
fired.AddLast(Self)
...
Method Hit()
fired.Remove(Self)
mag.AddLast(Self)



I'm losing bullets and as I'm using this way to add other entities and manage them problems in other areas?

Quick and accurate direction/help, greatly appreciated!


GfK(Posted 2009) [#2]
Add it to the new list *before* removing it from the old one. The way you're doing it, it can potentially go out of scope and get nuked by GC.


Gabriel(Posted 2009) [#3]
The way you're doing it, it can potentially go out of scope and get nuked by GC.

If it's true that an object can go out of scope and be collected within one of it's own methods, the GC has an extremely serious bug that needs to be fixed.


GfK(Posted 2009) [#4]
He did say it was pseudocode (p-code?) so I didn't pay too much attention to how he's structured it. I wouldn't be surprised, though.


Gabriel(Posted 2009) [#5]
How he's structured it isn't relevant to my point though. If he didn't have a reference to it, he couldn't put it back in the list at all. And if he does have a reference to it, the GC should not be collecting it.


Arowx(Posted 2009) [#6]
Hi removed the magazine just creating new bullets, quick fix!

But similar problem with an entities the user creates e.g.

Type Entity
Global list:TList
..
Method create(...)
...
list.AddLast(Self)
..
Method destory()
list.Remove(Self)



The system looks like it has created a new entity but non appear, ok going to try and debug this but it is intermittent!?


sswift(Posted 2009) [#7]
Not that this has anything to do with your problem, but why exactly are you moving bullets from one list to another? Wouldn't it be easier to just have an integer with a count of how many bullets are in the clip, and decrement that as you create and fire each bullet?

I suppose if you're writing some kinda game where you can put multiple types of bullet in the same clip, you might need to do that, but that would be extremely rare, and it would probably be better to just have seperate clips for each type of ammo.

What I'm saying is, you're making your code a lot more complicated than it needs to be. :-)


Arowx(Posted 2009) [#8]
Possibly only I'm trying to save on the cost of creating new bullet objects, the actual magazine is just a metaphore for bullets created but not in action.


Arowx(Posted 2009) [#9]
Are there any issues with polled input under heavy loads e.g. holding down a key constantly?


sswift(Posted 2009) [#10]
There's really no need to save on the cost of creating bullet objects. The days of the 486 are long behind us. :-)

As for polled input, I know of no issues with it. But I suppose that may depend on how you're reading the key input. Are you creating a bullet each frame you detect keydown? Or are you reading keys with getchar or something?


Gabriel(Posted 2009) [#11]
There's really no need to save on the cost of creating bullet objects. The days of the 486 are long behind us. :-)

Actually, I disagree. In any language which uses a garbage collector, it's very useful to get the hang of not dynamically creating and destroying objects more than absolutely necessary. Yes, in this case a handful of bullets is not much, but getting the hang of pooling objects to avoid dynamic allocation and deallocation of resources, particularly when GC controlled, is a good idea.


As for the original point, I'm not really seeing enough of your code to see where the problem might be. Any chance you can post a bigger (possibly even standalone) example?


Arowx(Posted 2009) [#12]
Ok finally figured it out because I was using a scale factor to draw things and an offset to keep the player centred, newly created sprites could be set up without being updated with their offset values before drawing!!!

Ouch!