Skip to reading
GDScript Programming & Godot

50. Include a PCA script library

Edition 0.86.4.1 HF1

0.86.4.1 Hotfix 1 edition. Earlier lessons and screenshots retain their original version context; revised references and supplements describe the newer behavior.

Download PDFView PDF page
Code example
func add_points(base: int, bonus: int = 1) -> int:
    return base + bonus
Code example
func run() -> int:
    return add_points(10, 5)

Put the example in a separate native record utility_gd. An IncludeScript call returns a script-call proxy, not pasted source and not the other record’s actual Node instance.

Code example
func run() -> int:
    var library: Variant = IncludeScript("utility_gd")
    var total: Variant = await library.Call("add_points", [10, 5])
    library.Set("last_total", total)
    return int(library.Get("last_total", 0))

Call invokes a named function; Run invokes run. Get/Set use the included-variable store, separate from the library’s member variables. A one-argument IncludeScript does not execute the library function by itself.

Global.Get(id) provides the same sort of call proxy. Use the public wrapper to inherit caller context, and use unique script IDs. Global is not a native Godot autoload singleton you can assume exists in an unrelated .gd file.

Page 59 of 378Permalink
Go to page