Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Multiple controllable gameobjects, how to?

Discussion in 'Scripting' started by Mangas, Jan 13, 2015.

  1. Mangas

    Mangas

    Joined:
    Jan 16, 2013
    Posts:
    17
    I was wondering how to approach a problem I have with my design about multiple controllable gameobjects in the same game.

    I have an InputController class, where I manage all the input of my game, and multiple targets, like the menus, the player and some enemies which can be controlled by the player with certain skills. Every controllable object implements the interface IControllable, and in the InputController there is a variable CurrentInputHolder which is the target currently selected.

    The thing is, I don't know what is best way to give control to these targets, do I let the objects assign themselves as CurrentInputHolder when they need it? Is there any pattern that can help me with this?

    I don't want CurrentInputHolder to be null at any time, the player always has something to control, and I want to make sure the right object is being assigned.
     
  2. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    This sounds like a very specific problem. I don't know about anyone else, but I don't think I could contribute anything useful without seeing some code, or knowing more about your use case.

    From your description, it seems to me that I would use the InputController to assign IControllables, rather than the other way around. I mean, it's called "InputController" which suggest to me it should make the decisions/assignments, and IControllables should only know about themselves and their implementation.

    Boom, you've just been taught encapsulation.
     
    Kiwasi likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    You might want something that might be best called an InputDispatcher, and it would have a notion of who is supposed to be getting input at any given instant.

    InputDispatcher would essentially know a lot about the relations between input clients in your game, while allowing the clients and the main input controller to remain simple/generic. Clients would let the dispatcher know that they want focus, or that they are done.

    That would allow the input dispatcher to give alternate input sources to your game, such as test sequencing inputs (playback of prerecorded taps for testing, for instance).
     
    Mangas likes this.
  4. Mangas

    Mangas

    Joined:
    Jan 16, 2013
    Posts:
    17
    I like the sound of the InputDispatcher, I can have a queue with the input sources and don't have to modify my current design too much!

    Thanks a lot!!! :D