Adam's Lair Forum

game development and casual madness
It is currently 2019/11/13, 15:29

All times are UTC + 1 hour [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: 2016/02/28, 16:34 
Forum Adept
Forum Adept
User avatar

Joined: 2015/03/15, 02:49
Posts: 354
Location: Out there
Role: Hobbyist
Code:
    public class Test : Component, ICmpInitializable
    {
        GameObject parent = new GameObject("parent");
        GameObject child = new GameObject("child");

        public void OnInit(InitContext context)
        {
            if (context == InitContext.Activate)
            {
                DualityApp.Keyboard.KeyDown += Keyboard_KeyDown;
            }
        }

        void Keyboard_KeyDown(object sender, Duality.Input.KeyboardKeyEventArgs e)
        {
            if (e.Key == Duality.Input.Key.Space)
            {
                Scene.Current.AddObject(parent);
                AddObjects();
                Log.Game.Write(Scene.Current.AllObjects.Count().ToString());
            }
            if (e.Key == Duality.Input.Key.A)
            {
                Scene.Current.RemoveObject(parent);
                Log.Game.Write(Scene.Current.AllObjects.Count().ToString());
            }
        }

        private void AddObjects()
        {
            Scene.Current.AddObject(this.parent);

            for (int i = 0; i < 5; i++)
            {
                var childClone = child.Clone();
                childClone.Parent = this.parent;
                Scene.Current.AddObject(childClone);
            }
        }

        public void OnShutdown(ShutdownContext context)
        {
            DualityApp.Keyboard.KeyDown -= Keyboard_KeyDown;
        }
    }

Ok, how to reproduce:
Just slap this code on a gameobject in a scene and press Space and then A, repeatedly.
Then, check what the log outputs.

Is it intended behaviour? Also, Scene.Current.ActiveObjects outputs the same count.

_________________
Hello there! Nice to meet you!
I have github. Apologies in advance for bad coding practices.


Top
 Profile  
 
PostPosted: 2016/02/28, 16:59 
Forum Adept
Forum Adept

Joined: 2014/12/13, 00:11
Posts: 398
Location: Brazil
Role: Gamer
The code you provided is not disposing the game object, so I think this is the issue.

_________________
I only know the basics of C#. I have no advanced knowledge.


Top
 Profile  
 
PostPosted: 2016/02/28, 20:49 
Site Admin
Site Admin
User avatar

Joined: 2013/05/11, 22:30
Posts: 2073
Location: Germany
Role: Professional
eyeliner wrote:
Is it intended behaviour? Also, Scene.Current.ActiveObjects outputs the same count.

You're adding the same parent to the Scene and removing it again - each time adding more child objects to the parent object. Removing an object from a Scene doesn't dispose or clear it, so yes, this is intended behavior :)

Try creating a new parent along with the children instead (which involves disposing the old one on removal, as RockyTV mentioned), or only create the children once when creating the parent object, if you just want to remove and add the object to the Scene.

_________________
Blog | GitHub | Twitter (@Adams_Lair)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC + 1 hour [ DST ]


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group