Marijo Trkulja's Blog - Posts Tagged "godot-gdscript-game-making"

Mastering Godot - articles from New book

New book by the author of the well-known titles on Godot game engine and GDScript such as “GD Script”, “Making games with GDScript”, “Autonomous Cars”, etc.

The book uses the MTH method for learning and is written for both beginner and experienced game developers. Beginners are advised to read a book from the beginning, and game developers to use it as a reminder and troubleshooting guide.

[From Mastering Godot book preface] If you are a complete beginner, start reading from the beginning. You will learn the basics of GDScript through the features and methods of the 2D node class. After that, go through the "GDScript in the programming" chapter. Later, you can learn about StaticBody2D, RigidBody2D, and KinematicBody2D. Game examples after @GDScript class and after 2D Body's chapter will be of additional help to you. In addition to the above for beginners, I advise you to watch free video tutorials on my Udemy account (Slavs Make Games M.D.C.). Also, a lot can be learned from video lessons Slavs Make Games M.D.C.


[Mastering Godot book first chapter]

Class 2D Node

This is a fundamental class for other 2D nodes. Class is inherited from the node class and has a position, rotation, scale, and z-index properties.

Properties:
position
rotation
rotation_degrees
scale
transform
global_position
global_rotation
global_rotation_degrees
global_scale
global_transform
z_index
z_as_relative

Position

Position can be set and can be retrieved from 2D object. For position setting use set_position(), and to retrieve position use get_position(). Vector2 variable need to be defined for set_position() method. When you use variable with get_position() method, variable will be vector2.

Example:

extends Node2D

func _ready():
var pos = Vector2(21,21)
self.set_position(pos)

Example comment:

For example, "self" represents an extended node – Node 2D. "Var" is used to define variables. Vector2 is a variable type for 2D space with horizontal and vertical space defined. The first variable in vector2 is horizontal, and the second variable is vertical. "Func" represents function – part of code. Function ready() automatically starts when you start a game scene and you don’t need to call it.

Rotation

Rotation can be set and can be retrieved from the 2D object. For rotation setting use set_rotation(), and to retrieve rotation use get_rotation(). Rotation is set in radians. When you retrieve rotation, the variable will be in radians. To convert radians to degrees, the key is knowing that 180 degrees are equal to pi radians. Then multiply the measurement in radians by 180 divided by pi. For direct work in degrees see rotation degrees property. In calculation, one radian is 57.2958.

Example:

# example code can be part of ready function
var rot = 1
self.set_rotation(rot)
var rot_deg = rot * 180 / PI

Example comment:

In this example, you can see the symbol “#”. This symbol is used for one line comment. The example also includes symbols for mathematical operations (* for multiplication and / for division) as well as a symbol for pi constant. Constant that represents how many times the diameter of a circle fits around its perimeter. It is approximately equal to 3.14159.

Scale

To scale in 2D space, the "scale" is used. The set_scale () method is used for set, and the get_scale () method is used to get scale value. For the following example to work, move the default Godot sprite into 2D scene space. This way the sprite node will be the child node for the 2D node.

Example:

extends Node2D

export var sca = Vector2(0,0)

func _ready():
sca = Vector2(3,3)
self.set_scale(sca)
print(self.get_scale())

Example comment:

Export var is used when we want to enable the value of a particular variable to be changed. It can be used when testing code and initially defining values. The print command prints a value in the “output” window.

Transform

Transform allows you to define position and rotation in 2D space. This property uses the transform2D variable defined by two parameters. Rotation(in radians) and position in 2D space. Use set_transform() method to set transform, and get_transform() to retrieve transform.

Example:

extends Node2D

func _ready():
transform_object(1,Vector2(60,60))


func transform_object(rot,pos):
var trans = Transform2D(rot,pos)
self.set_transform(trans)
var for_print = self.get_transform()
print("Transform2D: " + str(for_print))

Example comment:

In the example, we have a user-defined function. This function is called from ready with the function name. The user function have two parameters. These parameters are later used in transform2D. When you want to print some text via the "print" command, the text should be in quotation marks. Use the "+" character to connect parts of the text. Use the str () command to translate numeric values into text.

The writing of this extensive book is in progress, look for the free version here.
 •  0 comments  •  flag
Share on Twitter
Published on July 14, 2020 10:29 Tags: godot-gdscript-game-making