Is this like a var in a function?

Godot Version

Godot 4

Question

how does this part of the function works?
Screenshot 2024-03-07 164117
And what is it’s purpose?

pos in this case is the function’s parameter, which is a temporary variable. Its initial value is determined in the function call. If you call start(Vector2(1, 2)), then the value of pos will be Vector2(1, 2).

1 Like

ok… Know I did not quite understand what you mean by Its initial value is determined in the function call. If you call start(Vector2(1, 2)) , then the value of pos will be Vector2(1, 2) .


func test_function(pos):
     print(pos)

func _ready():
    test_function("String Number One")
    test_function("String Number Two")

What the code above does is it references the function and sends in a different value.
The Debug output would look like:

String Number One
String Number two

Hope that awnsers your question.

1 Like

you can also specify a type for the function parameter and a default value

extends Node2D

func my_string(a_value : String):
	print (a_value)
func my_string_with_default(a_value : String = "some default value"):
	print(a_value)

func _ready():
		
		#below passes a number, doesn't work
		#my_string(1)
		
		#this works
		my_string("Hello, world!")
		
		my_string_with_default() # no string provided
		# default value used

func my_string_with_default(a_value : Integer = -1):

This gives me an error: “Could not find type Integer in current scene”
It also errors referencing “Int” instead of “Integer”
However, I can still give a default value just fine.

func my_string_with_default(a_value = -1):

Oddly, it works ok with String. Is there a list of type names?

this page is one of the results after i searched for: gdscript variable types"

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.