Andrey Kachow
Andrey Kachow

Reputation: 1126

GDScript. How to make your own library in Godot and access it from your game scripts

I wonder how can I create a script, which contains methods I want to use in multiple scripts. I don't think I want to create a global singleton for it, because I am not storing any global data which will be preserved across multiple scenes. I am having a collection of useful functions nothing else.

Upvotes: 8

Views: 5404

Answers (1)

Andrey Kachow
Andrey Kachow

Reputation: 1126

A possible way to create your own library you just create a new script that extends nothing or extends Object. Use static keyword in front of your functions.

in my_lib.gd

extends Object

static func my_static_function():
    print("hello from my_lib.gd")

in your game script, you can access it using preload function

const my_library = preload("res://my_lib.gd")

func test():
    my_library.my_static_function()

Upvotes: 13

Related Questions