Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typeofname() to get a name of a type rather than its enumerated value #1860

Closed
Feniks-Gaming opened this issue Nov 19, 2020 · 2 comments · Fixed by godotengine/godot#69624
Milestone

Comments

@Feniks-Gaming
Copy link

Describe the project you are working on:

Any project will do currently tic tac toe game.

Describe the problem or limitation you are having in your project:

if you want to find out what type variable you have you can use typeof() method but it returns enum number.

var my_bool: bool = true
print(typeof(my_bool) # prints 1

Describe the feature / enhancement and how it helps to overcome the problem or limitation:

I would like to be able to do this instead

var my_bool: bool = true
print(typeofname(my_bool) # prints "bool"

Describe how your proposal will work, with code, pseudocode, mockups, and/or diagrams:

No idea how it would work with code in c++

If this enhancement will not be used often, can it be worked around with a few lines of script?:

Used fairly often and likely there is a work around but in my opinion 99% when user uses typeof() they want to know a name of type not it enumerated value

Is there a reason why this should be core and not an add-on in the asset library?:

Adds function to core functionality.

@Calinou
Copy link
Member

Calinou commented Nov 19, 2020

Related to #837.

@Calinou Calinou changed the title Add typeofname() to GDScript to get a name of a type rather than it's enumerated value Add typeofname() to get a name of a type rather than its enumerated value Nov 19, 2020
@Error7Studios
Copy link

Also, adding a constant for each type name will allow us to use them in match statements.

const TYPENAME_NIL := "Null"
const TYPENAME_BOOL := "bool"
const TYPENAME_INT := "int"
const TYPENAME_REAL := "float"
const TYPENAME_STRING := "String"
const TYPENAME_VECTOR2 := "Vector2"
const TYPENAME_RECT2 := "Rect2"
const TYPENAME_VECTOR3 := "Vector3"
const TYPENAME_TRANSFORM2D := "Transform2D"
const TYPENAME_PLANE := "Plane"
const TYPENAME_QUAT := "Quat"
const TYPENAME_AABB := "AABB"
const TYPENAME_BASIS := "Basis"
const TYPENAME_TRANSFORM := "Transform"
const TYPENAME_COLOR := "Color"
const TYPENAME_NODE_PATH := "NodePath"
const TYPENAME_RID := "RID"
const TYPENAME_OBJECT := "Object"
const TYPENAME_DICTIONARY := "Dictionary"
const TYPENAME_ARRAY := "Array"
const TYPENAME_RAW_ARRAY := "PoolByteArray"
const TYPENAME_INT_ARRAY := "PoolIntArray"
const TYPENAME_REAL_ARRAY := "PoolRealArray"
const TYPENAME_STRING_ARRAY := "PoolStringArray"
const TYPENAME_VECTOR2_ARRAY := "PoolVector2Array"
const TYPENAME_VECTOR3_ARRAY := "PoolVector3Array"
const TYPENAME_COLOR_ARRAY := "PoolColorArray"
const TYPENAME_MAX := "Max"


const _TYPENAME := {
	TYPE_NIL: TYPENAME_NIL,
	TYPE_BOOL: TYPENAME_BOOL,
	TYPE_INT: TYPENAME_INT,
	TYPE_REAL: TYPENAME_REAL,
	TYPE_STRING: TYPENAME_STRING,
	TYPE_VECTOR2: TYPENAME_VECTOR2,
	TYPE_RECT2: TYPENAME_RECT2,
	TYPE_VECTOR3: TYPENAME_VECTOR3,
	TYPE_TRANSFORM2D: TYPENAME_TRANSFORM2D,
	TYPE_PLANE: TYPENAME_PLANE,
	TYPE_QUAT: TYPENAME_QUAT,
	TYPE_AABB: TYPENAME_AABB,
	TYPE_BASIS: TYPENAME_BASIS,
	TYPE_TRANSFORM: TYPENAME_TRANSFORM,
	TYPE_COLOR: TYPENAME_COLOR,
	TYPE_NODE_PATH: TYPENAME_NODE_PATH,
	TYPE_RID: TYPENAME_RID,
	TYPE_OBJECT: TYPENAME_OBJECT,
	TYPE_DICTIONARY: TYPENAME_DICTIONARY,
	TYPE_ARRAY: TYPENAME_ARRAY,
	TYPE_RAW_ARRAY: TYPENAME_RAW_ARRAY,
	TYPE_INT_ARRAY: TYPENAME_INT_ARRAY,
	TYPE_REAL_ARRAY: TYPENAME_REAL_ARRAY,
	TYPE_STRING_ARRAY: TYPENAME_STRING_ARRAY,
	TYPE_VECTOR2_ARRAY: TYPENAME_VECTOR2_ARRAY,
	TYPE_VECTOR3_ARRAY: TYPENAME_VECTOR3_ARRAY,
	TYPE_COLOR_ARRAY: TYPENAME_COLOR_ARRAY,
	TYPE_MAX: TYPENAME_MAX,
}


func typename(__var) -> String:
	return _TYPENAME[typeof(__var)]


func print_var(__var) -> void:
	var type_name: String = typename(__var)
	print("\n", type_name, ":")

	match type_name:
		TYPENAME_ARRAY:
			print("[")
			for element in __var:
				print("\t", element, ",")
			print("]")
		TYPENAME_DICTIONARY:
			print("{")
			for i in __var.size():
				print("\t", __var.keys()[i], ": ", __var.values()[i], ",")
			print("}")
		_:
			print(__var)
	pass


func _ready():
	var dict := {"A": 1, "B": 2, "C": 3}
	var array := ["one", "two", "three"]
	print_var(dict)
	print_var(array)
	print_var(Color.black)
	print_var(Quat(Vector3.ZERO))

prints:

Dictionary:
{
	A: 1,
	B: 2,
	C: 3,
}

Array:
[
	one,
	two,
	three,
]

Color:
0,0,0,1

Quat:
(0, 0, 0, 1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants