Author Topic: Removing a nested object form a FlxGroup.  (Read 3870 times)

test84

  • Key Contributor
  • *****
  • Posts: 1328
  • Karma: +0/-0
  • ت
    • View Profile
    • My personal site.
Removing a nested object form a FlxGroup.
« on: Mon, Aug 20, 2012 »
Hi,

I have a FlxGroup for all my enemy bullets and each enemy adds it's weapon's bullet's group to it:

Registry.EnemyBulletsGroup.add(Pistol.group);

So the FlxGroup will have other FlxGroups inside it, not plain members (which are bullets).

But at some point for some fancy thing I need to remove a bullet from that group and add it to another one.

Problem is that Flixel's own FlxGroup.remove() doesn't look inside nexted FlxGroups and just checks with indexOf() that if that member (which is a bullet) is in the group or not, which returns false as it's inside a group that has the bullet.

And I can't put just bullets inside FlxGroup, like this:

Registry.EnemyBulletsGroup.add(Pistol.group.members);

because "members" is not FlxBasic and is Flash's own array type.

So what's the best approach here from options that I can see:

1) I somehow find a way to add Bullets into the FlxGroup so I can use the existing functionality of FlxGroup.Remove().

2) I add a method to FlxGroup (or edit it) so it can recursively look inside FlxGroup's inside it as well.

3) Some other option(s) that I'm not aware of.

Thanks.
blog, twitter, Check out my award winning game, Rot Gut:

test84

  • Key Contributor
  • *****
  • Posts: 1328
  • Karma: +0/-0
  • ت
    • View Profile
    • My personal site.
Re: Removing a nested object form a FlxGroup.
« Reply #1 on: Tue, Aug 21, 2012 »
*self bump*
blog, twitter, Check out my award winning game, Rot Gut:

Denis

  • Member
  • **
  • Posts: 16
  • Karma: +0/-0
    • View Profile
Re: Removing a nested object form a FlxGroup.
« Reply #2 on: Fri, Aug 24, 2012 »
Hello,

I guess a third option could be:
make N groups, one for each kind of weapon you have, and add bullets to them instead of using the big EnemyBullets group.
Then overlap-check each group with the character.
Probably not the best approach in terms of scaling - but still an option :)

I don't know the architecture of your game and so I am not sure if you're experiencing an architecture problem but it looks like you might do...

Let me ask you something.
Why do you make specific-weapon groups then and not put all the bullets in the EnemyBullets group directly? Why do you need separate groups at all and since now you're experiencing problems accessing them?

And by the way - Rot Gut is great! Rocks! :)

test84

  • Key Contributor
  • *****
  • Posts: 1328
  • Karma: +0/-0
  • ت
    • View Profile
    • My personal site.
Re: Removing a nested object form a FlxGroup.
« Reply #3 on: Tue, Aug 28, 2012 »
Thanks Denis for the reply,
Hello,

I guess a third option could be:
make N groups, one for each kind of weapon you have, and add bullets to them instead of using the big EnemyBullets group.
Then overlap-check each group with the character.
Probably not the best approach in terms of scaling - but still an option :)

I don't know the architecture of your game and so I am not sure if you're experiencing an architecture problem but it looks like you might do...

About the architecture you suggested, let me first tell you about what I currently do. I have two main groups, one for player attacks and one for enemy attacks that in each corresponding group, I store relevant bullets/attacks. So PlayerAttacks holds groups of Pistol, Shotgun and TommyGun bullets (remember, groups of them, not single bullets) and same thing for enemies.

And to re-state the problem, FlxGroup's Remove() method does not work nested so I can't remove a bullet from PlayerAttacks because a bullet is in another group and FlxGroup's Remove() does not work recursively.

Let me ask you something.
Why do you make specific-weapon groups then and not put all the bullets in the EnemyBullets group directly? Why do you need separate groups at all and since now you're experiencing problems accessing them?

The way FlxWeapon works is that it generates bullets for it's usage and I add that group of bullets to my PlayerAttack or EnemyAttacks. That's why I end up with groups of bullets. I tried to add bullets to a single big group but that's not been successful because  "members" of a FlxGroup are type of Array and you can't add a type of "Array" to a FlxGroup and it should be type of FlxBasic.

And by the way - Rot Gut is great! Rocks! :)
And I'm happy that you liked Rot Gut, it will be released on Friday ;)
blog, twitter, Check out my award winning game, Rot Gut:

Denis

  • Member
  • **
  • Posts: 16
  • Karma: +0/-0
    • View Profile
Re: Removing a nested object form a FlxGroup.
« Reply #4 on: Thu, Aug 30, 2012 »
Well, since you tried to make it with a big group of bullets and you use the different FlxWeapon groups out of convenience of using the weapon plugin,
I thought about suggesting you make your own FlxGroup class that will extend it, then add a new method like "addAll(group:FlxGroup)" which will cycle trough the members of the group and add them one by one as a single member but not the whole group as one. Then you could do bulletsGroup.addAll(weapon.group);
But then I realized FlxWeapon needs a group to operate, for example to spawn a new bullets and add them to it. So if you do addAll, it won't work in the cases when it needs to increase the group members. But if you make a certain amount of bullets when you init the weapon like weapon.makePixelBullet(100, ...) and you never change the amounts I guess this should work.

A safer way is extending the FlxWeapon, with an option to provide it with a group for it's bullets, so it would not make a new one but just use your group.

If you wish to keep the groups as they are the option with the extended recursive Remove() remains :)
But I wonder if you already solved this one?

Of course I liked Rot Gut - you did an amazing job there! Triple A flash game! :D Can't wait for the link of the released version!

test84

  • Key Contributor
  • *****
  • Posts: 1328
  • Karma: +0/-0
  • ت
    • View Profile
    • My personal site.
Re: Removing a nested object form a FlxGroup.
« Reply #5 on: Sat, Sep 1, 2012 »
Thanks Denis for both your reply and kind words towards Rot Gut, we are wishing for a Greenlight release!

And no, I'm yet to solve this one. I don't want to hack away and move on as I know it's an important thing and a bug here can be very hard to debug, as I have similar problems with QuadTree's.

I'm OK with extending FlxWeapon, in fact, I did for all my weapons, even have a new base for some of them.

And BTW, I didn't mean to recursively call Remove when I said "recursive", as it would probably yield some strange problems with how Flash works. I was about to check the bullet group and see if there are groups there, go inside them as I believe there should be no more than one level of nested FlxGroups there.

So what do you think?
blog, twitter, Check out my award winning game, Rot Gut:

Denis

  • Member
  • **
  • Posts: 16
  • Karma: +0/-0
    • View Profile
Re: Removing a nested object form a FlxGroup.
« Reply #6 on: Sun, Sep 2, 2012 »
You're welcome - good luck with the Greenlight release!

Yes I got you, by "recursive" you meant just to go deep enough within the nested groups to search for the bullet object. That's fine. And since it won't be more than one level deep it should be fast. Of course you can try that.

Though I still believe that extending FlxWeapon to work with a given bullets-group rather than making it's own group would be faster in terms of performance. Because you won't cycle the arrays while searching, you won't have any "remove" actions which again mess with the array and cost CPU...
And maybe faster in terms of developing time too. Maybe the only "catch" here would be that you will have to take care about the destroy method of the FlxWeapon, not to destroy the group if its a "given" and not "own" one...

Anyway, I'll be glad to hear your final solution when you resolve the problem.

test84

  • Key Contributor
  • *****
  • Posts: 1328
  • Karma: +0/-0
  • ت
    • View Profile
    • My personal site.
Re: Removing a nested object form a FlxGroup.
« Reply #7 on: Mon, Sep 3, 2012 »
Thanks Denis,

Thing is I don't think it's possible to use a shared bullets group with Flxweapon because it recycles a bullet from it's stash and puts it on the screen and if it uses a shared one, it can complicate stuff, then you have to recycle in a loop and check if that bullet is type of that current weapon and I think it can be messy.

I'm so stuck at this! :/

It's strange, I did the most hard parts of this feature and this thing that I thought is rather simple is getting me stuck.
blog, twitter, Check out my award winning game, Rot Gut:

test84

  • Key Contributor
  • *****
  • Posts: 1328
  • Karma: +0/-0
  • ت
    • View Profile
    • My personal site.
Re: Removing a nested object form a FlxGroup.
« Reply #8 on: Fri, Sep 7, 2012 »
* bump *
blog, twitter, Check out my award winning game, Rot Gut:

test84

  • Key Contributor
  • *****
  • Posts: 1328
  • Karma: +0/-0
  • ت
    • View Profile
    • My personal site.
Re: Removing a nested object form a FlxGroup.
« Reply #9 on: Mon, Sep 17, 2012 »
*bump*
blog, twitter, Check out my award winning game, Rot Gut:

photonstorm

  • Administrator
  • Key Contributor
  • *****
  • Posts: 1502
  • Karma: +1/-0
    • View Profile
    • Photon Storm
Re: Removing a nested object form a FlxGroup.
« Reply #10 on: Mon, Sep 17, 2012 »
My first question would be "why do you need to remove the bullet from one group and add it to another?" - changing a bullet pool size on the fly isn't that wise, it's best to keep it the same size constantly. Could you simply hide/kill the bullet you want to 'remove' and then spawn a new one matching its properties in this new group of yours?
http://www.photonstorm.com



"Tell me and I will forget, show me and I might remember, involve me and I will understand" - Confucius

test84

  • Key Contributor
  • *****
  • Posts: 1328
  • Karma: +0/-0
  • ت
    • View Profile
    • My personal site.
Re: Removing a nested object form a FlxGroup.
« Reply #11 on: Wed, Sep 19, 2012 »
Thanks man for stopping by.

To reply your question I have to say that: because at some point I want to turn an enemy bullet against themselves. So I have two main groups for player bullets and enemy bullets that I check collision with (enemy bullets against the player and vice versa.

About your suggestion, I have multiple weapons for enemies, I can find the weapon that shot this exact bullet but then I have to make several pools to have bullets for each enemy weapon, is that what you meant? (then I have to be conscious that each time I make a new enemy weapon I have to remember to add a new pool for the player)
blog, twitter, Check out my award winning game, Rot Gut:

photonstorm

  • Administrator
  • Key Contributor
  • *****
  • Posts: 1502
  • Karma: +1/-0
    • View Profile
    • Photon Storm
Re: Removing a nested object form a FlxGroup.
« Reply #12 on: Wed, Sep 19, 2012 »
Could you explain the set-up a little for me? i.e. current structure. I'm still thinking that just having multiple pools will work - it's the quantity of live objects in a pool that slows flixel down, not the quantity of pools.
http://www.photonstorm.com



"Tell me and I will forget, show me and I might remember, involve me and I will understand" - Confucius

test84

  • Key Contributor
  • *****
  • Posts: 1328
  • Karma: +0/-0
  • ت
    • View Profile
    • My personal site.
Re: Removing a nested object form a FlxGroup.
« Reply #13 on: Thu, Sep 20, 2012 »
Did you read first post of this topic?
blog, twitter, Check out my award winning game, Rot Gut:

photonstorm

  • Administrator
  • Key Contributor
  • *****
  • Posts: 1502
  • Karma: +1/-0
    • View Profile
    • Photon Storm
Re: Removing a nested object form a FlxGroup.
« Reply #14 on: Mon, Oct 1, 2012 »
Yes, but I'm still not 100% clear why the need for it to all be split up into different pools. In fact you could probably get away with having just 1 bullet pool (with lots of different types of bullets flying around within it). Anyway just thinking out loud, extending FlxGroup to scan nested groups should be pretty quick to do and might solve this (as long as it doesn't get stuck in a loop down there :)
http://www.photonstorm.com



"Tell me and I will forget, show me and I might remember, involve me and I will understand" - Confucius

LaughingLeader

  • Member
  • **
  • Posts: 64
  • Karma: +0/-0
    • View Profile
    • LaughingLeader Blog
Re: Removing a nested object form a FlxGroup.
« Reply #15 on: Tue, Oct 2, 2012 »
I would probably either create your own remove code, or create some sort of addGroupMembers function for an extended FlxGroup class you make.

Something like this:

Code: [Select]
public function addGroupMembers(Group:FlxGroup):void
{
   for(var i:int=0;i<Group.length;i++)
     {
          if(Group.members[i] != null)
            {
                  add(Group.members[i]);     
            }
     }
}

Or for your own remove code:

Code: [Select]
public function removeMember(Member:FlxObject, Group:FlxGroup=null):void
{
    if(Group != null)
      {
          for(var i:int=0;i<Group.length;i++)
                {
                    if(Group.members[i] != null)
                       {
                           if(Group.members[i] == Member)
                             {
                                  Group.remove(Member);
                             {
                       }
                }
      }
      else
         {
            if(Member != null) remove(Member);
         }

}

And I can't put just bullets inside FlxGroup, like this:

Registry.EnemyBulletsGroup.add(Pistol.group.members);

because "members" is not FlxBasic and is Flash's own array type.
Remember that a FlxGroup holds an array that contains all your objects, called members (which you're aware of). So if you wanted to add members from another group to a different group's members, you would have to add it to the array(members) of the group using push, not add.
So another for loop would work:
Code: [Select]
for(var i:int=0;i<Pistol.group.length;i++)
{
   if(Pistol.group.members[i] != null)
    {
       Registry.EnemyBulletsGroup.members.push(Pistol.group.members[i]); 
    }
}
But really you could just replace that push statement with an add statement (like the addGroupMembers function I put up above).
Code: [Select]
Registry.EnemyBulletsGroup.add(Pistol.group.members[i]); 

Once it sinks in that FlxGroup just hold arrays of all the objects you add to it, it can be pretty simple to manipulate them like you want by manipulating the array.
« Last Edit: Tue, Oct 2, 2012 by LaughingLeader »