Logo

Braindead.bzh

Game Dev Hobbyist

A blog about my game dev hobby and various stuff.

Menu
Logo

Braindead.bzh

Game Dev Hobbyist

A blog about my game dev hobby and various stuff.

Utilities

Overview

CardEngine provides some utilities to help with game development in general. At the moment the following tools are available:

  • The screen system helps with organizing your game.
  • The pseudo-random number generator helps with creating seeded game.

Screen system

AbstractScreen class: res://screens/abstract_screen.gd (source)

Introduction

The screen system is a very simple way to switch scene. It works with the combination of the main scene, scenes inheriting from AbstractScreen and a signal.

The main scene

Main scene: res://main.tscn (script)

The main scene contains two components essentials to the screen system:

  • A dictionary called _screens where you defined all the available screens and give them an ID.
  • A function called change_screen which switch from one screen to another. You don't call this function directly instead this function is connected automatically to the next_screen signal of the loaded screen.

Creating a screen

To create a screen, simply create a scene which inherits AbstractScreen, and save it in the screens folder for consistency. To be callable from other screens, add your screen to the _screens dictionary of the main scene (see above). Example:

var _screens = {
    "menu": preload("res://screens/menu/menu_screen.tscn"),
    "builder": preload("res://screens/builder/builder_screen.tscn"),
    "game": preload("res://screens/game/game_screen.tscn"),
    "board": preload("res://screens/board/board_screen.tscn")
}

Switching screen

You can easily switch from one screen to another from script by emitting the next_screen signal, passing the ID of the screen to load. Example:

func _on_MenuButton_pressed() -> void:
    emit_signal("next_screen", "menu")

Pseudo-random number generator (PRNG)

PseudoRng class: res://addons/cardengine/rng/pseudo_rng.gd (source)

Introduction

Many games which have randomness in them rely on a seed that is fed to a PRNG, this is the case with most of Rogue-like or Rogue-lite types of games. This give the impression of randomness while being deterministic given the same seed. CardEngine offers a utility class to help with this. Godot also offers a PRNG but you can only have one generator at a time. CardEngine overcomes this limitation and allows for unlimited generators to work in parallel.

API

  • set_string_seed(str_seed: String) -> void: seeds the PRNG using a string of characters, this helps making sharing seeds more user-friendly.
  • set_seed(random_seed: int) -> void: direct seeding of the PRNG.
  • generate() -> int: generates a random integer number and returns it.
  • randomi() -> int: alias for the generate function for API consistency.
  • randomf() -> float: generates a random float number between 0.0 and 1.0 (included) and returns it.
  • random_range(from: int, to: int) -> int: generates a random integer number between from and to (included) and returns it.
  • randomf_range(from: float, to: float) -> float: generates a random float number between from and to (included) and returns it.
  • random_vec2_range(from: Vector2, to: Vector2) -> Vector2: generates a random 2D vector between from and to (included) and returns it.