Godot: Difference between revisions

From Leo's Notes
This page was last edited on 18 October 2020, at 20:02.
mNo edit summary
mNo edit summary
Line 34: Line 34:


Objects belong to one collision layer but can collide with other objects in other layers by setting a collision mask. Objects that can collide can only be moved if they are a KinematicBody or if you manually change the position property of the object.
Objects belong to one collision layer but can collide with other objects in other layers by setting a collision mask. Objects that can collide can only be moved if they are a KinematicBody or if you manually change the position property of the object.


Tween to make position changes transition smooth?
Tween to make position changes transition smooth?
or more layers. All physics objects specifies which collision layers apply to them.
collision layers and collision masks. The collision layer specifies which layer co
<br />
===Tilemap===
===Tilemap===
Tilemap or Gridmap can be used to build out the game world. Each uses a Tileset or MeshLibrary (respectively) to build their tiles.  
Tilemap or Gridmap can be used to build out the game world. Each uses a Tileset or MeshLibrary (respectively) to build their tiles.  
Line 52: Line 43:
<br />
<br />


== Observations ==
==Observations==
In Tux Builder, the main scene is the Master/Gameplay.gd. The UI is loaded with _load_node("res://Scenes/UI/LevelUI.tscn", "LevelUI"). Editor is similar.  
In Tux Builder, the main scene is the Master/Gameplay.gd. The UI is loaded with _load_node("res://Scenes/UI/LevelUI.tscn", "LevelUI"). Editor is similar.  



Revision as of 20:02, 18 October 2020

Concepts

File System

The filesystem that Godot provides is res:// for resources such as static game files or assets and user:// for dynamic data such as generated map data or game saves. Addons should be placed in res://addons in your project. Godot converts resource files into its own format and are placed in the res://.import folder.

Objects

A Node is the building block of Godot; all scene objects in Godot are Nodes. Nodes have properties and functions and can emit signals which others can subscribe to. Nodes can be assigned as a child of another node and are arranged into a tree. A tree of nodes is referred to as a Scene. Active scenes that are loaded and in use are part of a Scene Tree. In addition to the hierarchy/inheritance model, nodes can also be organized by groups (eg. as groups of Enemies, Collectables, etc. which can be retrieved with get_tree().get_nodes_in_group("enemies")).

A scene is saved to a TSCN (text-scene) file.

Scene Tree

Godot maintains an active set of scenes called a Scene Tree. When scenes are added to the scene tree, all its nodes will be added. Child nodes are added after their parent node. The root node of the Scene Tree is up to you but must be of a Node2D for 2D scenes or Spatial for 3D scenes.

Control nodes are used for user interfaces and behave differently from Node2D. Controls should be placed in a separate tree, under a CanvasLayer node.

Code can get the SceneTree by calling get_tree().

Nodes also receive a enter_tree notification. Only when all nodes have been added will they then be ready.

2D

(0, 0) is the top left.

Sprite nodes are drawn in the order that they are traversed on the SceneTree. Traversal is done depth-first (TODO: is this right?), meaning the first objects in the tree are drawn first. To switch the draw order, based on its Y position on the screen, use a YSort node (Huh. does this call sort every time something needs to draw? How do we deal with specific ordering otherwise?).

Physics

The physics engine nodes are:

  • StaticBody - Non-moving solid physical obstacle.
  • RigidBody - Moving physical object. Managed by engine.
  • KinematicBody - Moving physical object. Managed by you the coder.
  • Area - Non-obstacle. Detects overlap with other physics bodies or areas.
  • VehicleBody - Simulate a moving vehicle with wheels. Includes drive and steering wheel functionality.
  • RayCast - Check along a directed line for any physics object; fast and effective check.

Objects belong to one collision layer but can collide with other objects in other layers by setting a collision mask. Objects that can collide can only be moved if they are a KinematicBody or if you manually change the position property of the object.

Tween to make position changes transition smooth?

Tilemap

Tilemap or Gridmap can be used to build out the game world. Each uses a Tileset or MeshLibrary (respectively) to build their tiles.

Alternatively you could write your own.


Observations

In Tux Builder, the main scene is the Master/Gameplay.gd. The UI is loaded with _load_node("res://Scenes/UI/LevelUI.tscn", "LevelUI"). Editor is similar.

Objects in the game are its own scene (tscn). The scene itself has _physics_process function that checks if it is affected by gravity, sets its velocity and direction to avoid out of bounds, or checks if it has reached a floor. It also checks if a player has collided with it. If he has, it plays a sound and stops its animation. The trampoline does more in a _on_Area2D_body_entered function which checks if the body is a player, enemy, or another trampoline. How a stack of trampolines interact with each other is handled here. The body that entered will have its velocity set.

Examples

Node

Example of signals

extends Node2D

signal got_hurt (damage)  

onready var hitpoints = 10 setget set_hitpoints

func set_hitpoints( new_hp ):
    var diff = new_hp - hitpoints
    if diff < 0:
        emit_signal("got_hurt", diff)
    hitpoints = new_hp

Questions & Answers

  1. How do we separate business logic from the presentation? It looks like we make models inherit Sprite so that they render so the MV (in MVC) is combined. How does this work if we want, say a 'headless' version of a game?
    • I suspect that everything even when it inherits a Sprite is treated as a model. Godot then renders everything separately when needed. This removes the dependency on graphical stuff if it isn't needed.
  2. sdf

See Also