In the following script, my current problem is with “CanDisableSkill()”
That’s called for the purpose to check if the node is inside a closed cycle of turned on nodes. Which in that case you should be able to turn off any one node that maintains the cycle closed. Any after that you shouldn’t be able to because it no longer would be a closed cycle.
using System.Collections.Generic;
using UnityEngine;
public class SkillTreeManager : MonoBehaviour {
public static SkillTreeManager Instance;
private void Awake() {
if (Instance == null) Instance = this;
}
private bool firstNodeOn = false; //Says if there's already a "First Node" turned on
public bool CanDisableSkill(SkillTreeNode nodeToDisable) {
HashSet<SkillTreeNode> visitedNodes = new();
var adjacentNodes = nodeToDisable.GetAdjacentNodes;
foreach (var node in adjacentNodes) {
if (DetectCycle(node.GetSkillTreeNode, nodeToDisable, visitedNodes)) {
Debug.Log("True");
return true;
}
}
Debug.Log("False");
return false;
}
// DFS method to detect cycles in the skill tree
private bool DetectCycle(SkillTreeNode currentNodeToCheck, SkillTreeNode parentNode, HashSet<SkillTreeNode> visitedNodes) {
if (!currentNodeToCheck.TurnedOn) return false;
if (visitedNodes.Contains(currentNodeToCheck)) return true;
visitedNodes.Add(currentNodeToCheck);
var adjacentNodes = currentNodeToCheck.GetAdjacentNodes;
foreach (var node in adjacentNodes) {
SkillTreeNode adjacentNode = node.GetSkillTreeNode;
if (adjacentNode == parentNode) continue;
if (DetectCycle(adjacentNode, currentNodeToCheck, visitedNodes)) return true;
}
visitedNodes.Remove(currentNodeToCheck); // Backtrack - unmark the node
return false;
}
public void CheckIfCanConnect(SkillTreeNode skillTreeNode) {
var adjacentNodes = skillTreeNode.GetAdjacentNodes;
var adjacentNodesTurnedOn = new List<SkillTreeNode.Node>();
foreach (var node in adjacentNodes) {
if (node.GetSkillTreeNode.TurnedOn) {
adjacentNodesTurnedOn.Add(node);
}
}
if (skillTreeNode.GetisFirstNode && adjacentNodesTurnedOn.Count == 0 && !skillTreeNode.TurnedOn && !firstNodeOn) {
skillTreeNode.SetTurnedOn(true);
firstNodeOn = true;
Debug.Log("Turning First Node On");
return;
}
if (skillTreeNode.GetisFirstNode && adjacentNodesTurnedOn.Count == 0 && skillTreeNode.TurnedOn) {
skillTreeNode.SetTurnedOn(false);
firstNodeOn = false;
Debug.Log("Turning First Node Off");
return;
}
if (!skillTreeNode.GetisFirstNode && adjacentNodesTurnedOn.Count == 0) return;
if (adjacentNodesTurnedOn.Count > 0 && !skillTreeNode.TurnedOn) {
skillTreeNode.SetTurnedOn(true);
return;
}
else if (!skillTreeNode.GetisFirstNode && adjacentNodesTurnedOn.Count == 1 && skillTreeNode.TurnedOn) {
skillTreeNode.SetTurnedOn(false);
return;
}
if (skillTreeNode.TurnedOn && !skillTreeNode.GetisFirstNode && adjacentNodesTurnedOn.Count > 1 && CanDisableSkill(skillTreeNode)) {
skillTreeNode.SetTurnedOn(false);
return;
}
}
}
Now, my problem, in the following image, is that, the node in the red square, once activated screws the hole thing. Because if the nodes in the green rectangle are all on, any node connected to the nodes from that rectangle will always count as being in a closed cycle of turned on nodes because it can do the loop check through the nodes in that rectangle and come back to the same node which is not what I desire.
skill tree testing
I don’t know if I made my problem clear so if anything, you can ask and I’ll try to clarify the problem
Here are some example of the problem that can help to clarify my issue:
That red one shouldn’t be able to be turned off
undesired result
This one is correct though
desired result
But then you’re also able to do this which I didn’t want
undesired result
And when one of those two are turned off the problem no longer prevails
desired result
I understand the why everything is happening. I’m just having a hard time coming with something that fixes this little problem
What I wanted was that the player should be able to turn off a node, even if surrounded by more that one turned on node, as long as it was inside a closed cycle of turned on nodes but the current result is that if there another small cycle inside the big cycle it doesn’t work as intended!
3