Skip to content

Commit 8276e51

Browse files
committed
Merge pull request #110767 from Ryan-000/Fix-AnimationPlayer-to-use-StringName
Fix AnimationPlayer to use StringName instead of String in the exposed API.
2 parents 72cf639 + 35999a1 commit 8276e51

File tree

7 files changed

+96
-33
lines changed

7 files changed

+96
-33
lines changed

doc/classes/AnimationPlayer.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
</description>
6565
</method>
6666
<method name="get_queue">
67-
<return type="PackedStringArray" />
67+
<return type="StringName[]" />
6868
<description>
6969
Returns a list of the animation keys that are currently queued to play.
7070
</description>
@@ -291,13 +291,13 @@
291291
</method>
292292
</methods>
293293
<members>
294-
<member name="assigned_animation" type="String" setter="set_assigned_animation" getter="get_assigned_animation">
294+
<member name="assigned_animation" type="StringName" setter="set_assigned_animation" getter="get_assigned_animation">
295295
If playing, the current animation's key, otherwise, the animation last played. When set, this changes the animation, but will not play it unless already playing. See also [member current_animation].
296296
</member>
297-
<member name="autoplay" type="String" setter="set_autoplay" getter="get_autoplay" default="&quot;&quot;">
297+
<member name="autoplay" type="StringName" setter="set_autoplay" getter="get_autoplay" default="&amp;&quot;&quot;">
298298
The key of the animation to play when the scene loads.
299299
</member>
300-
<member name="current_animation" type="String" setter="set_current_animation" getter="get_current_animation" default="&quot;&quot;">
300+
<member name="current_animation" type="StringName" setter="set_current_animation" getter="get_current_animation" default="&amp;&quot;&quot;">
301301
The key of the currently playing animation. If no animation is playing, the property's value is an empty string. Changing this value does not restart the animation. See [method play] for more information on playing animations.
302302
[b]Note:[/b] While this property appears in the Inspector, it's not meant to be edited, and it's not saved in the scene. This property is mainly used to get the currently playing animation, and internally for animation playback tracks. For more information, see [Animation].
303303
</member>
@@ -343,7 +343,7 @@
343343
</description>
344344
</signal>
345345
<signal name="current_animation_changed">
346-
<param index="0" name="name" type="String" />
346+
<param index="0" name="name" type="StringName" />
347347
<description>
348348
Emitted when [member current_animation] changes.
349349
</description>

editor/animation/animation_player_editor_plugin.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ void AnimationPlayerEditor::_notification(int p_what) {
113113

114114
if (player->is_playing()) {
115115
{
116-
String animname = player->get_assigned_animation();
116+
StringName animname = player->get_assigned_animation();
117117

118118
if (player->has_animation(animname)) {
119119
Ref<Animation> anim = player->get_animation(animname);
@@ -229,7 +229,7 @@ void AnimationPlayerEditor::_autoplay_pressed() {
229229
if (player->get_autoplay() == current) {
230230
//unset
231231
undo_redo->create_action(TTR("Toggle Autoplay"));
232-
undo_redo->add_do_method(player, "set_autoplay", "");
232+
undo_redo->add_do_method(player, "set_autoplay", StringName());
233233
undo_redo->add_undo_method(player, "set_autoplay", player->get_autoplay());
234234
undo_redo->add_do_method(this, "_animation_player_changed", player);
235235
undo_redo->add_undo_method(this, "_animation_player_changed", player);
@@ -238,7 +238,7 @@ void AnimationPlayerEditor::_autoplay_pressed() {
238238
} else {
239239
//set
240240
undo_redo->create_action(TTR("Toggle Autoplay"));
241-
undo_redo->add_do_method(player, "set_autoplay", current);
241+
undo_redo->add_do_method(player, "set_autoplay", StringName(current));
242242
undo_redo->add_undo_method(player, "set_autoplay", player->get_autoplay());
243243
undo_redo->add_do_method(this, "_animation_player_changed", player);
244244
undo_redo->add_undo_method(this, "_animation_player_changed", player);
@@ -564,8 +564,8 @@ void AnimationPlayerEditor::_animation_remove_confirmed() {
564564
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
565565
undo_redo->create_action(TTR("Remove Animation"));
566566
if (player->get_autoplay() == current) {
567-
undo_redo->add_do_method(player, "set_autoplay", "");
568-
undo_redo->add_undo_method(player, "set_autoplay", current);
567+
undo_redo->add_do_method(player, "set_autoplay", StringName());
568+
undo_redo->add_undo_method(player, "set_autoplay", StringName(current));
569569
// Avoid having the autoplay icon linger around if there is only one animation in the player.
570570
undo_redo->add_do_method(this, "_animation_player_changed", player);
571571
}
@@ -597,7 +597,7 @@ void AnimationPlayerEditor::_select_anim_by_name(const String &p_anim) {
597597
}
598598

599599
float AnimationPlayerEditor::_get_editor_step() const {
600-
const String current = player->get_assigned_animation();
600+
const StringName current = player->get_assigned_animation();
601601
const Ref<Animation> anim = player->get_animation(current);
602602
ERR_FAIL_COND_V(anim.is_null(), 0.0);
603603

@@ -1411,7 +1411,7 @@ void AnimationPlayerEditor::_seek_value_changed(float p_value, bool p_timeline_o
14111411
};
14121412

14131413
updating = true;
1414-
String current = player->get_assigned_animation();
1414+
StringName current = player->get_assigned_animation();
14151415
if (current.is_empty() || !player->has_animation(current)) {
14161416
updating = false;
14171417
current = "";
@@ -1460,7 +1460,7 @@ void AnimationPlayerEditor::_animation_finished(const String &p_name) {
14601460
finishing = true;
14611461
}
14621462

1463-
void AnimationPlayerEditor::_current_animation_changed(const String &p_name) {
1463+
void AnimationPlayerEditor::_current_animation_changed(const StringName &p_name) {
14641464
if (is_visible_in_tree()) {
14651465
if (finishing) {
14661466
finishing = false; // Maybe redundant since it will be false in the AnimationPlayerEditor::_process(), but for safety.

editor/animation/animation_player_editor_plugin.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ class AnimationPlayerEditor : public VBoxContainer {
206206

207207
void _list_changed();
208208
void _animation_finished(const String &p_name);
209-
void _current_animation_changed(const String &p_name);
209+
void _current_animation_changed(const StringName &p_name);
210210
void _update_animation();
211211
void _update_player();
212212
void _set_controls_disabled(bool p_disabled);

misc/extension_api_validation/4.5-stable.expected

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,17 @@ GH-111117
4040
Validate extension JSON: JSON file: Field was added in a way that breaks compatibility 'classes/LineEdit/methods/edit': arguments
4141

4242
Optional argument added. Compatibility method registered.
43+
44+
45+
GH-110767
46+
---------
47+
Validate extension JSON: Error: Field 'classes/AnimationPlayer/methods/get_assigned_animation/return_value': type changed value in new API, from "String" to "StringName".
48+
Validate extension JSON: Error: Field 'classes/AnimationPlayer/methods/get_autoplay/return_value': type changed value in new API, from "String" to "StringName".
49+
Validate extension JSON: Error: Field 'classes/AnimationPlayer/methods/get_current_animation/return_value': type changed value in new API, from "String" to "StringName".
50+
Validate extension JSON: Error: Field 'classes/AnimationPlayer/methods/get_queue/return_value': type changed value in new API, from "PackedStringArray" to "typedarray::StringName".
51+
Validate extension JSON: Error: Field 'classes/AnimationPlayer/methods/set_assigned_animation/arguments/0': type changed value in new API, from "String" to "StringName".
52+
Validate extension JSON: Error: Field 'classes/AnimationPlayer/methods/set_autoplay/arguments/0': type changed value in new API, from "String" to "StringName".
53+
Validate extension JSON: Error: Field 'classes/AnimationPlayer/methods/set_current_animation/arguments/0': type changed value in new API, from "String" to "StringName".
54+
Validate extension JSON: Error: Field 'classes/AnimationPlayer/signals/current_animation_changed/arguments/0': type changed value in new API, from "String" to "StringName".
55+
56+
Return types and parameters changed to StringName to improve performance. Compatibility methods registered; No compatibility system for signal arguments.

scene/animation/animation_player.compat.inc

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,54 @@ void AnimationPlayer::_seek_bind_compat_80813(double p_time, bool p_update) {
5858
seek(p_time, p_update, false);
5959
}
6060

61+
Vector<String> AnimationPlayer::_get_queue_compat_110767() {
62+
Vector<String> queue;
63+
for (const Variant &E : get_queue()) {
64+
queue.push_back(E);
65+
}
66+
return queue;
67+
}
68+
69+
String AnimationPlayer::_get_current_animation_compat_110767() const {
70+
return get_current_animation();
71+
}
72+
73+
void AnimationPlayer::_set_current_animation_compat_110767(const String &p_animation) {
74+
set_current_animation(p_animation);
75+
}
76+
77+
String AnimationPlayer::_get_assigned_animation_compat_110767() const {
78+
return get_assigned_animation();
79+
}
80+
81+
void AnimationPlayer::_set_assigned_animation_compat_110767(const String &p_animation) {
82+
set_assigned_animation(p_animation);
83+
}
84+
85+
String AnimationPlayer::_get_autoplay_compat_110767() const {
86+
return get_autoplay();
87+
}
88+
89+
void AnimationPlayer::_set_autoplay_compat_110767(const String &p_name) {
90+
set_autoplay(p_name);
91+
}
92+
6193
void AnimationPlayer::_bind_compatibility_methods() {
6294
ClassDB::bind_method(D_METHOD("set_process_callback", "mode"), &AnimationPlayer::_set_process_callback_bind_compat_80813);
6395
ClassDB::bind_method(D_METHOD("get_process_callback"), &AnimationPlayer::_get_process_callback_bind_compat_80813);
6496
ClassDB::bind_method(D_METHOD("set_method_call_mode", "mode"), &AnimationPlayer::_set_method_call_mode_bind_compat_80813);
6597
ClassDB::bind_method(D_METHOD("get_method_call_mode"), &AnimationPlayer::_get_method_call_mode_bind_compat_80813);
6698
ClassDB::bind_method(D_METHOD("set_root", "path"), &AnimationPlayer::_set_root_bind_compat_80813);
6799
ClassDB::bind_method(D_METHOD("get_root"), &AnimationPlayer::_get_root_bind_compat_80813);
100+
101+
ClassDB::bind_compatibility_method(D_METHOD("get_queue"), &AnimationPlayer::_get_queue_compat_110767);
102+
ClassDB::bind_compatibility_method(D_METHOD("get_current_animation"), &AnimationPlayer::_get_current_animation_compat_110767);
103+
ClassDB::bind_compatibility_method(D_METHOD("set_current_animation", "animation"), &AnimationPlayer::_set_current_animation_compat_110767);
104+
ClassDB::bind_compatibility_method(D_METHOD("get_assigned_animation"), &AnimationPlayer::_get_assigned_animation_compat_110767);
105+
ClassDB::bind_compatibility_method(D_METHOD("set_assigned_animation", "animation"), &AnimationPlayer::_set_assigned_animation_compat_110767);
106+
ClassDB::bind_compatibility_method(D_METHOD("get_autoplay"), &AnimationPlayer::_get_autoplay_compat_110767);
107+
ClassDB::bind_compatibility_method(D_METHOD("set_autoplay", "name"), &AnimationPlayer::_set_autoplay_compat_110767);
108+
68109
ClassDB::bind_compatibility_method(D_METHOD("seek", "seconds", "update"), &AnimationPlayer::_seek_bind_compat_80813, DEFVAL(false));
69110
BIND_ENUM_CONSTANT(ANIMATION_PROCESS_PHYSICS);
70111
BIND_ENUM_CONSTANT(ANIMATION_PROCESS_IDLE);

scene/animation/animation_player.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,8 @@ void AnimationPlayer::queue(const StringName &p_name) {
365365
}
366366
}
367367

368-
Vector<String> AnimationPlayer::get_queue() {
369-
Vector<String> ret;
368+
TypedArray<StringName> AnimationPlayer::get_queue() {
369+
TypedArray<StringName> ret;
370370
for (const StringName &E : playback_queue) {
371371
ret.push_back(E);
372372
}
@@ -579,8 +579,8 @@ bool AnimationPlayer::is_playing() const {
579579
return playing;
580580
}
581581

582-
void AnimationPlayer::set_current_animation(const String &p_animation) {
583-
if (p_animation == "[stop]" || p_animation.is_empty()) {
582+
void AnimationPlayer::set_current_animation(const StringName &p_animation) {
583+
if (p_animation == SNAME("[stop]") || p_animation.is_empty()) {
584584
stop();
585585
} else if (!is_playing()) {
586586
play(p_animation);
@@ -592,16 +592,16 @@ void AnimationPlayer::set_current_animation(const String &p_animation) {
592592
}
593593
}
594594

595-
String AnimationPlayer::get_current_animation() const {
596-
return (is_playing() ? playback.assigned : "");
595+
StringName AnimationPlayer::get_current_animation() const {
596+
return (is_playing() ? playback.assigned : StringName());
597597
}
598598

599-
void AnimationPlayer::set_assigned_animation(const String &p_animation) {
599+
void AnimationPlayer::set_assigned_animation(const StringName &p_animation) {
600600
if (is_playing()) {
601601
float speed = playback.current.speed_scale;
602602
play(p_animation, -1.0, speed, std::signbit(speed));
603603
} else {
604-
ERR_FAIL_COND_MSG(!animation_set.has(p_animation), vformat("Animation not found: %s.", p_animation));
604+
ERR_FAIL_COND_MSG(!animation_set.has(p_animation), vformat("Animation not found: %s.", p_animation.operator String()));
605605
playback.current.pos = 0;
606606
playback.current.from = &animation_set[p_animation];
607607
playback.current.start_time = -1;
@@ -611,7 +611,7 @@ void AnimationPlayer::set_assigned_animation(const String &p_animation) {
611611
}
612612
}
613613

614-
String AnimationPlayer::get_assigned_animation() const {
614+
StringName AnimationPlayer::get_assigned_animation() const {
615615
return playback.assigned;
616616
}
617617

@@ -745,15 +745,15 @@ bool AnimationPlayer::has_section() const {
745745
return Animation::is_greater_or_equal_approx(playback.current.start_time, 0) || Animation::is_greater_or_equal_approx(playback.current.end_time, 0);
746746
}
747747

748-
void AnimationPlayer::set_autoplay(const String &p_name) {
748+
void AnimationPlayer::set_autoplay(const StringName &p_name) {
749749
if (is_inside_tree() && !Engine::get_singleton()->is_editor_hint()) {
750750
WARN_PRINT("Setting autoplay after the node has been added to the scene has no effect.");
751751
}
752752

753753
autoplay = p_name;
754754
}
755755

756-
String AnimationPlayer::get_autoplay() const {
756+
StringName AnimationPlayer::get_autoplay() const {
757757
return autoplay;
758758
}
759759

@@ -1029,7 +1029,7 @@ void AnimationPlayer::_bind_methods() {
10291029
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "speed_scale", PROPERTY_HINT_RANGE, "-4,4,0.001,or_less,or_greater"), "set_speed_scale", "get_speed_scale");
10301030
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "movie_quit_on_finish"), "set_movie_quit_on_finish_enabled", "is_movie_quit_on_finish_enabled");
10311031

1032-
ADD_SIGNAL(MethodInfo(SNAME("current_animation_changed"), PropertyInfo(Variant::STRING, "name")));
1032+
ADD_SIGNAL(MethodInfo(SNAME("current_animation_changed"), PropertyInfo(Variant::STRING_NAME, "name")));
10331033
ADD_SIGNAL(MethodInfo(SNAME("animation_changed"), PropertyInfo(Variant::STRING_NAME, "old_name"), PropertyInfo(Variant::STRING_NAME, "new_name")));
10341034
}
10351035

scene/animation/animation_player.h

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,14 @@ class AnimationPlayer : public AnimationMixer {
166166
void _play_compat_84906(const StringName &p_name = StringName(), double p_custom_blend = -1, float p_custom_scale = 1.0, bool p_from_end = false);
167167
void _play_backwards_compat_84906(const StringName &p_name = StringName(), double p_custom_blend = -1);
168168

169+
Vector<String> _get_queue_compat_110767();
170+
String _get_current_animation_compat_110767() const;
171+
void _set_current_animation_compat_110767(const String &p_animation);
172+
String _get_assigned_animation_compat_110767() const;
173+
void _set_assigned_animation_compat_110767(const String &p_animation);
174+
String _get_autoplay_compat_110767() const;
175+
void _set_autoplay_compat_110767(const String &p_name);
176+
169177
static void _bind_compatibility_methods();
170178
#endif // DISABLE_DEPRECATED
171179

@@ -200,23 +208,23 @@ class AnimationPlayer : public AnimationMixer {
200208
void play_section_backwards(const StringName &p_name = StringName(), double p_start_time = -1, double p_end_time = -1, double p_custom_blend = -1);
201209
void play_with_capture(const StringName &p_name = StringName(), double p_duration = -1.0, double p_custom_blend = -1, float p_custom_scale = 1.0, bool p_from_end = false, Tween::TransitionType p_trans_type = Tween::TRANS_LINEAR, Tween::EaseType p_ease_type = Tween::EASE_IN);
202210
void queue(const StringName &p_name);
203-
Vector<String> get_queue();
211+
TypedArray<StringName> get_queue();
204212
void clear_queue();
205213
void pause();
206214
void stop(bool p_keep_state = false);
207215
bool is_playing() const;
208-
String get_current_animation() const;
209-
void set_current_animation(const String &p_animation);
210-
String get_assigned_animation() const;
211-
void set_assigned_animation(const String &p_animation);
216+
StringName get_current_animation() const;
217+
void set_current_animation(const StringName &p_animation);
218+
StringName get_assigned_animation() const;
219+
void set_assigned_animation(const StringName &p_animation);
212220
bool is_valid() const;
213221

214222
void set_speed_scale(float p_speed);
215223
float get_speed_scale() const;
216224
float get_playing_speed() const;
217225

218-
void set_autoplay(const String &p_name);
219-
String get_autoplay() const;
226+
void set_autoplay(const StringName &p_name);
227+
StringName get_autoplay() const;
220228

221229
void set_movie_quit_on_finish_enabled(bool p_enabled);
222230
bool is_movie_quit_on_finish_enabled() const;

0 commit comments

Comments
 (0)