Adam's Lair Forum

game development and casual madness
It is currently 2018/05/27, 03:13

All times are UTC + 1 hour [ DST ]




Post new topic Reply to topic  [ 10 posts ] 
Author Message
PostPosted: 2016/03/18, 18:06 
Newbie
Newbie

Joined: 2016/03/16, 00:50
Posts: 2
Role: Hobbyist
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?


Top
 Profile  
 
PostPosted: 2016/03/18, 20:20 
Forum Addict
Forum Addict
User avatar

Joined: 2013/09/19, 14:31
Posts: 860
Location: Italy
Role: Hobbyist
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!

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


Top
 Profile  
 
PostPosted: 2016/03/18, 20:26 
Forum Adept
Forum Adept

Joined: 2015/07/08, 16:41
Posts: 414
Role: Gamer
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();
        }
    }
}


Top
 Profile  
 
PostPosted: 2016/03/18, 21:03 
Site Admin
Site Admin
User avatar

Joined: 2013/05/11, 22:30
Posts: 2048
Location: Germany
Role: Professional
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.

_________________
Blog | GitHub | Twitter (@Adams_Lair)


Top
 Profile  
 
PostPosted: 2016/03/19, 13:16 
Newbie
Newbie

Joined: 2016/03/16, 00:50
Posts: 2
Role: Hobbyist
Thanks for the replies! Dont know how I missed the wiki page.. :/
Anyways thanks for the help! :)


Top
 Profile  
 
PostPosted: 2018/03/28, 09:56 
Newbie
Newbie

Joined: 2018/03/26, 15:43
Posts: 3
Role: Hobbyist
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.


Top
 Profile  
 
PostPosted: 2018/03/28, 18:25 
Site Admin
Site Admin
User avatar

Joined: 2013/05/11, 22:30
Posts: 2048
Location: Germany
Role: Professional
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?

_________________
Blog | GitHub | Twitter (@Adams_Lair)


Top
 Profile  
 
PostPosted: 2018/03/29, 08:14 
Newbie
Newbie

Joined: 2018/03/26, 15:43
Posts: 3
Role: Hobbyist
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/


Top
 Profile  
 
PostPosted: 2018/03/29, 17:45 
Site Admin
Site Admin
User avatar

Joined: 2013/05/11, 22:30
Posts: 2048
Location: Germany
Role: Professional
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.

_________________
Blog | GitHub | Twitter (@Adams_Lair)


Top
 Profile  
 
PostPosted: 2018/03/29, 19:35 
Newbie
Newbie

Joined: 2018/03/26, 15:43
Posts: 3
Role: Hobbyist
No problem. Checking for being called in the editor solved the problem too. I just wanted to mention the display bug.


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

All times are UTC + 1 hour [ DST ]


Who is online

Users browsing this forum: No registered users and 7 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