General Godot Information - acbarker19/Godot-Action-RPG GitHub Wiki

Different Types of Bodies for Objects

  • KinematicBody2D = Movement controlled by code or user
  • RigidBody2D = Movement controlled by physics
  • StaticBody2D = No movement

Attaching a Script to a Node

  1. Select the desired node
  2. Click the Attach Node Script button in the left menu (Looks like a scroll with a plus)
  3. Make sure language is set to GDScript
  4. Set the desired path

Do Something on Key Press

if Input.is_action_pressed("ui_KEYNAMEHERE"):

Lock a Node's Child Nodes

  1. Select the parent node
  2. In the top menu, select Make sure the object's children are not selectable (Looks like 2 boxes stacked diagonally)

Toggle Pixel Snapping in the Main Window

When you are able to change the size or position of an object in the main window, it might be useful to have your changes always snap to the nearest pixel. To toggle this, do the following:

  1. Click on the 3 dots in the top menu
  2. Check or uncheck Use Pixel Snap

Toggle Grid Snapping in the Main Window

When you are able to change the size or position of an object in the main window, it might be useful to have your changes always snap to the nearest grid tile. To toggle this, do the following:

  1. Click on the Use Grid Snap button in the top menu (Looks like a tic-tac-toe board with a magnet)

Move a Child Node Without Moving the Parent Node

  1. Select the child node
  2. Hold the alt key and drag the child node in the main window

Create Object That Can Be Re-used Across Scenes

If there is a node that you have already created, skip to step 4.

  1. Press the + button at the top of the main window
  2. Select 2D Scene, User Interface, or Other Node. If the object is being placed in a scene, it will most likely be Other Node
  3. Set up the object however you wish
  4. Right-click node that you wish to re-use
  5. Select Save Branch as Scene
  6. Select save location
  7. Click the object's Open in Editor button (Looks like movie clapperboard)
  8. Make sure position is set to (0,0)

Add Re-usable Object to Scene

  1. In the FileSystem in the bottom left, open the folder with the object
  2. Drag the OBJECT.tscn file into the main window

Assign Variable When Object is Ready

Some variables will use an object's nodes and require the object's _ready() function to be called. This can be seen below:

var variableName = null

func _ready():
	variableName = $NodeName

This can be shortened to the following:

onready var variableName = $NodeName

onready will wait for the object to completely load. $ will allow you to access an object's child node.

_physics_process() vs. _process()

Both functions can be run inside a KinematicBody2D to create a repeating action based on delta time. _process() will have variable delta time and should be used in most cases. _physics_process() has a constant delta and is used after a physics event has occurred. This would be useful for things related to the actual KinematicBody2D, such as getting the object's position.

queue_free()

queue_free() will remove an object from a scene and delete its children.

Area2D

Area2D nodes require a child CollisionShape2D. It is useful for detecting collisions when something passes in or out of a space.

Collision Layers vs Collision Masks

  • Collision layers describe what layer a collidable object is on.
  • Collision masks describe what layer a collidable object can collide with. For example, a player would be on the Player layer, and they would have a World mask. This would mean any other objects that have a Player mask will collide with a player, and the player will collide with any other objects that have a World layer.

Export Var

Using export var var_name will allow a variable to be edited within the scene editor whenever one scene is attached to another. For example, the Stats scene has export var max_health. If Stats is attached as a node to an enemy scene, the max_health variable can be changed in the right menu under Script Variables > Max Health.

An important thing to note is that exported variables can be edited as the game is running, so they can be fine-tweaked during gameplay.

SetGet

When declaring a variable, setget can be added to attach getter an setter functions that are accessed whenever the variable is. For example, a class could have the following:

var some_value = 1 getset set_some_value, get_some_value

func set_some_value(value):
	some_value = value


func get_some_value():
	return some_value

With a variable initialized this way, the variable can be retrieved and updated by simply accessing the variable like the following:

some_object.some_value = 5
some_other_value = some_object.some_value

This is particularly useful for getters or setters that have more logic to them. One of the functions can also be skipped in the following way:

var some_value = 1 getset set_some_value
var some_value_2 = 2 getset , get_some_value

Call Down/Signal Up

When a node is interacting with a child node, it should call down in the following way:

var child = $ChildNode

child.some_value = 1

When a node is interacting with a parent node, it should signal up to the parent. In the child script, add the following:

signal some_signal

// whenever it needs to interact with the parent, add the following
emit_signal("some_signal")

To receive the signal in the parent node, do the following:

  1. In the left menu, select the child node
  2. In the right menu, open the Node tab
  3. Double click the signal that should be observed
  4. Click Connect in the popup
  5. Inside the newly created function in the parent, add the code that is needed to interpret the child's changes

How to Add Something Programmatically to the World Scene

It may be useful to add something to the world scene through code. To do this, add the following:

var objectName = load("res://Folder/ObjectName.tscn")
var world = get_tree().current_scene
world.add_child(objectName)
objectName.global_position = global_position

If the object can be created at a point relative to the parent node, it may be better to get the parent node instead of the world.

How to Add Something Programmatically to the Parent Scene

It may be useful to add something to the parent scene through code. To do this, add the following:

var objectName = load("res://Folder/ObjectName.tscn")
get_parent().add_child(objectName)
objectName.global_position = global_position

If the object can be created at a point in the world, it may be better to get the world instead of the parent node.

Control Nodes

Control Nodes are primarily used for UI elements that are meant to stay on a specific position of the screen.

Monitor Game Performance

To view general performance, do the following:

  1. Play the game
  2. In the IDE, select Debugger on the bottom menu
  3. In the Debugger menu that popped up, select the Monitors tab
  4. To view a graph of data, check one of the Monitors listed

To see what is causing performance drops, do the following:

  1. select the Profiler tab in the bottom Debugger menu
  2. Select Start
  3. Play the game
  4. Select Stop
  5. Select a frame where performance dropped
  6. View the list of functions that took the most time or calls

Randomize Project Load States

When a scene loads, it will load with the same seed for randomization. This means that every action the game takes independent of player interaction will be the same until some code changes. If the game should have a different state every time it loads, the game just needs to call the randomize() function somewhere. In this project, it is called in the Player's _ready() function.