Skip to content

Commit

Permalink
v3.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
sohomsahaun committed Jan 25, 2022
1 parent e64ad9d commit 17d31e4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 30 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<h1 align="center">SnowState 3.0.1</h1>
<h1 align="center">SnowState 3.0.2</h1>
<p align="center">@sohomsahaun</p>

---
Expand Down
2 changes: 1 addition & 1 deletion SnowState.yyp

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

70 changes: 42 additions & 28 deletions scripts/SnowState/SnowState.gml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* SnowState | v3.0.1
* SnowState | v3.0.2
* Documentation: https://github.com/sohomsahaun/SnowState/wiki
*
* Author: Sohom Sahaun | @sohomsahaun
Expand Down Expand Up @@ -78,7 +78,7 @@ function SnowState(_initState, _execEnter = true) constructor {
// Replace parent's events with defined ones
_events = variable_struct_get_names(_struct);
_i = 0; repeat (array_length(_events)) {
_event = _events[@ _i];
_event = _events[_i];
_state[$ _event] = {
exists: SNOWSTATE_EVENT.DEFINED,
func: method(owner, _struct[$ _event])
Expand Down Expand Up @@ -150,10 +150,13 @@ function SnowState(_initState, _execEnter = true) constructor {
event: _event
};
self[$ _event] = method(_temp, function() {
var _args = array_create(argument_count);
var _i = 0; repeat(argument_count) {
_args[_i] = argument[_i];
++_i;
var _args = undefined;
if (argument_count > 0) {
_args = array_create(argument_count);
var _i = 0; repeat(argument_count) {
_args[_i] = argument[_i];
++_i;
}
}
exec(event, undefined, _args);
});
Expand Down Expand Up @@ -201,7 +204,7 @@ function SnowState(_initState, _execEnter = true) constructor {

var _name, _i;
_i = 0; repeat (array_length(invalidStateNames)) {
_name = invalidStateNames[@ _i]; ++_i;
_name = invalidStateNames[_i]; ++_i;
if (_state == _name) {
if (_func != undefined) _func("State name can not be \"", _name, "\".");
return false;
Expand Down Expand Up @@ -250,7 +253,7 @@ function SnowState(_initState, _execEnter = true) constructor {

// Add to history
if (array_length(childQueue) > 0) {
history[@ 0] = childQueue[@ 0];
history[@ 0] = childQueue[0];
childQueue = [];
}

Expand Down Expand Up @@ -281,7 +284,7 @@ function SnowState(_initState, _execEnter = true) constructor {
with (__this) {
_arr = variable_struct_get_names(_struct);
_i = 0; repeat(array_length(_arr)) {
_event = _arr[@ _i];
_event = _arr[_i];
assert_event_name_valid(_event);
assert_event_available(_event);
_events[$ _event] = {
Expand All @@ -293,7 +296,7 @@ function SnowState(_initState, _execEnter = true) constructor {

_arr = variable_struct_get_names(defaultEvents);
_i = 0; repeat(array_length(_arr)) {
_event = _arr[@ _i];
_event = _arr[_i];
_defEvent = defaultEvents[$ _event];
if (!variable_struct_exists(_struct, _event)) {
_events[$ _event] = {
Expand All @@ -312,7 +315,7 @@ function SnowState(_initState, _execEnter = true) constructor {
/// @param {string} [state_name]
/// @param {array} [args]
/// @returns {SnowState} self
execute = method(other, function(_event, _state = __this.history[@ 0], _args = []) {
execute = method(other, function(_event, _state = __this.history[0], _args = undefined) {
with (__this) {
if (!is_state_defined(_state)) {
snowstate_error("State \"", _state, "\" is not defined.");
Expand All @@ -321,7 +324,10 @@ function SnowState(_initState, _execEnter = true) constructor {

currEvent = _event;
var _func = states[$ _state][$ _event].func;
with (owner) script_execute_ext(method_get_index(_func), _args);
with (owner) {
if (_args == undefined) _func();
else script_execute_ext(method_get_index(_func), _args);
}
}

return self;
Expand All @@ -330,8 +336,8 @@ function SnowState(_initState, _execEnter = true) constructor {
/// @returns {string} The current state
get_current_state = method(other, function() {
with (__this) {
var _state = ((array_length(history) > 0) ? history[@ 0] : undefined);
if (array_length(childQueue) > 0) _state = childQueue[@ 0];
var _state = ((array_length(history) > 0) ? history[0] : undefined);
if (array_length(childQueue) > 0) _state = childQueue[0];
return _state;
}
});
Expand All @@ -341,15 +347,15 @@ function SnowState(_initState, _execEnter = true) constructor {
history_add = method(other, function(_state) {
with (__this) {
if (historyEnabled) {
if (history[@ 1] == undefined) {
history[@ 1] = history[@ 0];
if (history[1] == undefined) {
history[@ 1] = history[0];
history[@ 0] = _state;
} else {
array_insert(history, 0, _state);
history_fit_contents();
}
} else {
history[@ 1] = history[@ 0];
history[@ 1] = history[0];
history[@ 0] = _state;
}
}
Expand Down Expand Up @@ -454,7 +460,7 @@ function SnowState(_initState, _execEnter = true) constructor {
with (__this) {
var _transition, _dest, _i;
_i = 0; repeat(array_length(_transitions)) {
_transition = _transitions[@ _i]; ++_i;
_transition = _transitions[_i]; ++_i;

// For reflexive wildcard transitions, change to source
_dest = _transition.to;
Expand Down Expand Up @@ -482,7 +488,7 @@ function SnowState(_initState, _execEnter = true) constructor {

_events = variable_struct_get_names(_parent);
_i = 0; repeat (array_length(_events)) {
_event = _events[@ _i];
_event = _events[_i];
_parEvent = _parent[$ _event];

_exists = SNOWSTATE_EVENT.NOT_DEFINED;
Expand Down Expand Up @@ -513,9 +519,9 @@ function SnowState(_initState, _execEnter = true) constructor {
_events = variable_struct_get_names(defaultEvents);

_i = 0; repeat(array_length(_states)) {
_state = states[$ _states[@ _i]];
_state = states[$ _states[_i]];
_j = 0; repeat(array_length(_events)) {
_event = _events[@ _j];
_event = _events[_j];
if (!variable_struct_exists(_state, _event)) {
_defEvent = defaultEvents[$ _event];
_state[$ _event] = {
Expand Down Expand Up @@ -600,6 +606,14 @@ function SnowState(_initState, _execEnter = true) constructor {
return false;
};

/// @param {string} state_name
/// @returns {bool} Whether state_name exists (true), or not (false)
state_exists = function(_state) {
with (__this) {
return variable_struct_exists(states, _state);
}
};

/// @returns {array} Array containing the states defined
get_states = function() {
with (__this) {
Expand All @@ -617,7 +631,7 @@ function SnowState(_initState, _execEnter = true) constructor {
/// @returns {string} The previous state
get_previous_state = function() {
with (__this) {
return ((array_length(history) > 1) ? history[@ 1] : undefined);
return ((array_length(history) > 1) ? history[1] : undefined);
}
};

Expand Down Expand Up @@ -696,7 +710,7 @@ function SnowState(_initState, _execEnter = true) constructor {
/// @returns {SnowState} self
inherit = function() {
with (__this) {
var _state = history[@ 0];
var _state = history[0];

if (!variable_struct_exists(parent, _state)) {
if (SNOWSTATE_DEBUG_WARNING) {
Expand All @@ -710,15 +724,15 @@ function SnowState(_initState, _execEnter = true) constructor {
_len = array_length(childQueue);
_str = "";
_i = 0; repeat (_len) {
if (childQueue[@ _i] == _state) break;
if (childQueue[_i] == _state) break;
++_i;
}

if (_i < _len) {
_str += string(_state);
repeat (_len-_i-1) {
++_i;
_str += " -> " + string(childQueue[@ _i]);
_str += " -> " + string(childQueue[_i]);
}
_str += " -> " + string(_state);
snowstate_error("Circular inheritance found. Inheritance chain: ",
Expand Down Expand Up @@ -851,7 +865,7 @@ function SnowState(_initState, _execEnter = true) constructor {

var _i, _from;
_i = 0; repeat (array_length(_source)) {
_from = _source[@ _i]; ++_i;
_from = _source[_i]; ++_i;
if (!is_string(_from) || (_from == "")) {
if (SNOWSTATE_DEBUG_WARNING) {
snowstate_trace("State name should be a non-empty string. Transition not added.");
Expand Down Expand Up @@ -1039,7 +1053,7 @@ if (!is_string(SNOWSTATE_REFLEXIVE_TRANSITION_NAME) || (string_length(SNOWSTATE_
}

// Some info
#macro SNOWSTATE_VERSION "v3.0.1"
#macro SNOWSTATE_DATE "22-11-2021"
#macro SNOWSTATE_VERSION "v3.0.2"
#macro SNOWSTATE_DATE "25-01-2022"

show_debug_message("[SnowState] You are using SnowState by @sohomsahaun (Version: " + string(SNOWSTATE_VERSION) + " | Date: " + string(SNOWSTATE_DATE) + ")");

0 comments on commit 17d31e4

Please sign in to comment.