Skip to content

Commit

Permalink
Adds ChatterboxNodeChangeCallback()
Browse files Browse the repository at this point in the history
  • Loading branch information
JujuAdams committed Sep 4, 2024
1 parent 755556c commit 7e71dea
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 12 deletions.
1 change: 1 addition & 0 deletions chatterbox.yyp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 21 additions & 5 deletions scripts/ChatterboxCreate/ChatterboxCreate.gml
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,8 @@ function __ChatterboxClass(_filename, _singleton, _local_scope) constructor
return undefined;
}

current_node = _node;
__ChangeNode(_node, true);
current_instruction = current_node.root_instruction;
current_node.MarkVisited();

__ChatterboxVM();
}
Expand Down Expand Up @@ -159,9 +158,8 @@ function __ChatterboxClass(_filename, _singleton, _local_scope) constructor
return undefined;
}

current_node = _node;
__ChangeNode(_node, true);
current_instruction = current_node.root_instruction;
current_node.MarkVisited();

__ChatterboxVM();
}
Expand Down Expand Up @@ -193,7 +191,7 @@ function __ChatterboxClass(_filename, _singleton, _local_scope) constructor
return undefined;
}

current_node = _node;
__ChangeNode(_node, false);
current_instruction = _next;

__ChatterboxVM();
Expand Down Expand Up @@ -512,4 +510,22 @@ function __ChatterboxClass(_filename, _singleton, _local_scope) constructor
array_resize(optionStructArray, _count);
array_resize(optionWeightArray, _count);
}

static __ChangeNode = function(_newNode, _markAsVisited)
{
var _oldNode = current_node;

current_node = _newNode;
if (_markAsVisited) current_node.MarkVisited();

if (is_undefined(_system.__nodeChangeCallback))
{
//Do nothing!
}
else if (is_method(_system.__nodeChangeCallback) || script_exists(_system.__nodeChangeCallback))
{
_system.__nodeChangeCallback((_oldNode != undefined)? _oldNode.title : undefined,
(_newNode != undefined)? _newNode.title : undefined);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Feather disable all

/// Sets a callback functiojn that is executed whenever a chatterbox changes node. The callback
/// will be executed in the following situations:
///
/// - Jumping to a node
/// - Hopping to a node
/// - Hopping back to a node
///
/// Three arguments are passed to the callback function: the name of the old node and the name of
/// the new node (in that order).

function ChatterboxNodeChangeCallback(_function)
{
static _system = __ChatterboxSystem();

_system.__nodeChangeCallback = _function;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions scripts/__ChatterboxSystem/__ChatterboxSystem.gml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ function __ChatterboxSystem()
__variablesMap = ds_map_create();
__variablesList = ds_list_create();
__variablesSetCallback = undefined;
__nodeChangeCallback = undefined;
__constantsMap = ds_map_create();
__constantsList = ds_list_create();
__defaultVariablesMap = ds_map_create();
Expand Down
15 changes: 8 additions & 7 deletions scripts/__ChatterboxVM/__ChatterboxVM.gml
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,9 @@ function __ChatterboxVMInner(_instruction)
{
var _next_node = FindNode(_split.node);
if (_next_node == undefined) __ChatterboxError("Node \"", _split.node, "\" could not be found in \"", filename, "\"");
_next_node.MarkVisited();

__ChangeNode(_next_node, true);
_next = _next_node.root_instruction;
current_node = _next_node;
}
else
{
Expand All @@ -314,9 +314,9 @@ function __ChatterboxVMInner(_instruction)

_next_node = FindNode(_split.node);
if (_next_node == undefined) __ChatterboxError("Node \"", _split.node, "\" could not be found in \"", _split.filename, "\"");
_next_node.MarkVisited();

__ChangeNode(_next_node, true);
_next = _next_node.root_instruction;
current_node = _next_node;
}
else
{
Expand Down Expand Up @@ -368,9 +368,10 @@ function __ChatterboxVMInner(_instruction)
__ChatterboxTrace("Error! File \"", _split.filename, "\" not found or not loaded");
}

file = _file;
filename = file.filename;
current_node = _node;
file = _file;
filename = _file.filename;

__ChangeNode(_node, false);
}
break;

Expand Down

0 comments on commit 7e71dea

Please sign in to comment.