Broadcast passing a class reference?

I want to execute a sing broadcast statement for INPUT changes, so I created a class called InputManager with several global variables such as

yaw
pitch
roll
throttleup
throttledown
headleft
headright
pause

and more.

The class is just a container for all the state information

Then instead of having to perform a getcomponent to retrieve the current state, I thought I could perform a Broadcast, passing a reference to the InputManager, and allow any other component that needs it to receive the reference such as this:

public class InputControlManager : MonoBehaviour 
{ 
    InputManager _inputs = new InputManager();

    void FixedUpdate()
    {
        _inputs.yaw = Input.GetAxis("Mouse X") * mouseXsensativity;

        BroadcastMessage("UpdateInputControlls", _inputs, SendMessageOptions.DontRequireReceiver);
    }
}

public class SomeOtherManager : MonoBehaviour 
{ 
    InputManager _inputs = new InputManager();

    void UpdateInputControlls( InputManager _im)
    {
	_inputs.acceleratePressed = _im.acceleratePressed;
	_inputs.deceleratePressed = _im.deceleratePressed;
	_inputs.fullspeedPressed = _im.fullspeedPressed;
	_inputs.fullstopPressed = _im.fullstopPressed;
	_inputs.mouseYaw = _im.mouseYaw;
	_inputs.mousePitch = _im.mousePitch;
	_inputs.mouseRoll = _im.mouseRoll;
    }
}

I get no errors with the above code, but the Broadcast is never received by the SomeOtherManager component. I assume it doesn’t like passing a class. Is there a way to format the call/receiver to make this work, or does someone have a better idea? I could go back to using a GetComponent, but I thought using broadcast is the better way to do it with a Component based engine?

Thanks

Larry

This is a silly question, but I have to ask: Or you actually creating an instance of InputControlManager and SomeOtherManager somewhere?

SomeOtherControlManager would be a separate component attached to several different game objects, if those objects needed to know anything about the input state

There would be only one InputControlManager attached to the top level of the player prefab. Ideally, instead of SomeOtherManager having its own copy of the InputManager data, I would just store a reference to the copy, but for now I’m not worried about optimization as much as functionality.

For example my vehicle has a separate components for steering, weapons, engines, etc… each wants to know about different input states. I was just hardcoding the input check in each component so it just looked at what it cared about. However, when I started to work on my AI that broke. I want to use the same steering, weapons, engines components for player or AI. Therefore, I decided put the InputControlManager into its own components attached to a player prefab, and an AIControlManager component attached to the AI prefab, but the rest of the prefab (more or less) behaves the same.

I know I could use if statements to check things like networkView.isMine or isAI or isHuman, or whatever else I want, and process different code paths that way, but that’s not very component oriented. I’m trying to get away from the traditional procedural way of coding and force myself to think component.

Any suggestions on how to best approach this is appreciated.

I understand what you’re trying to do, and I agree that some kind of message passing system does indeed help with keeping the code will encapsulated, more resilient to change, etc. My question was purely technical, since I don’t know how this would work if you didn’t actually have one actual instance of each component alive and kicking in your scene.

Anyways, I think that instead of rolling your won, you might want to look at one of the several messaging systems on the Wiki. I also have been using a little open source library called TinyMessenger. It has been working really well, even on iOS, and it’s just one C# file.

interesting. I hadn’t thought to use a separate event system, I assumed I could get the built in broadcast to work, but this might be a way to go. Thanks

I’m sure you can get the broadcast methods to work with a little bit of tinkering, but a messaging system is a better choice IMHO. At least none of the messaging system mentioned rely on reflection and specifying method names as strings.

I got it to work changing InputManager to a struct instead of a class, but this is really inefficient as its passing all the data on the stack, not a reference, and I’m storing the data in each component that needs it instead of a reference. At least it works. I will look into optimizing the routine probably with one of the suggested methods on the wiki. thanks again for pointing that out.