Skip to main content
Became Hot Network Question
Correcting name of language
Source Link
DMGregory
  • 141k
  • 23
  • 258
  • 401

How do I create and import a class in Godot GscriptGDScript

I would like to create and import a class in gscriptGDScript. Here is some non-functional pseudo code to explain what I'm trying to do:

class_name WordPrinter

var my_word: String

func _init(word: String)
  my_word = word

func print_word()
  print(my_word)
const word_printer = import("./word_printer.gd").new("Hello World")

word_printer.print_word()

I've found various tutorials on classes and imports, but I have yet to find a straightforward code example that works. Here are some places I've looked:

The second link above does show working code, but the imported class does not have any type definitions, and is not very useful.

How do I create and import a class in Godot Gscript

I would like to create and import a class in gscript. Here is some non-functional pseudo code to explain what I'm trying to do:

class_name WordPrinter

var my_word: String

func _init(word: String)
  my_word = word

func print_word()
  print(my_word)
const word_printer = import("./word_printer.gd").new("Hello World")

word_printer.print_word()

I've found various tutorials on classes and imports, but I have yet to find a straightforward code example that works. Here are some places I've looked:

The second link above does show working code, but the imported class does not have any type definitions, and is not very useful.

How do I create and import a class in GDScript

I would like to create and import a class in GDScript. Here is some non-functional pseudo code to explain what I'm trying to do:

class_name WordPrinter

var my_word: String

func _init(word: String)
  my_word = word

func print_word()
  print(my_word)
const word_printer = import("./word_printer.gd").new("Hello World")

word_printer.print_word()

I've found various tutorials on classes and imports, but I have yet to find a straightforward code example that works. Here are some places I've looked:

The second link above does show working code, but the imported class does not have any type definitions, and is not very useful.

edited tags
Link
Theraot
  • 28.3k
  • 4
  • 55
  • 83
Source Link
Seph Reed
  • 275
  • 4
  • 10

How do I create and import a class in Godot Gscript

I would like to create and import a class in gscript. Here is some non-functional pseudo code to explain what I'm trying to do:

class_name WordPrinter

var my_word: String

func _init(word: String)
  my_word = word

func print_word()
  print(my_word)
const word_printer = import("./word_printer.gd").new("Hello World")

word_printer.print_word()

I've found various tutorials on classes and imports, but I have yet to find a straightforward code example that works. Here are some places I've looked:

The second link above does show working code, but the imported class does not have any type definitions, and is not very useful.