DEV Community

Engels Garcia
Engels Garcia

Posted on

Moving into the niche! From JavaScript to GDScript

If you already know JavaScript, you’ve most likely experienced the world of web development and are familiar with many programming concepts and best practices. Now if you’re here that means you want to dive into game development. As per the title, I’ll be telling you about this language called GDScript, it is used for scripting in the Godot game engine. Whether you’re looking to develop 2D or 3D games, GDScript is an accessible and powerful tool to make it easy to get into game development.

Why Should I Learn GDScript?

First of all, let me tell you what GDScript is. GDScript is a high-level, dynamically typed programming language designed specifically for Godot, an open-source and feature-rich game engine. The language is optimized for rapid game development and is tightly integrated into the Godot environment. As a JavaScript developer, GDScript might feel familiar in some ways, especially because both languages share an easy-to-understand syntax. This makes GDScript a great choice for transitioning into game development.

Now let me give you some reasons to learn this language

  • Specific for Game Development: GDScript is tailor-made for developing games and lets you access Godot’s most powerful features such as scene management, physics, etc. all using simple and readable code.
  • Beginner Friendly: this point is self-explanatory. Godot is incredibly beginner-friendly and pretty fast to pick up.
  • Popularity: Godot’s popularity has been growing rapidly. It’s known for being free and open-source with a strong community of developers and contributors.

Syntax Differences: GDScript vs. JavaScript

While GDScript shares several similarities with JavaScript, it has its unique features that cater specifically to game development. Let’s compare key parts of the syntax:

1. Scripting for Nodes

Godot keeps information in elements called scenes, which have a tree-like structure of elements called nodes, which can also be scenes themselves. GDScript files are created and attached to these nodes. All files have the extends keyword at the very first line, which defines the class this script inherits or extends. In this example, it's Sprite2D, meaning our script will get access to all the properties and child nodes of the Sprite2D node.

extends Sprite2D
Enter fullscreen mode Exit fullscreen mode

2. Syntax Differences

Before we move onto more GDScript code, let’s briefly look at the most notable differences in syntax between GDScript and JavaScript focusing on GDScript:

  • For the very basics, variables are declared with the var keyword with no const or let variations. Logging into the console is done using the print keyword as opposed to the console.log() function. Finally, comments are made using the syntax element # instead of //.
var greet = "hi"
print(greet) # prints "hi"
Enter fullscreen mode Exit fullscreen mode
const greet = "hi";
console.log(greet); // logs "hi"
Enter fullscreen mode Exit fullscreen mode
  • Functions in GDScript are declared using the func keyword, shortening the common function declaration with the function keyword or arrow function syntax.
func greet():
  print("hi")
Enter fullscreen mode Exit fullscreen mode
function greet() {
  console.log("hi");
}
// or
const greet = () => {
  console.log("hi");
}
Enter fullscreen mode Exit fullscreen mode
  • Conditionals are more similar to Python in that they do not use braces but indentation instead.
if true:
  print(true)
elif 5 > 10:
  print("this will never run")
else:
  print(false)
Enter fullscreen mode Exit fullscreen mode
if (true) {
  console.log(true);
} else if (5 > 10) {
  console.log("this will never run");
} else {
  console.log(false);
}
Enter fullscreen mode Exit fullscreen mode
  • GDScript uses for with ranges instead of traditional for or for of.
# count from 0 to 4
for i in range(5):
  print(i)
Enter fullscreen mode Exit fullscreen mode
// count from 0 to 4
for (let i = 0; i < 5; i++) {
  console.log(i);
}
Enter fullscreen mode Exit fullscreen mode

3. Starting Functions

Every GDScript files also have 2 functions upon creation: _ready and _process. Starting with the _ready function, it runs as soon as the node the file is attached to gets rendered on the scene. On the other hand, the _process function runs every frame and has a parameter called delta which is the amount of time the previous frame took to complete.

extends Node

func _ready() -> void:
  pass

func _process(delta: float) -> void:
  pass
Enter fullscreen mode Exit fullscreen mode

Now, several things are happening in this script. First, the script is attached to a blank node but this is irrelevant for this example. Then the _ready and _process functions are both defined as void functions, meaning they do not return anything. Note that the _process function has the delta parameter which is a float. Unlike in JavaScript, Godot throws an error if you have an empty function, in this example notice how both functions have the pass keyword on them which is a skip to avoid getting such errors.

Tips For Learning

  • Understand Godot’s Architecture: GDScript is tightly integrated with Godot’s scene system. Familiarize yourself with how Godot handles scenes and nodes to truly take advantage of GDScript.
  • Use Your JavaScript Knowledge: If you’re already proficient in JavaScript, use your understanding of concepts like variables, functions, and conditionals to help you learn GDScript. The biggest difference will likely be adjusting to indentation-based syntax and the specific features of Godot.
  • Practice with Small Projects: Start by creating simple projects in Godot, such as a 2D platformer, or go with the 20 Games Challenge. This will help you get accustomed to the engine’s tools and the syntax of GDScript.
  • Use The Help Function: Godot includes a help search bar from which you can find documentation on syntax among other elements.

Resources

20 Games Challenge

GDQuest

Your First 2D Game Tutorial

Conclusion

GDScript offers a smooth transition for JavaScript developers who are looking to dive into game development with the Godot engine. With its familiar syntax and powerful, game-specific features, it’s an excellent language for developing both 2D and 3D games. While it does have unique aspects such as its integration with Godot’s framework, your JavaScript knowledge will serve you as a solid foundation to quickly pick up GDScript. With practice and exploration, you’ll be on your way to bringing your ideas to life. Good luck on your journey into the world of game development!

Top comments (0)