I'm trying to spawn a combatant from script at the start of a battle.

In the Battle Start Event of that battle, I use a Call Function node to call the script that will spawn the combatant.
https://imgur.com/pgV8zbQ
The script is attached to the player prefab.

The code I used to spawn a Combatant
public void startBossFight(int max)
{
Debug.Log("In Function");
Combatant newCombatant = ORK.Combatants.Create(12, ORK.Game.ActiveGroup, true, true);
newCombatant.Init();
ORK.Game.ActiveGroup.JoinBattle(newCombatant);
}


The compiler doesn't give any error, but when I start the battle, I get the following error in the console, and the script doesn't spawn combatants.
https://imgur.com/R2352cK

The Debug.Log statement doesn't print anything to the console either, so I don't think anything inside of the function is executed at all.
  • The Player Group actor has all player combatants participating in battle, so it'd try to call that function on each of them, which might cause the errors/warnings on the combatants that don't have the component, while still executing it on the player itself.

    You could try using a static method (via Static class origin), since this doesn't seem to use any fields from the component instance, that'd only execute it once.

    The issue here is mainly that you only join the combatant to the player's battle group - this doesn't spawn them or adds them to an already ongoing battle. You also need to call ORk.Battle.Join(newCombatant) to add the combatant to the ongoing battle, which should spawn it at the next free battle spot.
    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 managed to fix the error, and added the ORK.Battle.Join() statement, but the new combatant still doesn't spawn.

    My code looks like this now

    public void startBossFight(int max)
    {
    Combatant newCombatant = ORK.Combatants.Create(12,
    ORK.Game.ActiveGroup, true, true);

    Debug.Log("Step 1");
    newCombatant.Init();

    Debug.Log("Step 2");
    ORK.Game.ActiveGroup.Join(newCombatant, false, true);

    Debug.Log("Step 3");
    ORK.Game.ActiveGroup.JoinBattle(newCombatant);

    Debug.Log("Step 4");
    ORK.Battle.Join(newCombatant);
    Debug.Log("Joined");
    }


    I confirmed that all of the Debug.Log statements were printed to the console, but it still didn't spawn a new combatant into the battle.
  • edited March 2021
    You can spawn a combatant manually via combatant.Object.Spawn function, which takes parameters for position, rotation, etc.
    Post edited by gamingislove on
    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!
  • Hi Void and I are working on the same game.

    1. Using combatant.Object.Spawn() wasn't doing the trick either. Is there anything I have to do after calling Spawn to get the combatant to appear? I'm definitely getting the correct Combatant since I am able to Debug Log the name. However, there is no Combatant being added to the hierarchy in Unity.

    2. Do I need to Create and Init a combatant even If I've already set them up in the Combatant list in my Framework?

    3. I was previously able to do this using Nodes and it was working, but I need to scale it using a script. The Nodes I was calling that worked was "Join Battle" -> "Join Battle Group". When I look up JoinBattleGroup in the API it is a BattleStep and I'm not sure how to use that.
  • 1) Do you get any message in the Unity console?
    The only way I see the Spawn function not working is if you don't have a prefab set up for the combatant.

    2) The combatants defined in ORK are just the settings, in-game you need to create individual instances of them, which is usually handled by combatant spawners or battles.

    3) The code above is correct, although you don't even need to use Join and JoinBattle here, because you already pass on the group when creating the combatant, which automatically joins it to the group and battle group (if there's a free spot).

    What's the player battle group size (Game > Game Settings > Player/Group Settings) and how many combatants are already part of it?
    Might be that the player's battle group is already full and can't add another combatant.
    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!
  • So I'm using List<Combatant> party = ORK.Game.ActiveGroup.GetGroup(); to get a list of party members in the group, and then use a for loop to go through the list to add or remove combatants based on who is in the list.

    for (int x = 0; x < party.Count; x++)
    {
    if (party[x].GetShortName() == "Combatant")
    {
    ORK.Game.ActiveGroup.LeaveBattle(party[x], true);

    (The block of code from the previous post that creates a newCombatant and join it to
    battle)
    }
    }


    Would using a for loop cause any problems? After testing, I found that the program runs find and does what I want for the first 2 entries of the list, but throws an error on the third entry regardless of what order the combatants are in.
  • Try this instead:
    List<Combatant> party = new List<Combatant>(ORK.Game.ActiveGroup.GetGroup());

    GetGroup returns the actual combatant list, i.e. it'll change when you add or remove members from the group (though only removing from battle group doesn't change it).
    The code above packs them into a new list, so it'll not be affected by group changes.
    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!
  • That didn't fix the problem. The get party line is also outside of the for loop, so it won't get called again and updates the combatant list right?
  • What error are you getting?

    As said, GetGroup returns the list of combatants also used by the group itself, i.e. any changes you do to the group will automatically also be in the list you got from this function. Doesn't matter if you don't call it again, as it operates on the same list. That's why you have to pack it into a new list as shown above if you do changes to the group.
    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!
  • https://imgur.com/R2352cK
    This is the error I'm getting.

    I also tried to call the function as static method, but the same problem still persists.

  • Can you post the whole function as it currently is? That'd allow me to test it :)
    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!
  • Here's the file containing the function:
    https://ufile.io/dkqcip61

    troupList.troupeComp refers to a Singleton Dictionary that contain the name of combatants the player has in reserves, and the quantity of each.
  • UPDATE:
    I was able to make the StartBossBattle function an IEnumerator and call that instead. This solved the issue of the thread being exited before the function completed (I think that was the issue anyway).

    The only problem is now when I call -
    ORK.Game.ActiveGroup.LeaveBattle(party[x], true);
    The combatant party[x] is (correctly) not in the battle, but they still spawn and are on the battlefield. Is there something additional to call to remove their gameobjects as well?
  • Might be a timing issue, e.g. if you spawn and leave the battle group (with destroying the prefab) in the same frame, the destruction probably doesn't happen because Unity didn't spawn it yet.

    Adding a short Wait node (even 0 seconds to continue in the next frame) should be enough to fix that.
    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.