# How can I facilitate communication between “attack” and “movement” components?

I'm currently programming in Unreal, but this is more of a component oriented design question.

I'm trying to follow a component driven aproach, and I have currently achieved creating a movement component which gets connected to the character object, it can do the following (apart from the usual walk, jump behaviour)

• WallSlide/WallClimb (like parkour) on Walls that the character cannot climb.

• Ledge Grab when reaching the apex of a wall, the character will start grab the ledge and climb to the end so he can be on top of the ledge.

• Hook Movement Implementation: you set a target point and the movement component will make the character travel to that point, triggering an event when it reaches the destination or it's interrupted by something in it's journey

The problem is that I'm right now trying to figure where and how to put the "attack" actions of the character. Say I make an Attack component, how do I deal with the weapon or attack wanting to move the character while the movement component is wanting to do the same thing (think of a shoryuken or a stinger)?

The thing that confuses me is that the movement is the domain of the movement component, and adding the movement logic of the combo breaks the decoupling of the modules, if I have to program the movement component thinking of the attack component.

I know this is an abstract question and i have asked it aroud everywhere, but I just cannot find some good material on how to approach this, every place I check, they use some "patch like" programing where they program directly to the character and don't worry decoupling the behavior.

• On the design front, why are wall slide, climb, grab etc different components as opposed to states? Like you couldn't you have a generic component for movement that can in turn run the required state? – Benzi Jun 11 '16 at 22:34
• Are we talking Unreal 1, 2, 3 or 4? – ZEKE Jun 12 '16 at 12:15
• @Benzi they are movement modes like MOVE_Falling or MOVE_Walking, only they are used in PhysCustom, they are indeed states of my movement component. The problem comes when other components like attack component want to move the character, like a lunging attack, should move the character due the corresponding attack, but it isn't quite the responsibility of the movement component – Carlos Ibáñez Chacana Jun 12 '16 at 20:49
• @ZEKE unreal engine 4... sorry, should have mentioned that – Carlos Ibáñez Chacana Jun 12 '16 at 20:49

One way to approach this is to have some facility for components that don't normally deal with movement to request of the appropriate that a movement occur.

If you're using a message-passing approach to talk between components, you'd just fill out a RequestMovement message with information about the desired movement or animation and send it off, and the movement component would trap and respond to it.

In Unreal's case, the component architecture it uses allows you to directly obtain a reference to the movement component from the attack component, and so you could just call myMovementComponent->RequestStartMovement(WhateverMovement).

The RequestStartMovement function can check to see if the character's current locomotion state allows for the requested movement to begin, and if so, begin the motion. One advantage of this approach versus the message-passing approach is that RequestStartMovement can immediately return a value indicating if the request was approved, so your attack component can (for example) abort the rest of its attack processing if the required motion could not be started for some reason.

This implies that your "movements" must be factored out into data or at least into some fashion where there are unique identifiers for each movement or animation you'd want to be able to request so that components can refer to them without having to know anything more about the movement other than the fact that it exists under a given name (in the past I've used Blueprint enumerations for this; they are trivial to extend as data and can be correlated to animations or more complex motion scripts fairly easily).

• Wouldn't i be breaking composition by making attack components know that a movement component exist or requiring it for the attacks? In unreal's case the movement component can use the Root Motion to use the animation's movement to be used on the character (and ignore the normal physics of the mode) – Carlos Ibáñez Chacana Jun 11 '16 at 19:15
• Sure, a little. You could minimize that information leak by moving the API to talk to the movement component to the actor (a custom actor subtype) if you wanted, but that limits how you can re-use your component. In practice you must leak some information about the existence of a way to trigger motion; in the grand scheme of things, acquiring a transient reference to the motion component is not going to destroy the world. – Josh Jun 11 '16 at 19:50
• This may be asking too much, but i think you are onto something... is it posible you could provide me with some example because i cannot grasp entirely the solution? thanks a lot man – Carlos Ibáñez Chacana Jun 12 '16 at 20:52