# Turn-based combat - turn starts for two allies

**URL:** <https://forums.unrealengine.com/t/turn-based-combat-turn-starts-for-two-allies/2254367>\
**Category:** C++\
**Tags:** rpg, bug, turn-based, unreal-engine, question, CPP\
**Created:** [January 1, 2025, 7:09pm UTC](https://forums.unrealengine.com/t/turn-based-combat-turn-starts-for-two-allies/2254367 "2025-01-01T19:09:23Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wuka1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Wuka1](https://forums.unrealengine.com/u/Wuka1)\
**Post date:** [January 1, 2025, 7:09pm UTC](https://forums.unrealengine.com/t/turn-based-combat-turn-starts-for-two-allies/2254367/1 "2025-01-01T19:09:23Z")

</div>

I am making a turn-based RPG game in UE5 with C++ (with 2D sprites). Think of something like Darkest Dungeon but with memes and no procedural generation. For the most part my combat system works, but sometimes the turn triggers for an allied character that isn’t even in the party (another ally performs their attack) and sometimes the turn triggers for two allies at once - when one attacks, the other does too, same with using an items.  
For my allies I have two arrays - AlvailableAllies that stores all allies regardless of whether they’re in the party or not and AlliedParty - the actual party.  
For fight encounters I have an actor class - AFightEncounter that spawns enemies and deals with what happens after the fight and a regular object UFight that is created when the fight is started and handles the fight itself.  
I’ve tried debugging - looks like StartTurn() (and Use Item() as well when we choose to use an item) is called twice for different allies, I suspect it might be ralated to my timers - one that introduces a bit of delay between turns, the other that prevents the enemy from being stuck on their turn when they can’t use any skill. I honestly don’t have any idea what to do about it.

Here’s the code on pastebin, because I don’t know if there’s a character limit here:

> **[FightEncounter - Pastebin.com](https://pastebin.com/JJSiMXvq)**
>
> Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.

If it’s not very readible lket me know if i should split each source file into separate link. Thank you in advance.

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [January 1, 2025, 8:27pm UTC](https://forums.unrealengine.com/t/turn-based-combat-turn-starts-for-two-allies/2254367/2 "2025-01-01T20:27:44Z")

</div>

in Fight.cpp you seem to have pasted a header file. There are no implementations inside of Fight.cpp of any of the functions just a clone of Fight.h

---

<div class="post-metadata">

**Author:** ![Wuka1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Wuka1](https://forums.unrealengine.com/u/Wuka1)\
**Post date:** [January 1, 2025, 8:47pm UTC](https://forums.unrealengine.com/t/turn-based-combat-turn-starts-for-two-allies/2254367/3 "2025-01-01T20:47:18Z")

</div>

oh, sorry for that, edited it out

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [January 1, 2025, 10:33pm UTC](https://forums.unrealengine.com/t/turn-based-combat-turn-starts-for-two-allies/2254367/4 "2025-01-01T22:33:00Z")

</div>

Unfortunately there is too little information to dig into the problem.  
I’m guessing that maybe the building of AlliedParty inside of your CurrentGameMode may be the source of the problem. I don’t have access to the function that builds the allied party but maybe you are duplicating the pointer to a specific character (maybe a bad for loop?)

Maybe consider using AddUnique for your TArray to not add duplicates.

```auto
Fight->AlliedParty = CurrentGameMode->AlliedParty;

```

inside of `AFightEncounter::SetupAllies()`

This is the variable that really dictates how the turns pan out as it takes in the allied party and then enemies to build the turn order in `void UFight::BuildTurnOrder(){}`.

---

<div class="post-metadata">

**Author:** ![Wuka1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Wuka1](https://forums.unrealengine.com/u/Wuka1)\
**Post date:** [January 1, 2025, 10:57pm UTC](https://forums.unrealengine.com/t/turn-based-combat-turn-starts-for-two-allies/2254367/5 "2025-01-01T22:57:48Z")

</div>

I pasted my DefaultGameMode class here: [DefaultGameMode - Pastebin.com](https://pastebin.com/HgMmbNqi)  
Although I don’t understand how it could cause starting a turn twice with different allies. If you explained it a bit I’d really appreciate it.  
Edit: Oh, and i’m actually using AddUnique both for Available allies and AlliedParty, completely forgot that as it was couple of months ago

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [January 1, 2025, 11:06pm UTC](https://forums.unrealengine.com/t/turn-based-combat-turn-starts-for-two-allies/2254367/6 "2025-01-01T23:06:11Z")

</div>

Well it’s just causing more missing files at this point. Try putting a break-point durning the turn build process (in visual studio F9) and then try stepping through the code (F10) to see where the errors are occurring

You can disable code optimizations with this:

> [@How to disable optimization for whole project C++ for debug?](https://forums.unrealengine.com/t/how-to-disable-optimization-for-whole-project-c-for-debug/477074/5):
>
> Ran into similar issues when using compiler intrinsics, which clashes with some optimization flags. You might also want to disable the creation of precompiled header files via: // Do not use PCH or Unity builds, PCHUsage = ModuleRules.PCHUsageMode.NoPCHs; // Nor Optimization, since intrinsics clash with those options confusedOptimizeCode = CodeOptimization.Never; That did the trick for me.

It will help you evaluate the contents of TArrays while the code is running (usually optimizations will obstruct the code)

---

<div class="post-metadata">

**Author:** ![Wuka1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Wuka1](https://forums.unrealengine.com/u/Wuka1)\
**Post date:** [January 2, 2025, 7:23pm UTC](https://forums.unrealengine.com/t/turn-based-combat-turn-starts-for-two-allies/2254367/7 "2025-01-02T19:23:23Z")

</div>

Other than this on the screenshot I don’t see anything weird (it just is like that at the end, when I’m enqueueing the tail is the first character from BothParties, then it disappears). BothParies array is fine, nothing unexpected here.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/f/d/afdd322085bf85b533c5c5414f19e3f65f5f4a98.png)  
Maybe it’s garbage collected somehow, but when I try set it as a Uproperty it won’t compile. Maybe i can ditch the queue and make it a regular array that i remove the last element from at the beggining of the turn so i can make it a uproperty but I don’t know if it’ll make it any better

---

<div class="post-metadata">

**Author:** ![Wuka1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Wuka1](https://forums.unrealengine.com/u/Wuka1)\
**Post date:** [January 2, 2025, 7:31pm UTC](https://forums.unrealengine.com/t/turn-based-combat-turn-starts-for-two-allies/2254367/8 "2025-01-02T19:31:16Z")

</div>

Ok, when it built turn order the second time there was an ally that is not in my party and it’s the one that i swaped out before the fight

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [January 2, 2025, 8:13pm UTC](https://forums.unrealengine.com/t/turn-based-combat-turn-starts-for-two-allies/2254367/9 "2025-01-02T20:13:17Z")

</div>

Maybe you should consider isolating the queue for the fight & it’s logic to a separate fight manager. Right now you seem to have the logic for building and managing the flow of battle spread out to many areas.

Sometimes the game mode, other times the in AFightEncounter.

---

<div class="post-metadata">

**Author:** ![Wuka1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Wuka1](https://forums.unrealengine.com/u/Wuka1)\
**Post date:** [January 2, 2025, 8:47pm UTC](https://forums.unrealengine.com/t/turn-based-combat-turn-starts-for-two-allies/2254367/10 "2025-01-02T20:47:43Z")

</div>

Anything that happens during the battle is handled in UFight. I have to have my party in the game mode as I need it in the exploration and dialogues

---

<div class="post-metadata">

**Author:** ![Wuka1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Wuka1](https://forums.unrealengine.com/u/Wuka1)\
**Post date:** [January 4, 2025, 8:06pm UTC](https://forums.unrealengine.com/t/turn-based-combat-turn-starts-for-two-allies/2254367/11 "2025-01-04T20:06:13Z")

</div>

The funny thing is it doesn’t happen when staring new game or loading from save, only after adding new ally to party/replacing one ally with another, I change how I handle that but it still happens

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [January 4, 2025, 8:08pm UTC](https://forums.unrealengine.com/t/turn-based-combat-turn-starts-for-two-allies/2254367/12 "2025-01-04T20:08:42Z")

</div>

Could it be that 2 allies always just have a higher speed that dictate that they start the turn?  
Maybe the speed calculation is somehow influencing the outcome?

---

<div class="post-metadata">

**Author:** ![Wuka1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Wuka1](https://forums.unrealengine.com/u/Wuka1)\
**Post date:** [January 4, 2025, 8:13pm UTC](https://forums.unrealengine.com/t/turn-based-combat-turn-starts-for-two-allies/2254367/13 "2025-01-04T20:13:08Z")

</div>

but that just sorts the array and it seems to be sorting it fine from what I’ve seen in the debugger

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [January 4, 2025, 8:21pm UTC](https://forums.unrealengine.com/t/turn-based-combat-turn-starts-for-two-allies/2254367/14 "2025-01-04T20:21:56Z")

</div>

I’ve noticed that the Fight has it’s own timer and each character internally also has their own timer.  
you use  
`CurrentCharacter->TurnTimer` at one point but do not ever clear or reset it. Is this done internally through `ACombatCharacter::BeginTurn` function?

---

<div class="post-metadata">

**Author:** ![Wuka1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Wuka1](https://forums.unrealengine.com/u/Wuka1)\
**Post date:** [January 4, 2025, 10:13pm UTC](https://forums.unrealengine.com/t/turn-based-combat-turn-starts-for-two-allies/2254367/16 "2025-01-04T22:13:30Z")

</div>

No, everthing is inside UFight, the TurnTimer handle exist only in UFight, I used characters because only Actors have access to the world timer manager

EDIT: I completely forgot the characters have their own turn timer as well lol

---

<div class="post-metadata">

**Author:** ![Wuka1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Wuka1](https://forums.unrealengine.com/u/Wuka1)\
**Post date:** [January 7, 2025, 10:29pm UTC](https://forums.unrealengine.com/t/turn-based-combat-turn-starts-for-two-allies/2254367/17 "2025-01-07T22:29:20Z")

</div>

I pushed my sourcecode to github as I plan to release the game free on itch anyway. i made some changes to the classes I included in the pastebin file but those issues still persist [GitHub - WKogut98/WirSprawiedliwosci: Turn-based RPG based on proboscis monkey meme](https://github.com/WKogut98/WirSprawiedliwosci)

---

<div class="post-metadata">

**Author:** ![Wuka1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Wuka1](https://forums.unrealengine.com/u/Wuka1)\
**Post date:** [January 9, 2025, 11:27pm UTC](https://forums.unrealengine.com/t/turn-based-combat-turn-starts-for-two-allies/2254367/18 "2025-01-09T23:27:30Z")

</div>

Copilot suggested checking if TurnQueue is empty at the start of a turn, to my surprise it might have worked, because I played 3 fights like I did before and haven’t encountered those problems. Obviously the sample size is a bit too small to know for sure and that solution seem random but I’ll take whatever I can get
