Godot Notes
Most important note to self: Pay close attention to that feeling of “WTF am I thinking here?!” when reading code. You have just found code that’s hard to read. Improving this code is an excellent lesson in writing clear code.
List of common things you want to be able to do
This is going to be very chaotic for the first few months. You have been warned =)
Declaring variables (with type)
var variable_name : type = value
var length : int = 10
For loop
for n in 8:
print(n);
Get mouse position in Viewport
get_viewport().get_mouse_position();
Expose a variable in the editor
@export var string_variable : String;
Expose a multi-line string in the editor
@export (String,MULTILINE) var string_variable : String;s
Spawn an object
var Wall = preload("res://Prefabs/Wall.tscn")
func _ready() -> void:
var instance = Wall.instantiate()
add_child(instance)
Note: without add_child it doesn’t work
Mouse Button Down (simplest version)
if Input.is_mouse_button_pressed( 1 )
Note: 1 is LEFT mouse button
Tweens
var tween = create_tween()
tween.set_ease(Tween.EASE_IN)
tween.set_parallel(true) #allows two tweens to work at the same time eg. animating X and Y position axis separately.
tween.tween_property(player,"position:x",position.x,duration)
“player” is the object whose properties you want to animate. “position:x” is the property. Note that you can animate each axis of a vector separately “duration” is the duration of the animation
Tilemap things
Place tile on tilemap
$TileMap.set_cell(layerid, position_in_tilemap, sourceid, atlas_coordinates)
- layerid is the id of the layer you want to draw in.
- position_in_tilemap is a vector2i defining the position where to draw the tile.
- sourceid is the id of the tileset texture you want to use.
- atlas_coordinates is a vector2i defining the position in the tileset texture you need to draw
Code fragments from various projects
Drawing Tiles on a Tilemap with the Mouse
mouse_position = viewport.get_mouse_position();
modulo_overflow_x = (int(mouse_position.x) % tile_size)
modulo_overflow_y = (int(mouse_position.y) % tile_size)
sprite_cursor.position.x = mouse_position.x - modulo_overflow_x+tile_size/2;
sprite_cursor.position.y = mouse_position.y - modulo_overflow_ys+tile_size/2;
if Input.is_mouse_button_pressed( 1 ):
var position = tilemap.local_to_map(tilemap.to_local(mouse_position));
var tile = Vector2i(0,0);
tilemap.set_cell(position, 0, tile);
if Input.is_mouse_button_pressed( 2 ):
var position = tilemap.local_to_map(tilemap.to_local(mouse_position));
var tile = Vector2i(1,0);
tilemap.set_cell(position, 0, tile);