Skip to content

tutorial_gdscript_efficiently

reduz edited this page Sep 20, 2014 · 23 revisions

Using GDScript Efficiently

About

This tutorial aims to be a quick reference for how to use GDScript more efficiently. It focuses in common cases specific to the language, but also covers a lot related to using dynamically typed languages.

Dynamic Nature

Pros & Cons of Dynamic Typing

GDScript is a Dynamically Typed language. As such, it's main advantages are that:

  • Language is very simple to learn.
  • Most code can be written and changed quickly and without hassle.
  • Less code written means less errors & mistakes to fix.
  • Easier to read the code (less clutter).
  • No compilation is required to test.
  • Run-Time is tiny.
  • Duck-Typing and Polymorphism by nature.

While the main cons are:

  • Less performance than statically typed languages.
  • More difficult to refactor (symbols can't be traced)
  • Some errors only appear while running the code.
  • Less flexibility for code-completion (some values can only known at run-time).

This, translated to reality, means that Godot+GDScript are a combination designed to games very quickly and efficiently. For games that are very computationally intensive and can't benefit from the engine built-in tools (such as the Vector types, Physics Engine, Math library, etc), the possibility of using C++ is present too. This allows to still create the entire game in GDScript and add small bits of C++ in the areas that need a boost.

Variables & Assignment

All variables in a dynamicaly typed language are "variant"-like. This means that their type is not fixed, and is only modified through assignment. Example:

Static:

int a; // value uninitialized
a=5; // this is valid
a="Hi!" // this is invalid

Dynamic:

var a # null by default
a=5 # valid, 'a' becomes an integer
a="Hi!" # valid, 'a' changed to a string

Clone this wiki locally