Will Mediation binder work if instantiate a gameobject with view in runtime?

144 views
Skip to first unread message

bhargav Rahul

unread,
Jun 6, 2015, 2:32:03 AM6/6/15
to stran...@googlegroups.com

Hi everyone,

I'm a bit of a StrangeIOC newbie, but I've been getting up to speed on everything recently, and understand the fundamentals.

I've encountered a Problem I'm sure is a very simple one yet i'm not able to see why i'm running into it.

Lets say In a small game i have a CreateMonsterSignal that is bound to a CreateMonsterCommad .

The signal is dispatched when i Click a button.

the CreateMosterCommand instantiates a prefab from my resources

The Prefab has a MosterVIew attached to it .I've bound the view to MonsterMediator in my game context. 

The problem is My MonsterMediator is not attached to the prefab after i call CreateMosterCommand.
 I am under the impression that Strange automatically binds and attaches the Mediator to the view i.e It get attached to the GameObject. Please point out my mistake.

Game Context

public class GameContext : SignalContext
 
{
   
public GameContext (MonoBehaviour contextView) : base (contextView)
   
{
   
}
   
protected override void mapBindings ()
   
{
     
base.mapBindings ();
     
       
//other commands and signals
      commandBinder
.Bind<CreateMonsterSignal> ().To<CreateMonsterCommand> ();
      mediationBinder
.Bind<MonsterView> ().To<MonsterMediator> ();
   
}
   
public override void Launch ()
   
{
      injectionBinder
.GetInstance<StartGameSignal> ().Dispatch ();
   
}
 
}

MonsterMediator

using System;
using strange.extensions.mediation.impl;
public class MonsterMediator: Mediator
 
{
 
[Inject]
 
public MonsterView MonsterView { get; set; }


 
[Inject]
 
public PlayMonsterAnimationSignal PlayMonsterAnimationSignal { get; set;}
                 
   
public override void OnRegister ()
   
{
     
MonsterView.Initialize ();
     
PlayMonsterAnimationSignal.AddListener (PlayMonsterAnimation);
   
}
   
public override void OnRemove ()
   
{
       
PlayMonsterlAnimationSignal.RemoveListener (PlayMonsterAnimation);
   
}
   
public void PlayMonsterAnimation()
   
{
//       Do some animation
   
}
 
}


Robert Noack

unread,
Jun 6, 2015, 10:15:42 AM6/6/15
to stran...@googlegroups.com
Hi,

I'm pretty sure the problem that you are having is because you attached the view to the prefab, but you need to attach the view at runtime. Just remove the view from the prefab, and inside your code, instantiate the prefab without the view. Then add the line :

gameObject.AddComponent<MonsterView>();

bhargav Rahul

unread,
Jun 6, 2015, 11:30:08 AM6/6/15
to stran...@googlegroups.com
Hey,

Thanks for the response , I have tried the same but still the mediator is not getting attached to the game object hence i'm not able to use any signals....
I'm sure I've made an obvious mistake....but its just one of those things.......at least I'm going to learn how to be "Strange".... :D

Cheers  

Robert Noack

unread,
Jun 7, 2015, 3:15:07 PM6/7/15
to stran...@googlegroups.com
Its kind of hard to know what can be the problem because its likely a small mistake in the code somewhere.

Do you have an object in your scene with the Gamecontext script attached?
Does your CreateMonsterSignal ever get fired?

Also not 100% sure about this but I think you have to make the prefabs under the main game object
i.e. if your gamecontext is attached to Gameroot, make sure you are creating the prefabs under GameRoot

If none of that helps maybe someone has some more ideas :)

Bhargav Rahul

unread,
Jun 8, 2015, 12:38:04 AM6/8/15
to stran...@googlegroups.com
Hey!!

That was very helpful! Turns out i wasn't making it the child of my GameObject which holds the Context..
Thanks a lot!...
Reply all
Reply to author
Forward
0 new messages