I am trying to start a battle with an NPC in the current scene, do a bit of damage, escape, and when returning to the scene later (eg the next day in game), do a bit more damage, eventually reaching some victory condition.

I have an NPC with an "Event Interaction", and "Add Combatant" (with remember combatant checked) components. When the event is invoked it, after a short a dialog, it calls the "Start Battle" node using event object, and specifies the player as a combatant.

When battling, the combatant enters "DontDestroyOnLoad" and, as expected, the object carries over to the next scene(s). When eventually returning to the original combatant scene, it remembers its status values but a new NPC is also spawned, and when battling, spawns a new combatant with its status values set to their initial values (there are now two of these combatants in the scene).

If instead the combatant is explicitly destroyed (after battle or when leaving the scene) it remembers its status values the next time the scene is visited but when interacting with the NPC it will not start battle.

I've read over this post and this post however they don't seem quite the same.

I've tried a few other configurations, including using a battle component. but these are the ones that have given me the closest results. Any help is appreciated. Thanks!
  • You don't need to specify the player as a combatant in the Start Battle node - the player is automatically part of the battle.

    As you noticed, you should destroy the combatant at the end of the battle, otherwise the DontDestroyOnLoad will lead to 2 combatants, since the reloading scene doesn't know about the still existing game object and loads the NPC as usual.

    Generally, a battle can only start if there are combatants with hostile factions, i.e. the NPC's combatant needs to be of an enemy faction to be able to fight the player. When using a Battle component, the battle will not occur again if it's set up to use a Scene ID.
    Please consider rating/reviewing my products on the Asset Store (hopefully positively), as that helps tremendously with getting found.
    If you're enjoying my products, updates and support, please consider supporting me on patreon.com!
  • I tried destroying the combatant but it destroyed the NPC it was attached to as well.

    You're description of the Scene ID is what I expected. I'm pretty sure the Battle component wasn't using it but I can double check.

    Thanks for your help.
  • Well, yeah - if the NPC is a combatant (e.g. via Add Combatant component), it'll get destroyed. Alternatively, you can just use a regular NPC without a combatant and still start a battle, e.g. via a battle component.
    Please consider rating/reviewing my products on the Asset Store (hopefully positively), as that helps tremendously with getting found.
    If you're enjoying my products, updates and support, please consider supporting me on patreon.com!
  • I knew the NPC would get removed, was just trying to say that it doesn't seem to be the solution I'm looking for.

    The battle is taking place in the same scene as the NPC interaction and the player needs to fight against something, so I'm guessing I would still need to add the combatant to the NPC, or add a new combatant, but it would be using the same prefab as the NPC, so wouldn't it appear as a duplicate?

    I feel like I'll probably need to do something creative with having one game object for the NPC vs one for the combatant, and maybe add/remove them based on whether the battle is taking place or not, but open to suggestions. Unless I'm misinterpreting your suggestion.

    Appreciate the response. Thanks.
  • Hm, I see - yeah, that can be a bit tricky having everything continue in the same scene. Though, if you talk and fight in the same scene, the combatant shouldn't actually be marked as DontDestroyOnLoad, since there's no scene transition.

    Depending on your enter/end battle transitions are visually presented to the player this can be easier or a bit harder. E.g. if you use screen fades, you can mask the switch pretty easy be doing it when the screen is faded out (or the camera isn't looking at the NPC).

    A very simple solution would be to just reload the scene at the end of the battle. Otherwise, you can work with multiple game objects for the NPC and the combatant, enabling/disabling them as needed. In that case, I'd recommend to have the actual game event on a 3rd, separate game object, controlling the other 2.
    Please consider rating/reviewing my products on the Asset Store (hopefully positively), as that helps tremendously with getting found.
    If you're enjoying my products, updates and support, please consider supporting me on patreon.com!
Sign In or Register to comment.