Adam's Lair Forum
https://forum.adamslair.net:80/

Problem with ICmpInitializable
https://forum.adamslair.net:80/viewtopic.php?f=4&t=742
Page 1 of 1

Author:  Dreas [ 2016/03/18, 18:06 ]
Post subject:  Problem with ICmpInitializable

Hello!
So i have been playing around with Duality for some days now, really like it. It got some stability but besides that its really good!

So my problem right now is that i have a script that makes platforms that the player can jump to. I got the code in the OnInit method on a Component. The problems is that the editor calls the OnInit in the editor. Is there a way to only run the code when the GameObject is loaded in-game?

Author:  SirePi [ 2016/03/18, 20:20 ]
Post subject:  Re: Problem with ICmpInitializable

Hey, welcome to the forum! :mrgreen:

For your question, this code can tell you if you are in the editor or not.. simply reverse the result and you should be all set :)
Code:
if(DualityApp.ExecEnvironment == DualityApp.ExecutionEnvironment.Editor &&
   DualityApp.ExecContext == DualityApp.ExecutionContext.Editor)
{
    // you are in the editor
}


Good luck!

Author:  hsnabn [ 2016/03/18, 20:26 ]
Post subject:  Re: Problem with ICmpInitializable

Ooh, SirePi already posted while I was writing this up, but here you go (a bit different version):

You can accomplish this with by checking some properties in DualityApp:
Code:
void ICmpInitializable.OnInit(Component.InitContext context)
{
    // Remember to check InitContext!
    // Otherwise code will be triggered in other contexts as well.
    // Check the InitContext enum for more information.
    if (context == InitContext.Activate)
    {
        if (DualityApp.ExecContext == DualityApp.ExecutionContext.Game &&
            DualityApp.ExecEnvironment == DualityApp.ExecutionEnvironment.Launcher)
        {
            SpawnPlatform();
        }
    }
}

Author:  Adam [ 2016/03/18, 21:03 ]
Post subject:  Re: Problem with ICmpInitializable

This question is already answered completely, I just wanted to mention that there's also the Wiki article on Common Pitfalls covering the InitContext part of this. ^^

Edit: Also, if you want your code to run in the editor's sandbox mode, only check DualityApp.ExecContext and omit the checks for DualityApp.ExecEnvironment - the context covers "is your game being executed right now" regardless of its host, while the environment covers where your game is currently hosted. Depends on what behavior you need really.

Author:  Dreas [ 2016/03/19, 13:16 ]
Post subject:  Re: Problem with ICmpInitializable

Thanks for the replies! Dont know how I missed the wiki page.. :/
Anyways thanks for the help! :)

Author:  Kulko [ 2018/03/28, 09:56 ]
Post subject:  Re: Problem with ICmpInitializable

Okay I finally found this info and it helped me solve my problem.

However on the way to this I encountered the following bug, which I wanted to raise. If there is any better way to do this please tell me.

I have a component controller which in its onInit creates another component InitState describing the current gamestate.

Before I checked for being in the editor this got automatically run when adding the controller component to the game Object. So with adding one component I automatically added two. However in the scene tree only the second component (InitState) gets displayed and I had to deactivate components and reactivate components in order to see the controller. In the properties Pane both components are shown correctly.

Bonus Bug: I was not able to delete the reference to the InitState from the Controller via the red cross.

Author:  Adam [ 2018/03/28, 18:25 ]
Post subject:  Re: Problem with ICmpInitializable

Hey there! The two bugs are probably related, and I'd guess the editor somehow got into an inconsistent state there. Can you share the source code that caused this behavior?

Author:  Kulko [ 2018/03/29, 08:14 ]
Post subject:  Re: Problem with ICmpInitializable

Code:
        public void OnInit(InitContext context)
        {
            if (context == InitContext.Activate)
            {
                ChangeState<InitBattleState>();
            }
        }

        public virtual void ChangeState<T>() where T : State, new()
        {
            CurrentState = GetState<T>();
        }

        public virtual T GetState<T>() where T : State , new()
        {
            T target = GameObj.GetComponent<T>();
            if (target == null)
                target = GameObj.AddComponent<T>();
            return target;
        }

For context, I try to implement the lessons from this article: http://theliquidfire.com/2015/06/01/tactics-rpg-state-machine/

Author:  Adam [ 2018/03/29, 17:45 ]
Post subject:  Re: Problem with ICmpInitializable

Thanks! I have filed a new issue on GitHub to investigate this potential bug.

In the meantime, try modeling State as an object that's not a Component, or scheduling state changes and executing outside of OnInit.

Author:  Kulko [ 2018/03/29, 19:35 ]
Post subject:  Re: Problem with ICmpInitializable

No problem. Checking for being called in the editor solved the problem too. I just wanted to mention the display bug.

Page 1 of 1 All times are UTC + 1 hour [ DST ]
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/