0 votes

I'd like to do something like

config.gd

extend Resource
class_name Config
enum Configuration { Option1, Option2, Option3 }
export(Configuration) var how_to_do_something = Option1

builder.gd

build(config : Config) -> OptionStrategy:
  # id like to do something like this to instance
  # the strategy classes Option1, Option2, Opntion3, and so on
  # get_class(config.how_to_do_something.string).new()

It might sound senseless. But I know that I will add more Options in the future, and I don't want to maintain a match case in the builder for each Option I implement.

in Engine by (50 points)

1 Answer

0 votes
Best answer

I'm not aware of a way to instance by name from a string, but I know it's possible to do it if you have the path of the script resource (i.e res://scripts/my_script.gd):

var instance = load(path_to_script).new()

Which btw is how scripts are deserialized in the first place :p That's what I did to implement savegames in my last game jam (along with a few selected properties).

by (27,164 points)
selected by

I've been searching in the web about this. And sadly, it seems that it is not possible to convert enums to strings automatically. But yeah, I guess that doing load("%s.gd" % option_string_representation) would do the trick if I happen to have the string.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.

Categories