Adam's Lair Forum

game development and casual madness
It is currently 2018/07/23, 02:06

All times are UTC + 1 hour [ DST ]




Post new topic Reply to topic  [ 16 posts ]  Go to page Previous  1, 2
Author Message
PostPosted: 2018/07/12, 09:51 
Newbie
Newbie

Joined: 2018/06/17, 19:09
Posts: 8
Role: Hobbyist
Thanks! But am I correct that there is no way to attach a component to a gameobject without activating the component?


Top
 Profile  
 
PostPosted: 2018/07/12, 12:08 
Forum Addict
Forum Addict
User avatar

Joined: 2013/09/19, 14:31
Posts: 869
Location: Italy
Role: Hobbyist
Simply set to inactive before adding it

Code:
MyComponent cmp = new MyComponent();
cmp.Active = false;
myGameObject.AddComponent(cmp);

_________________
Come on Duality's Discord channel. We have cookies! :mrgreen:


Top
 Profile  
 
PostPosted: 2018/07/21, 19:35 
Newbie
Newbie

Joined: 2018/06/17, 19:09
Posts: 8
Role: Hobbyist
Why doesn't this work? It works for the first time when called but it seems that the MapManager stays active and subsequent calls won't do anything. Also, I have this small map and still CPU usage is around 30% and GPU over 90%. Why is this?
Code:
namespace TileMap_Test3
{
    public class MapController : Component, ICmpUpdatable
    {
        void ICmpUpdatable.OnUpdate()
        {
            if (DualityApp.Keyboard[Key.I])
            {
                AddMapManager(10);
            }
            if (DualityApp.Keyboard[Key.K])
            {
                AddMapManager(20);
            }
            if (DualityApp.Keyboard[Key.L])
            {
                AddMapManager(30);
            }
        }


        public void AddMapManager(int mapSize)
        {
            MapManager add = this.GameObj.GetComponent<MapManager>();
            if (add == null)
            {
                add = new MapManager(mapSize);
                this.GameObj.AddComponent<MapManager>(add);
            }
            else
            {
                add.MapSize = mapSize;
                add.Active = true;
            }
        }
    }
}


Code:
namespace TileMap_Test3
{
    [RequiredComponent(typeof(Tilemap))]
    public class MapManager : Component, ICmpInitializable
   {
        private int mapSize = 0;


        public MapManager(int size)
        {
            this.MapSize = size;
        }


        public int MapSize { get => mapSize; set => mapSize = value; }


        public void OnInit(InitContext context)
        {
            if (context == InitContext.Activate)
            {
                this.GameObj.GetComponent<Tilemap>().Resize(MapSize, MapSize);
                this.Active = false;
            }     
        }

        public void OnShutdown(ShutdownContext context)
        {
        }


Top
 Profile  
 
PostPosted: 2018/07/21, 20:10 
Forum Addict
Forum Addict
User avatar

Joined: 2013/09/19, 14:31
Posts: 869
Location: Italy
Role: Hobbyist
That's a bit of a weird code you got there..

First of all, you can't just call
Code:
this.GameObj.AddComponent<MapManager>(add);


from your class and have everything set up properly, the RequiredComponent attribute is there for editor's use only; when you build Components or GameObjects directly by code, you have to provide all the necessary steps
i.e. in this case, you should add a Tilemap first, since it's required by your MapManager component.

Second, although it *should* work on paper, I think that changing your component's Active status inside an Active-change-response event might not work as expected.

Why not just change your AddMapManager call to something like
Code:
public void AddMapManager(int mapSize)
{
   MapManager mm = this.GameObj.GetComponent<MapManager>();
   if (mm == null)
   {
      this.GameObj.AddComponent<Tilemap>();
      mm = this.GameObj.AddComponent<MapManager>();
   }

   mm.SetSize(mapSize);
}

_________________
Come on Duality's Discord channel. We have cookies! :mrgreen:


Top
 Profile  
 
PostPosted: 2018/07/22, 12:35 
Newbie
Newbie

Joined: 2018/06/17, 19:09
Posts: 8
Role: Hobbyist
The TileMap is already there so I do not think it needs to be added? Also, as the MapManager component is going to activate when it is added to the gameobject (unless I first make it deactivated), it makes sense to make a constructor for the MapManager, so that I can use the mapSize as a parameter or doesn't it?

Well, I managed to make it work the way I wanted by moving the Active = false to the end of the AddMapManager call, out of the OnInit of MapManager.

Seems that the high CPU and GPU usage only happens when Debugging, but is that normal?


Top
 Profile  
 
PostPosted: 2018/07/22, 14:56 
Forum Addict
Forum Addict
User avatar

Joined: 2013/09/19, 14:31
Posts: 869
Location: Italy
Role: Hobbyist
StrigoiTyrannus wrote:
The TileMap is already there so I do not think it needs to be added? Also, as the MapManager component is going to activate when it is added to the gameobject (unless I first make it deactivated), it makes sense to make a constructor for the MapManager, so that I can use the mapSize as a parameter or doesn't it?

Ah, if you always have your Tilemap Component ready in your scene there is no need to explicitly add it. I thought you wanted to generate everything needed at runtime; and of course, if you feel better having a parametrized constructor feel free to do so :D mine was just an idea, not a to-do.

StrigoiTyrannus wrote:
Seems that the high CPU and GPU usage only happens when Debugging, but is that normal?

It's entirely possible, since while in debug (inside the editor) there is no cap to the FPS so your game runs as fast as possible.

_________________
Come on Duality's Discord channel. We have cookies! :mrgreen:


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 16 posts ]  Go to page Previous  1, 2

All times are UTC + 1 hour [ DST ]


Who is online

Users browsing this forum: No registered users and 10 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:  
Powered by phpBB® Forum Software © phpBB Group