Detect if a variable is a STRING or INT

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Godot_Starter

I have a variable var Peter = 0 and then I set this variable to a string or a int and then I want to do something if it is a string and I want to do something if it is an int.

:bust_in_silhouette: Reply From: deaton64

Hi,
Use typeof()

Use this list as a reference.

if typeof(Peter) == TYPE_STRING:
	print("I'm a string")
elif typeof(Peter) == TYPE_INT:
	print("I'm an int")
:bust_in_silhouette: Reply From: frankiezafe

Another solution is to use the is logical operator:

if Peter is String:
    print("I'm a string")
elif Peter is int:
    print("I'm a integer")
elif Peter is Spatial:
    print("I'm a spatial")
elif Peter is Resource:
    print("I'm a resource")

and so on.

A good practice is to type your variable, this will avoid type testing and will increase performance of your code:

var Peter:int = 0

The editor will complain if you write further:

Peter = "my best friend"