GDScript Examples

Instantiate Nodes

      
#in header
var sound_object_scene = preload("res://SoundObject/sound_object.tscn")

#in creation code function
var sound_obj = sound_object_scene.instantiate()
dest.add_child(sound_obj)

    

Mouse Input

      func _input(event):
	# Mouse in viewport coordinates.
	if event is InputEventMouseButton:
		print("Mouse Click/Unclick at: ", event.position)
	elif event is InputEventMouseMotion:
		print("Mouse Motion at: ", event.position)

	# Print the size of the viewport.
	print("Viewport Resolution is: ", get_viewport().get_visible_rect().size)
    
From a Godot Tutorial.

Keyboard Input

func _input(event) -> void :
	if event.is_action_pressed("camera"):
		swap_cameras()
      
    

Signals

#declare in script heading
signal pick_up(object, collect)

#emit the signal in a function
pick_up.emit(self, collect)


#create function to handle signal
##Called when the sound object is picked up
func _on_object_pickup(object, collect):
	#wait until we are done handling the signal to make the scene change
	if collect:
             call_deferred("remove_and_check_done", object)

#call defered by _on_object_pickuo so the signal can finish before the object is destroyed
func remove_and_check_done(object):
		object.queue_free()

#connect from some other object
sound_obj.pick_up.connect(_on_object_pickup)