Skip to content

How to make a Godot-style singleton #233

Answered by panno8M
FranChauntry asked this question in Q&A
Discussion options

You must be logged in to vote

Your method is best for use with Nim only. That is, the following pattern

# globals.nim
type Global* = object
  value*: int
var global*: Global
import gdext
import gdext/classes/gdNode
import globals

type Main* {.gdsync.} = ptr object of Node

method ready (self: Main) {.gdsync.} =
  global.value = 10

If you want to refer to it from GDScript as well, you need to devise a way to do so using the Godot mechanism.

# globals.nim
import gdext
import gdext/classes/gdEngine

type Global* {.gdsync.} = ptr object of Object
  value* {.gdexport.}: int

var global*: Global

proc register_Global_singleton {.execon: initialize_scene.} =
  # This callback is called from initialize_scene by the execon ma…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@FranChauntry
Comment options

Answer selected by panno8M
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants