Skip to content

Commit

Permalink
assign llm if null based on scene and hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
amakropoulos committed Jul 10, 2024
1 parent fe7a8e9 commit 2fc0ec1
Showing 1 changed file with 50 additions and 1 deletion.
51 changes: 50 additions & 1 deletion Runtime/LLMCharacter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,10 @@ public void Awake()
if (!enabled) return;
if (!remote)
{
AssignLLM();
if (llm == null)
{
Debug.LogError("No llm provided!");
Debug.LogError($"No LLM assigned or detected for LLMCharacter {name}!");
return;
}
id_slot = llm.Register(this);
Expand All @@ -152,6 +153,54 @@ public void Awake()
InitHistory();
}

void OnValidate()
{
AssignLLM();
}

void Reset()
{
AssignLLM();
}

void AssignLLM()
{
if (remote || llm != null) return;

LLM[] existingLLMs = FindObjectsOfType<LLM>();
if (existingLLMs.Length == 0) return;

SortBySceneAndHierarchy(existingLLMs);
llm = existingLLMs[0];
string msg = $"Assigning LLM {llm.name} to LLMCharacter {name}";
if (llm.gameObject.scene != gameObject.scene) msg += $" from scene {llm.gameObject.scene}";
Debug.Log(msg);
}

void SortBySceneAndHierarchy(LLM[] array)
{
for (int i = 0; i < array.Length - 1; i++)
{
bool swapped = false;
for (int j = 0; j < array.Length - i - 1; j++)
{
bool sameScene = array[j].gameObject.scene == array[j + 1].gameObject.scene;
bool swap = (
(!sameScene && array[j + 1].gameObject.scene == gameObject.scene) ||
(sameScene && array[j].transform.GetSiblingIndex() > array[j + 1].transform.GetSiblingIndex())
);
if (swap)
{
LLM temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
swapped = true;
}
}
if (!swapped) break;
}
}

protected void InitHistory()
{
InitPrompt();
Expand Down

0 comments on commit 2fc0ec1

Please sign in to comment.