Skip to content

Instantly share code, notes, and snippets.

View brettchalupa's full-sized avatar

Brett Chalupa brettchalupa

View GitHub Profile
@brettchalupa
brettchalupa / level_1.gd
Created April 12, 2023 12:25
Scene Switching in Godot 4
# How to switch to a PackedScene
extends Node2D
@export var level_2_scene:PackedScene
func _on_level_2_button_pressed() -> void:
get_tree().change_scene_to_packed(level_2_scene)
@brettchalupa
brettchalupa / main.gd
Created April 11, 2023 13:22
Godot 4 Frames Per Second Explained
extends Node2D
const FPS_DICTIONARY = {
0: 0,
1: 60,
2: 30,
}
func _ready() -> void:
set_max_fps_text()
@brettchalupa
brettchalupa / main.gd
Created April 10, 2023 13:23
GDScript in Godot 4 @onready var Explained
extends Node2D
const MAX_HEALTH = 5
var health = MAX_HEALTH
@onready var health_bar = $HealthBar
@onready var health_label = $HealthLabel
func _ready() -> void:
update_health_ui()
health_bar.max_value = MAX_HEALTH
@brettchalupa
brettchalupa / main.gd
Created April 10, 2023 13:22
Godot 4 Health Bars
extends Node2D
const MAX_HEALTH = 5
var health = MAX_HEALTH
func _ready() -> void:
update_health_ui()
$HealthBar.max_value = MAX_HEALTH
func update_health_ui():
@brettchalupa
brettchalupa / player.gd
Created April 6, 2023 14:13
Using Enums with Godot
extends CharacterBody2D
class_name Player
@export var weapon:PackedScene
const BULLET_SPEED = 400
@export var fire_type := FireType.SINGLE
enum FireType {
@brettchalupa
brettchalupa / main.gd
Created April 4, 2023 13:26
Playing Music and Sound Effects in Godot 4
extends Node2D
@onready var pickup_sfx:AudioStreamPlayer = $PickupSfx
@onready var music:AudioStreamPlayer = $Music
func _on_play_sfx_pressed() -> void:
pickup_sfx.play()
var music_pos = 0.0
@brettchalupa
brettchalupa / main.gd
Last active November 23, 2023 12:23
Godot Saving and Loading Data from Disk
extends Node2D
var times_pressed := 0
var high_score := 0
var playing := false
@onready var timer:Timer = $Gameplay/Timer
func _ready() -> void:
load_high_score()
@brettchalupa
brettchalupa / main.gd
Created April 3, 2023 14:04
Godot Timers Explained
extends Node2D
func _process(delta: float) -> void:
$TimeRemaining.text = "%s" % roundf($FlashTimer.time_left)
func _on_flash_timer_timeout() -> void:
toggle_icon_visibility()
func toggle_icon_visibility():
if $Icon.visible:
@brettchalupa
brettchalupa / icon.gd
Created April 3, 2023 13:37
Signals in Godot 4
extends Sprite2D
var rotating = true
signal rotating_toggled(is_rotating:bool)
func _ready() -> void:
rotating_toggled.emit(rotating)
func _process(delta: float) -> void:
if rotating:
@brettchalupa
brettchalupa / greeting.txt
Created April 3, 2023 13:14
Godot Read from Disk
Hello, world! I'm from an external file.
And I'm a new line!