Skip to content

Commit

Permalink
[docs, fix] Comments and small fixes
Browse files Browse the repository at this point in the history
- Added comments to the Runtime scripts
- Fixed a few small issues with Components calling scripts with no name or method name
  • Loading branch information
Owmacohe committed Dec 7, 2023
1 parent 246eae0 commit 40853b8
Show file tree
Hide file tree
Showing 17 changed files with 594 additions and 111 deletions.
2 changes: 2 additions & 0 deletions Components/Nodes/Event.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class Event : DescantNodeComponent

public override DescantNodeInvokeResult Invoke(DescantNodeInvokeResult result)
{
if (ScriptName == "" || MethodName == "") return result;

if (DescantComponentUtilities.InvokeFromObjectOrScript(
this,
ObjectTag,
Expand Down
2 changes: 1 addition & 1 deletion Components/Nodes/StatisticReveal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public override DescantNodeInvokeResult Invoke(DescantNodeInvokeResult result)
{
DescantActor actor = DescantComponentUtilities.GetActor(this, result.Actors, ActorName);

if (actor == null) return result;
if (actor == null || ScriptName == "" || MethodName == "") return result;

if (DescantComponentUtilities.InvokeFromObjectOrScript(
this,
Expand Down
15 changes: 8 additions & 7 deletions Components/Nodes/TimedChoice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public class TimedChoice : DescantNodeComponent

[ParameterGroup("Tag of object to find")] public string ObjectTag;
[ParameterGroup("Script to find")] public string ScriptName;
[ParameterGroup("Method to call")] public string MethodName;
[ParameterGroup("Methods to call")] public string TimerMethodName;
[ParameterGroup("Methods to call")] public string FinishedMethodName;

[ParameterGroup("When timer runs out (base 1)")] public int ChoiceToPick;

Expand All @@ -29,22 +30,22 @@ public override bool FixedUpdate()

foreach (var i in GameObject.FindObjectsOfType<MonoBehaviour>())
{
if (!DescantComponentUtilities.InvokeFromObjectOrScript(
if (ScriptName != "" && TimerMethodName != "" && !DescantComponentUtilities.InvokeFromObjectOrScript(
this,
ObjectTag,
ScriptName,
MethodName,
TimerMethodName,
percentage.ToString()
)) DescantComponentUtilities.MissingMethodError(this, ScriptName, MethodName);
)) DescantComponentUtilities.MissingMethodError(this, ScriptName, TimerMethodName);

if (percentage >= 1)
if (ScriptName != "" && FinishedMethodName != "" && percentage >= 1)
if (!DescantComponentUtilities.InvokeFromObjectOrScript(
this,
ObjectTag,
ScriptName,
MethodName,
FinishedMethodName,
(ChoiceToPick - 1).ToString()
)) DescantComponentUtilities.MissingMethodError(this, ScriptName, MethodName);
)) DescantComponentUtilities.MissingMethodError(this, ScriptName, FinishedMethodName);
}

return true;
Expand Down
3 changes: 2 additions & 1 deletion Documentation/todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@
- Create a proper tutorial
- Add `DescantIfNode`
- ~~Make script calling easier~~
- Make autosave better
- Make autosave better
- Create log system
29 changes: 23 additions & 6 deletions Editor/DescantEditorUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace DescantEditor
public static class DescantEditorUtilities
{
#if UNITY_EDITOR

#region VisualElements

/// <summary>
Expand Down Expand Up @@ -74,11 +75,15 @@ public static List<T> FindAllElements<T>(VisualElement element) where T : Visual
#endregion

#region File Paths

/// <summary>
/// Extracts the file name of a Descant file from its path
/// </summary>
/// <param name="path">The full or local path ending in [filename].desc.json</param>
/// <param name="isDescantGraph">
/// Whether the file being pointed towards is a Descant Graph
/// (as opposed to a Descant Actor)
/// </param>
/// <returns>The name of the Descant file, without its folder structure or .desc.json extension</returns>
public static string GetDescantFileNameFromPath(string path, bool isDescantGraph = true)
{
Expand Down Expand Up @@ -166,8 +171,6 @@ public static string GetFullPathFromInstanceID(int instanceID)

#endregion

#region Misc

/// <summary>
/// Checks to see if two lists of some type are equal
/// </summary>
Expand All @@ -187,15 +190,19 @@ public static bool AreListsEqual<T>(List<T> a, List<T> b)

return true;
}

#endregion

#endif

/// <summary>
/// Saves a given DescantActor
/// </summary>
/// <param name="newFile">Whether this actor should be saved to a new file</param>
/// <param name="data">The actor data to be saved</param>
public static void SaveActor(bool newFile, DescantActor data)
{
#if UNITY_EDITOR
// Setting the local path if this is the first time
if (newFile) data.Path = DescantEditorUtilities.GetCurrentLocalDirectory() + data.Name + ".descactor.json";
if (newFile) data.Path = GetCurrentLocalDirectory() + data.Name + ".descactor.json";
#endif

// Saving to the full path
Expand All @@ -204,11 +211,21 @@ public static void SaveActor(bool newFile, DescantActor data)
DescantUtilities.FormatJSON(JsonUtility.ToJson(data)));
}

/// <summary>
/// Loads a DescantActor from the file at the given path
/// </summary>
/// <param name="fullPath">The full disc path to the file</param>
/// <returns>A generated actor</returns>
public static DescantActor LoadActorFromPath(string fullPath)
{
return LoadActorFromString(File.ReadAllText(fullPath));
}

/// <summary>
/// Loads a DescantActor from the given data string
/// </summary>
/// <param name="str">The JSON-formatted string of the actor</param>
/// <returns>A generated actor</returns>
public static DescantActor LoadActorFromString(string str)
{
return JsonUtility.FromJson<DescantActor>(str);
Expand Down
25 changes: 20 additions & 5 deletions Editor/DescantFileHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,26 @@ public static class DescantFileHandler
/// Checks to see whether the file with the supplied instanceID has the file type .desc.json
/// </summary>
/// <param name="instanceID">The instanceID of the file being checked</param>
/// <returns>Whether the file is a Descant file</returns>
/// <returns>Whether the file is a Descant Graph file</returns>
static bool IsDescantGraphFile(int instanceID)
{
string path = DescantEditorUtilities.GetFullPathFromInstanceID(instanceID);
return path.Substring(path.Length - 10) == ".desc.json";
}

/// <summary>
/// Checks to see whether the file with the supplied instanceID has the file type .descactor.json
/// </summary>
/// <param name="instanceID">The instanceID of the file being checked</param>
/// <returns>Whether the file is a Descant Actor file</returns>
static bool IsDescantActorFile(int instanceID)
{
string path = DescantEditorUtilities.GetFullPathFromInstanceID(instanceID);
return path.Substring(path.Length - 15) == ".descactor.json";
}

/// <summary>
/// Project view contextual menu edit option for Descant files
/// Project view contextual menu edit option for Descant Graph files
/// </summary>
[MenuItem("Assets/Edit Descant Graph")]
static void EditGraphFile() {
Expand All @@ -39,28 +44,35 @@ static void EditGraphFile() {
}

/// <summary>
/// Method to confirm that the edit option only shows up for Descant files
/// Method to confirm that the edit option only shows up for Descant Graph files
/// </summary>
/// <returns>Whether the selected file is a Descant files</returns>
/// <returns>Whether the selected file is a Descant Graph file</returns>
[MenuItem("Assets/Edit Descant Graph", true)]
static bool ConfirmEditGraphFile() {
return IsDescantGraphFile(Selection.activeObject.GetInstanceID());
}

/// <summary>
/// Project view contextual menu edit option for Descant Actor files
/// </summary>
[MenuItem("Assets/Edit Descant Actor")]
static void EditActorFile() {
EditorWindow.GetWindow<DescantActorEditor>("Descant Actor Editor").Load(
DescantEditorUtilities.GetFullPathFromInstanceID(
Selection.activeObject.GetInstanceID()));
}

/// <summary>
/// Method to confirm that the edit option only shows up for Descant Actor files
/// </summary>
/// <returns>Whether the selected file is a Descant Actor file</returns>
[MenuItem("Assets/Edit Descant Actor", true)]
static bool ConfirmEditActorFile() {
return IsDescantActorFile(Selection.activeObject.GetInstanceID());
}

/// <summary>
/// Project view contextual menu create option for Descant files
/// Project view contextual menu create option for Descant Graph files
/// </summary>
[MenuItem("Assets/Create/Descant Graph")]
static void CreateNewGraphFile()
Expand All @@ -82,6 +94,9 @@ static void CreateNewGraphFile()
}
}

/// <summary>
/// Project view contextual menu create option for Descant Actor files
/// </summary>
[MenuItem("Assets/Create/Descant Actor")]
static void CreateNewActorFile()
{
Expand Down
4 changes: 3 additions & 1 deletion Examples/ConversationUI.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -556,8 +556,10 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
graph: {fileID: 4900000, guid: 308f2ac7a29cf44298ec2d67504079d4, type: 3}
player: {fileID: 0}
NPC: {fileID: 0}
actors: []
displayOnStart: 1
displayOnStart: 0
response: {fileID: 2309141401438911275}
choices: {fileID: 8548063041029733740}
choice: {fileID: 5535320327982640812, guid: 975a384186ab54f8bbf0ab32f86b5472, type: 3}
Expand Down
Loading

0 comments on commit 40853b8

Please sign in to comment.