https://github.com/calinou/godot-snippets
Code snippets for use with the Godot Engine
https://github.com/calinou/godot-snippets
Last synced: 12 months ago
JSON representation
Code snippets for use with the Godot Engine
- Host: GitHub
- URL: https://github.com/calinou/godot-snippets
- Owner: Calinou
- License: cc0-1.0
- Created: 2016-01-11T20:22:24.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2016-01-11T20:54:56.000Z (over 10 years ago)
- Last Synced: 2025-03-29T12:34:44.427Z (over 1 year ago)
- Homepage: http://godotengine.org
- Size: 2.93 KB
- Stars: 21
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# godot-snippets
**A list of useful Godot snippets.**
## Basics
### Instancing a scene by code
```gdscript
var scene_file = preload("res://path/to/scene_file.tscn")
var scene_instance = scene_file.instance()
add_child(scene_instance)
```
### Play some music
```gdscript
func play(music):
var stream = load("res://" + str(music) + ".opus")
get_node("StreamPlayer").set_stream(stream)
get_node("StreamPlayer").play()
```
### Printing only when running from editor, or debug-mode exports
```gdscript
func print_debug(text):
if OS.is_debug_build():
print(text)
```
### Printing to console with a timestamp
```gdscript
# YYYY-MM-DD
func get_date():
return str(OS.get_date()["year"]) + "-" + str(OS.get_date()["month"]).pad_zeros(2) + "-" + str(OS.get_date()["day"]).pad_zeros(2)
# hh:mm:ss
func get_time():
return str(OS.get_time()["hour"]).pad_zeros(2) + ":" + str(OS.get_time()["minute"]).pad_zeros(2) + ":" + str(OS.get_time()["second"]).pad_zeros(2)
# [YYYY-MM-DD | hh:mm:ss]
func print_timestamp(text):
print("[" + get_date() + " | " + get_time() + "] " + text)
```
### Make a string translatable (with possible replacement)
```gdscript
func _ready():
print(tr("HELLO_WORLD"))
print(tr("COLLECT_N_APPLES").replace("%s", str(apples)))
```
## Advanced
### Game options management using an .ini file
```gdscript
func get_option(section, key, default):
var config = ConfigFile.new()
config.load("user://config.ini")
var value = config.get_value(section, key, default)
return value
func set_option(section, key, value):
var config = ConfigFile.new()
config.load("user://config.ini")
config.set_value(section, key, value)
config.save("user://config.ini")
```
## License
Copyright (c) 2016 Calinou and contributors
This file and all snippets included are under CC0 1.0 Universal. See `LICENSE.md` for more information.