Get a string list of animations C#?

Godot Version

v4.3.beta2.mono.official [b75f0485b]

Question

So yeah, I want to fill a List<string> with all the animation names from AnimationPlayer on _Ready() but want the items to be System.String, not Godot.StringName.
Does anyone know a concise conversion from List<StringName> to List<string>?

You can use ToString() from StringName.
Also, GetQueue() method from AnimationPlayer, returns a string array of the animation keys that are currently queued to play.

see below for examples. Example 1: you can assign a string using StringName. I don’t think ToString() is needed. Example 2: if you leave the list as List you can still iterate each item as a string. Example: how to convert from List to List. might be a better way to do it, my search was: “c# convert list int to list string”

using Godot;
using System.Collections.Generic;
using System.Linq;

public partial class MyTest:Node2D {
	public MyTest() {
		/*

		Example 1

		*/

		GD.Print("----------- v_string_name -> v_string ----------");

		StringName v_string_name = "Hello,world!";
		string v_string = v_string_name;


		GD.Print("v_string_name:", v_string_name, ", v_string", v_string);

		/*

		Example 2

		*/

		GD.Print("----------- v_list_string_name ----------");

		List<StringName> v_list_string_name = new List<StringName>();
		v_list_string_name.Add(new StringName("Hello, world!"));
		v_list_string_name.Add(new StringName("1"));
		v_list_string_name.Add(new StringName("A"));



		foreach(string j_string in v_list_string_name) {
			GD.Print(j_string);
		}


		GD.Print("----------- v_list_string ----------");


		/*

		Example 3

		*/


		// source:   https://www.techiedelight.com/convert-list-of-int-to-list-of-string-in-csharp/
		List<string> v_list_string = v_list_string_name.ConvertAll<string>(_a_string_name => _a_string_name);

		GD.Print(" output:");
		foreach(string j_string in v_list_string) {
			GD.Print(j_string);
		}
	}
}


1 Like

Thx for your time, guys.

I’ve misread (StringName — Godot Engine (stable) documentation in English) - StringName does not always need explicit conversion to System.String. They meant that the methods on the page only work with System.String, not that the types are not interchangable by any circumstance.
My loop looks like this:

foreach (string str in Player.GetAnimationList()) {
    animations.Add(str); }

…, where animations is a local List<string>, works just fine. I did not have to explicitly convert GetAnimationList() items.

Sorry for bothering!

1 Like

No need to be sorry, welcome to the forums!

1 Like