Giter Site home page Giter Site logo

godot-snippets's Introduction

godot-snippets

A list of useful Godot snippets.

Basics

Instancing a scene by code

var scene_file = preload("res://path/to/scene_file.tscn")
var scene_instance = scene_file.instance()
add_child(scene_instance)

Play some music

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

func print_debug(text):
	if OS.is_debug_build():
		print(text)

Printing to console with a timestamp

# 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)

func _ready():
	print(tr("HELLO_WORLD"))
	print(tr("COLLECT_N_APPLES").replace("%s", str(apples)))

Advanced

Game options management using an .ini file

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.