Skip to content

Commit bd32776

Browse files
committed
Relocate "default_transition" and use it when teleporting in an animation state machine.
1 parent f418603 commit bd32776

File tree

2 files changed

+45
-22
lines changed

2 files changed

+45
-22
lines changed

scene/animation/animation_node_state_machine.cpp

+41-21
Original file line numberDiff line numberDiff line change
@@ -774,10 +774,8 @@ AnimationNode::NodeTimeInfo AnimationNodeStateMachinePlayback::_process(const St
774774
// Can't travel, then teleport.
775775
if (p_state_machine->states.has(temp_travel_request)) {
776776
path.clear();
777-
if (current != temp_travel_request || reset_request_on_teleport) {
778-
_set_current(p_state_machine, temp_travel_request);
779-
reset_request = reset_request_on_teleport;
780-
teleport_request = true;
777+
if (current != temp_travel_request) {
778+
path.push_back(temp_travel_request);
781779
}
782780
} else {
783781
ERR_FAIL_V_MSG(AnimationNode::NodeTimeInfo(), "No such node: '" + temp_travel_request + "'");
@@ -789,18 +787,20 @@ AnimationNode::NodeTimeInfo AnimationNodeStateMachinePlayback::_process(const St
789787

790788
if (teleport_request) {
791789
teleport_request = false;
792-
// Clear fadeing on teleport.
793-
fading_from = StringName();
794-
fadeing_from_nti = AnimationNode::NodeTimeInfo();
795-
fading_pos = 0;
796-
// Init current length.
797-
pi.time = 0;
798-
pi.seeked = true;
799-
pi.is_external_seeking = false;
800-
pi.weight = 0;
801-
current_nti = p_state_machine->blend_node(p_state_machine->states[current].node, current, pi, AnimationNode::FILTER_IGNORE, true, true);
802-
// Don't process first node if not necessary, instead process next node.
803-
_transition_to_next_recursive(tree, p_state_machine, p_delta, p_test_only);
790+
// // Clear fadeing on teleport.
791+
// fading_from = StringName();
792+
// fadeing_from_nti = AnimationNode::NodeTimeInfo();
793+
// fading_pos = 0;
794+
// // Init current length.
795+
// pi.time = 0;
796+
// pi.seeked = true;
797+
// pi.is_external_seeking = false;
798+
// pi.weight = 0;
799+
// current_nti = p_state_machine->blend_node(p_state_machine->states[current].node, current, pi, AnimationNode::FILTER_IGNORE, true, true);
800+
// // Don't process first node if not necessary, instead process next node.
801+
// _transition_to_next_recursive(tree, p_state_machine, p_delta, p_test_only);
802+
803+
// path.push_back(travel_request);
804804
}
805805

806806
// Check current node existence.
@@ -1058,8 +1058,17 @@ AnimationNodeStateMachinePlayback::NextInfo AnimationNodeStateMachinePlayback::_
10581058
next.switch_mode = ref_transition->get_switch_mode();
10591059
next.is_reset = ref_transition->is_reset();
10601060
next.break_loop_at_end = ref_transition->is_loop_broken_at_end();
1061+
return next; // Once we have the information we need, we can actually just return that info
10611062
}
10621063
}
1064+
// There is no transition that ends at the next state in the state machine. We can use the default transition
1065+
next.node = path[0];
1066+
next.xfade = p_state_machine->default_transition->get_xfade_time();
1067+
next.curve = p_state_machine->default_transition->get_xfade_curve();
1068+
next.switch_mode = p_state_machine->default_transition->get_switch_mode();
1069+
next.is_reset = p_state_machine->default_transition->is_reset();
1070+
next.break_loop_at_end = p_state_machine->default_transition->is_loop_broken_at_end();
1071+
return next; // Once we have the information we need, we can actually just return that info
10631072
} else {
10641073
int auto_advance_to = -1;
10651074
float priority_best = 1e20;
@@ -1198,11 +1207,6 @@ void AnimationNodeStateMachinePlayback::_bind_methods() {
11981207

11991208
AnimationNodeStateMachinePlayback::AnimationNodeStateMachinePlayback() {
12001209
set_local_to_scene(true); // Only one per instantiated scene.
1201-
default_transition.instantiate();
1202-
default_transition->set_xfade_time(0);
1203-
default_transition->set_reset(true);
1204-
default_transition->set_advance_mode(AnimationNodeStateMachineTransition::ADVANCE_MODE_AUTO);
1205-
default_transition->set_switch_mode(AnimationNodeStateMachineTransition::SWITCH_MODE_IMMEDIATE);
12061210
}
12071211

12081212
///////////////////////////////////////////////////////
@@ -1320,6 +1324,18 @@ bool AnimationNodeStateMachine::are_ends_reset() const {
13201324
return reset_ends;
13211325
}
13221326

1327+
void AnimationNodeStateMachine::set_default_transition(Ref<AnimationNodeStateMachineTransition> p_transition) {
1328+
default_transition.instantiate();
1329+
default_transition->set_xfade_time(0);
1330+
default_transition->set_reset(true);
1331+
default_transition->set_advance_mode(AnimationNodeStateMachineTransition::ADVANCE_MODE_AUTO);
1332+
default_transition->set_switch_mode(AnimationNodeStateMachineTransition::SWITCH_MODE_IMMEDIATE);
1333+
}
1334+
1335+
Ref<AnimationNodeStateMachineTransition> AnimationNodeStateMachine::get_default_transition() {
1336+
return default_transition;
1337+
}
1338+
13231339
bool AnimationNodeStateMachine::can_edit_node(const StringName &p_name) const {
13241340
if (states.has(p_name)) {
13251341
const AnimationNode *anode = states[p_name].node.ptr();
@@ -1789,6 +1805,9 @@ void AnimationNodeStateMachine::get_argument_options(const StringName &p_functio
17891805
#endif
17901806

17911807
void AnimationNodeStateMachine::_bind_methods() {
1808+
ClassDB::bind_method(D_METHOD("set_default_transition", "transition"), &AnimationNodeStateMachine::set_default_transition);
1809+
ClassDB::bind_method(D_METHOD("get_default_transition"), &AnimationNodeStateMachine::get_default_transition);
1810+
17921811
ClassDB::bind_method(D_METHOD("add_node", "name", "node", "position"), &AnimationNodeStateMachine::add_node, DEFVAL(Vector2()));
17931812
ClassDB::bind_method(D_METHOD("replace_node", "name", "node"), &AnimationNodeStateMachine::replace_node);
17941813
ClassDB::bind_method(D_METHOD("get_node", "name"), &AnimationNodeStateMachine::get_node);
@@ -1824,6 +1843,7 @@ void AnimationNodeStateMachine::_bind_methods() {
18241843
ADD_PROPERTY(PropertyInfo(Variant::INT, "state_machine_type", PROPERTY_HINT_ENUM, "Root,Nested,Grouped"), "set_state_machine_type", "get_state_machine_type");
18251844
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_transition_to_self"), "set_allow_transition_to_self", "is_allow_transition_to_self");
18261845
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "reset_ends"), "set_reset_ends", "are_ends_reset");
1846+
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "default_transition", PROPERTY_HINT_RESOURCE_TYPE, ""), "set_default_transition", "get_default_transition");
18271847

18281848
BIND_ENUM_CONSTANT(STATE_MACHINE_TYPE_ROOT);
18291849
BIND_ENUM_CONSTANT(STATE_MACHINE_TYPE_NESTED);

scene/animation/animation_node_state_machine.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ class AnimationNodeStateMachine : public AnimationRootNode {
136136
Ref<AnimationNodeStateMachineTransition> transition;
137137
};
138138

139+
Ref<AnimationNodeStateMachineTransition> default_transition;
139140
Vector<Transition> transitions;
140141

141142
StringName playback = "playback";
@@ -206,6 +207,9 @@ class AnimationNodeStateMachine : public AnimationRootNode {
206207
void set_reset_ends(bool p_enable);
207208
bool are_ends_reset() const;
208209

210+
void set_default_transition(Ref<AnimationNodeStateMachineTransition> p_transition);
211+
Ref<AnimationNodeStateMachineTransition> get_default_transition();
212+
209213
bool can_edit_node(const StringName &p_name) const;
210214

211215
void set_graph_offset(const Vector2 &p_offset);
@@ -259,7 +263,6 @@ class AnimationNodeStateMachinePlayback : public Resource {
259263
bool is_reset = false;
260264
};
261265

262-
Ref<AnimationNodeStateMachineTransition> default_transition;
263266
String base_path;
264267

265268
AnimationNode::NodeTimeInfo current_nti;

0 commit comments

Comments
 (0)