Made an Array[float] constructor! ...now how the hell do I do that in CS

Godot Version

v4.4

Question

func farray(info)->Array[float]:
	var size:int
	var only_one_val:=false
	match typeof(info):
		TYPE_FLOAT, TYPE_INT, TYPE_BOOL:
			only_one_val=true
		TYPE_VECTOR2, TYPE_VECTOR2I, TYPE_PACKED_VECTOR2_ARRAY:
			size=2
		TYPE_VECTOR3, TYPE_VECTOR3I, TYPE_PACKED_VECTOR3_ARRAY:
			size=3
		TYPE_VECTOR4, TYPE_VECTOR4I, TYPE_PACKED_VECTOR4_ARRAY, TYPE_QUATERNION:
			size=4
		TYPE_ARRAY, TYPE_PACKED_BYTE_ARRAY, TYPE_PACKED_COLOR_ARRAY,\
		TYPE_PACKED_FLOAT64_ARRAY, TYPE_PACKED_INT64_ARRAY,\
		TYPE_PACKED_FLOAT32_ARRAY, TYPE_PACKED_INT32_ARRAY:
			size=info.size()
		_: push_warning("No compatible type found for float_Array() input");return []
	var array:Array[float]
	if only_one_val: array.append(float(info))
	else: for axis in range(size):array.append(float(info[axis]))
	return array

great
now C# it

public static Godot.Collections.Array float_Array(Variant info){
	int size=2;
	bool only_one_val=true;
	GD.Print((String)(Variant.VariantType)info);
	switch((Variant.VariantType)info){
		case 3:// TYPE_FLOAT
		case 2:// TYPE_INT
		case 1:// TYPE_BOOL
			only_one_val=true;break;
		case 5:// TYPE_VECTOR2
		case 6:// TYPE_VECTOR2I
		case 35:// TYPE_PACKED_VECTOR2_ARRAY
			size=2;break;
		case 9:// TYPE_VECTOR3
		case 10:// TYPE_VECTOR3I
		case 36:// TYPE_PACKED_VECTOR3_ARRAY
			size=3;break;
		case 12:// TYPE_VECTOR4
		case 13:// TYPE_VECTOR4I
		case 38:// TYPE_PACKED_VECTOR4_ARRAY
		case 15:// TYPE_QUATERNION
			size=4;break;
		case 28:// TYPE_ARRAY
		case 29:// TYPE_PACKED_BYTE_ARRAY
		case 37:// TYPE_PACKED_COLOR_ARRAY
		case 33:// TYPE_PACKED_FLOAT64_ARRAY
		case 31:// TYPE_PACKED_INT64_ARRAY
		case 32:// TYPE_PACKED_FLOAT32_ARRAY
		case 30:// TYPE_PACKED_INT32_ARRAY
			size=info.Count();break;
		_:
			GD.PushWarning("No compatible type found for float_Array() input");return [];
	}
	var array=new Godot.Collections.Array();
	if only_one_val {array.Add((float)info);}
	else {foreach (int axis in range(size)){array.Add((float)info[axis]);}}
	return array;
}

riddled with holes
If you have any input on it that’d be great

Hi,

May I ask what are you trying to do?
I tried implementing your function in C# and I’m struggling because I don’t even understand the point of your code. Anyway, since you’re returning an array made of float values, maybe that would work?

public static float[] float_Array(Variant info) {
    return info.AsFloat32Array();
}

Note that this returns a C# array instead of a Godot array, but that should not be that much of a deal.

It’s a constructor, like how int(true) is equal to 1. It infers a variable of one type from a variable of another. Godot doesn’t have an array constructor, presumably because there are just too many things you can store in an array for it to make sense, so I made one for just making float-typed arrays

also I fucked up wait a sec
fixed

Well, in that case, I believe you have to handle each type manually, as you indeed cannot cast anything to a float like this. Seems like a bummer, but at least, once such a method is implemented and works fine, you should not have to worry about it anymore.

Would something like this work for you?

switch (info.VariantType)
{
	case Variant.Type.Bool:   return [info.AsBool() ? 1f : 0f];
	case Variant.Type.Int:    return [info.AsInt64()];
	case Variant.Type.Float:  return [(float)info.AsDouble()];
	case Variant.Type.String: return [float.Parse(info.AsString())];
	
	case Variant.Type.Vector2:
		Vector2 v2 = info.AsVector2();
        return [v2.X, v2.Y];
	
    case Variant.Type.Vector2I:
		Vector2 v2i = info.AsVector2I();
		return [v2i.X, v2i.Y];
    
    // TODO: Insert any other handled cases here
	
	default:
		GD.PushWarning($"Invalid type {info.VariantType} found for float_Array() input");
		return [];
}
1 Like