edited January 10 in ORK Scripting
I'm trying to start a phase grid battle from C#. I can see the move range of my leader unit, but I can't select another friendly unit unless I make this new unit the leader of the player group (which I don't want).

Here is what I do:
1) I create a grid.
2) I add a Battle component to an empty game object.
3) I configure the battle. I specify the grid, set the Start Type to None and check Auto-Join with a range of 50 (otherwise, it doesn't work). Everything else is unchecked.
4) I create a script assigned to the same object than the Battle component. Its purpose is to create the units, spawn them at the right spots on the battlefield and start the battle event. Here it is:

public class LargeScaleBattleInit : MonoBehaviour
{
private BattleComponent mainBattle;
private BattleGridComponent mainBattleGrid;
[SerializeField]
private List<int> playerUnitIDList;
[SerializeField]
private List<int> enemyUnitIDList;
private List<Combatant> playerUnitCbtList = new List<Combatant>();
private List<Combatant> enemyUnitCbtList = new List<Combatant>();
[SerializeField]
private List<BattleGridCellComponent> playerDeploymentCellList;
[SerializeField]
private List<BattleGridCellComponent> enemyDeploymentCellList;

private void Awake()
{
mainBattle = GetComponent<BattleComponent>();
mainBattleGrid = GameObject.Find("MainBattleGrid").GetComponent<BattleGridComponent>();
ORK.Battle.BattleStarted += OnBattleStart;
}

void Start()
{
for (int i = 0; i < playerUnitIDList.Count; i++)
{
Combatant c = ORK.Combatants.Create(playerUnitIDList[i], ORK.Game.ActiveGroup, true, true);
c.Init();
c.Object.Spawn(playerDeploymentCellList[i].transform.position, true, 0, false, Vector3.one);
playerUnitCbtList.Add(c);
}
ORK.Game.ActiveGroup.SetBattleGroup(playerUnitCbtList, true);
Group enemyArmy = new Group(1); //1 is the ID of the enemy faction...
for (int i = 0; i < enemyUnitIDList.Count; i++)
{
Combatant c = ORK.Combatants.Create(enemyUnitIDList[i], enemyArmy, true, true);
c.Init();
c.Object.Spawn(enemyDeploymentCellList[i].transform.position, true, 0, false, Vector3.one);
enemyUnitCbtList.Add(c);
}
enemyArmy.SetBattleGroup(enemyUnitCbtList, true);
//ORK.Battle.Join(playerUnitCbtList); //It doesn't change anything if I add this line. However, if I omit the next one, the battle refuses to start...
ORK.Battle.Join(enemyUnitCbtList);
mainBattle.StartEvent(playerUnitCbtList[0].GameObject);
}

void OnBattleStart()
{
//This is the equivalent of the "Place on grid" node...
for(int i = 0; i < playerUnitCbtList.Count; i++)
{
playerUnitCbtList[i].Grid.Cell = playerDeploymentCellList[i];
}
for (int i = 0; i < enemyUnitCbtList.Count; i++)
{
enemyUnitCbtList[i].Grid.Cell = enemyDeploymentCellList[i];
}

}
}

5) On the scene camera, I create a "Combatant Cell Picker" script. It goes like this:

[RequireComponent(typeof(Camera))]
public class BattlefieldHexPicker : MonoBehaviour
{
private Camera largeScaleBattleCam;
void Start()
{
largeScaleBattleCam = GetComponent<Camera>();
}
void Update()
{
if (Input.GetMouseButtonUp(0))
{
Vector3 mousePos = Input.mousePosition;
Ray screenRay = largeScaleBattleCam.ScreenPointToRay(mousePos);
int screenRayMask = 1 << LayerMask.NameToLayer("Grid");
RaycastHit screenRayHit;
bool isHitScreenRay = Physics.Raycast(screenRay, out screenRayHit, largeScaleBattleCam.farClipPlane, screenRayMask, QueryTriggerInteraction.Collide);
if (isHitScreenRay)
{
BattleGridCellComponent cell = screenRayHit.collider.transform.parent.GetComponent<BattleGridCellComponent>();
if(ORK.Battle.SelectingCombatant.IsPlayerControlled() && cell.Combatant != null)
{
if (cell.Combatant.IsPlayerControlled())
{
//If I uncomment the next line, it works, but I don't want to change the leader...
//ORK.Game.ActiveGroup.SetLeader(cell.Combatant, true);
ORK.Battle.SystemSettings.phase.CombatantSelected(cell.Combatant);
}
}
}
}
}
}

If I uncomment the line ORK.Game.ActiveGroup.SetLeader(cell.Combatant, true);, then it works: when I click on the hex the Choosing combatant is changed. However, I don't want to change the leader, only the selecting player. Also, I don't understand why I have to check the "Auto Join" parameter of the Battle component since I make the units manually join the battle with the ORK.Battle.Join(enemyUnitCbtList) command.

I'd like to know what I'm doing wrong that could explain all those issues.

Thanks!
Post edited by ArsMagika on
  • The Battle component's StartEvent function will clear any previous battle's setup. Instead of joining the combatants to the battle and using the StartEvent function, use the StartGroup or StartGroups function to start the battle with the provided (enemy) groups.

    As for the cell picker - the CombatantSelected function should work, but I assume that the combatant isn't part of the current faction's phase. Are the combatants part of the player's battle 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!
  • Thanks for the answer GIL! I'll try with StartGroups as soon as I can and come back to you.

    As for your question, yes, the combatants I try to select are already part of the player's battle group and I try to select them during the player's faction phase. I thought it was necessary to have them in combat.

    I'm simply trying to select the combatants of the player group. I don't want to select them with ORK's interface because I have different kinds of phase combat in my game and I don't want the player to be able to select his units by clicking on them in the other kinds of phase combat. By coding the "picking" myself, I avoid changing the ORK parameters and I feel it is simpler.
  • edited January 10
    Ok, so I replaced StartEvent with StartGroups and removed the Join command. The battle starts with the "leader" unit selected. However, when I select a non-leader player unit in the player's phase, the selection highlight disappears for a frame but it reappears right after on the same unit. I can't select another unit.

    It seems the CombatantSelect function does not work. This is weird because I already used that function in another combat type and it worked well...
    Post edited by ArsMagika on
  • Seems strange - internally, ORK also uses this function when e.g. using input keys to switch combatants.

    Btw, the built-in Player Combatant Selection settings in phase battles already have such a functionality - enable Allow Clicking and While Selected to be able to click-select a different 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!
  • I was able to solve the issue with this line:
    pGroup.LockBattleMember(combatant, false);

    I use this on every battle member of my groups and then, when I use ConbatantSelect, it works as expected.

    Could you explain me the use of this LockBattleMember function please?

    Thanks!
  • It's actually only used by the Lock Battle Member node - i.e. if this has an impact, it means you previously have actively locked those battle members to not be removed from the battle 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!
Sign In or Register to comment.