Coding combat?

Where would the logic of combat be done? in game mode or individual character classes? if so, how would I access the enemies / targets object so I could call methods in their class? (so I can call takeDamage method, etc)

Well that should go into your pawns/weapons.

Anything else is highly dependent on your combat system.

For example if you had guns, you could either use

  1. InstantHit weapons using traces (the traces return the hit actor that you can damage)
  2. Projectiles that collide with an Actor that you can damage. Collisions fire the OnHit() event that passes the hit actor as parameter.

And well, if you want melee there are serveral different ways to do this as well. Either just use a simple trace from the camera with melee range or have a trace from shoulder to arm, or sword trace etc… you get the point.

Thanks for the reply, I should have been more specific in the original post :slight_smile:

Combat : Player uses tab which cycles through all the enemies on the opposing team, cast a spell or attack (within range) which will deal damage to enemy target.

To do this I thought about having an array of players which is held in the gameMode and in the class which extends off of Pawn, have it change target depending on position in the array?

so for example (forgive the pseudo code done in 2 seconds :stuck_out_tongue: ):


PlayerPawn players [6];


for (int i = 0; i < players.size(); I++)
    if (Target pressed)
           target += 1;

if (attack1 pressed)

well, there are ActorIterators that just give you the references pretty easily like this:

for (TActorIterator<**AActor**> ActorItr(GetWorld()); ActorItr; ++ActorItr )

just swap **AActor **with what ever class you are trying to aim at.

just put this code in your PlayerCharacter and go from there. You should stay out of the GameMode for stuff like damage, movement, basic player logic.
The GameMode should handle game rules, like what pawn/controller to spawn, scores, win conditions etc.

ahhh perfect, you’re a life saver! thanks! last Question: is there a way for this to be run Server side only? (since everyone knows its bad to handle this stuff client side :p)

Yeah, just authority guard it by calling a server RPC that runs that code.

with like

void ClientInputTargetStuff();

UFUNCTION(Reliable, Server, WithValidation)
void ServerTargetNextEnemyOrSomethingAlongThoseLines();


//if we call this function on client-side, we route it to the server and let it handle Health and stuff.
void AYourCharacter::ClientInputTargetStuff()
       if(Role < ROLE_Authority)

bool AYourCharacter::ServerTargetNextEnemyOrSomethingAlongThoseLines_Validate()
     return true;
//This function only runs on the server. Make sure you change variables marked for replication to send them back to all clients, like health.
void AYourCharacter::ServerTargetNextEnemyOrSomethingAlongThoseLines_Implementation()
    //just make sure that TakeDamage changes a Variable that is marked for replciation. So if you create a Health Variable, make sure it is replicated to all clients.

But networking is actually a pretty big topic, and I would advice to just start a few small projects that only focus and the different methods of replication/RPC stuff.
Also, make sure to watch this It is Blueprint, but the basic principles apply to C++