What does TypeCast node in Visual Script do?

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

According to Godot Docs, I can use it to cast arguments or other objects to the type I desire. Afterward, I can even drag the object output to get full completion:

enter image description here

And it is also possible to cast to a script, which will allow complete script properties and functions:

enter image description here

However, I can’t understand it at all, I am new to Godot. Can someone please explain some examples use cases of this node? Thanks.

:bust_in_silhouette: Reply From: indigochill

Although this is an old question, I found it while trying to find how to specify the base type to cast to, so I’ll answer the original question as well.

Suppose you have a node that emits an “input_event” signal. This signal will give you a base InputEvent but that doesn’t tell you what kind of input you just received. Maybe the mouse was moved or there was a click or the user entered a keyboard input. By trying to cast that input event to the InputEventMouseButton type, you can test whether the input was a mouse button input and run your code only in that case instead of on all types of input events.

Another case where you might want to cast is if you want to call a function with an argument that you don’t quite have the right type for but you have something that can be cast into the right type. For example, you can safely cast an integer to a float because it effectively just means adding a decimal point and zero on the end of your integer. Now if you had a function that needed a float instead of an integer, you have your float.