Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NativeScript support for script classes via exported script properties. #20561

Merged
merged 1 commit into from
Aug 16, 2018

Conversation

willnationsdev
Copy link
Contributor

Depends on #20560.

This enables NativeScripts to be registered as script classes by having the script in question implement String-returning methods _get_script_class_name() and _get_script_class_icon_path().

Let me know what you guys think.

@willnationsdev
Copy link
Contributor Author

@mysticfall This is an example of an implementation of those methods. The C# one would just have to parse out the relevant info from its GDMonoClass stuff (I think, idk all the C# details).

VisualScript would probably need you to add the name/icon_path as properties of VisualScript itself and then create GUI elements in the visual script editor that let the user input values for the name/icon, possibly use a FileDialog to select the desired icon, etc. Then the VisualScriptLanguage implementation would be a trivial task of loading the VisualScript and returning the member variables.

Not sure about PluginScript.

@willnationsdev willnationsdev force-pushed the script-class-ns branch 2 times, most recently from abba1cf to b1626f1 Compare July 31, 2018 02:59
@karroffel
Copy link
Contributor

I think we can add an API to NativeScript 1.1 to get rid of that function call

@willnationsdev
Copy link
Contributor Author

willnationsdev commented Jul 31, 2018

@karroffel In that case, you'll have to explain how the NativeScript class would access that information (to return it in the get_global_class_name method). Member property?

@karroffel
Copy link
Contributor

@willnationsdev we can add a new field to the gdns resource called global_name and the read from there without having to actually load a library.

@willnationsdev
Copy link
Contributor Author

@karroffel That'd be good. We'd also need a property for a file path to an icon.

@willnationsdev willnationsdev force-pushed the script-class-ns branch 3 times, most recently from 3113f84 to 5e4a48f Compare August 5, 2018 04:58
@willnationsdev
Copy link
Contributor Author

willnationsdev commented Aug 5, 2018

@karroffel I just modified this PR, so let me know what you think. I made properties "script_class_name" and "script_class_icon_path". Going to sleep now. ;-)

ClassDB::bind_method(D_METHOD("get_class_documentation"), &NativeScript::get_class_documentation);
ClassDB::bind_method(D_METHOD("get_method_documentation", "method"), &NativeScript::get_method_documentation);
ClassDB::bind_method(D_METHOD("get_signal_documentation", "signal_name"), &NativeScript::get_signal_documentation);
ClassDB::bind_method(D_METHOD("get_property_documentation", "path"), &NativeScript::get_property_documentation);

ADD_PROPERTYNZ(PropertyInfo(Variant::STRING, "class_name"), "set_class_name", "get_class_name");
ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "library", PROPERTY_HINT_RESOURCE_TYPE, "GDNativeLibrary"), "set_library", "get_library");
ADD_GROUP("Script Class", "script_class");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it common to include the trailing _ here? Not a deal breaker and the editor probably still shows it correctly 😄

}

String NativeScriptLanguage::get_global_class_name(const String &p_path, String *r_base_type, String *r_icon_path) const {
Ref<NativeScript> script = ResourceLoader::load(p_path, "NativeScript");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This actually loads and initializes the library. That's the only way you can get information like "base type", but it's still suboptimal.

This is just a comment, you don't need to change anything here, it's fine, just realized that it's a bit "meh" to load and unload libraries all the time when all you need is three strings 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there perhaps a better way we should be defining / accessing these values then? Perhaps some way to directly access the data contained within the resource file itself? I mean, I could directly open up the file and parse it for the information...but that's kinda hacky.

Copy link
Contributor Author

@willnationsdev willnationsdev Aug 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, if it inherits Resource, is there a way to load it AS a Resource, rather than as a NativeScript, and then just directly access the member properties on it using .get() (assuming that doesn't also load the library)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, if you just parse it as a regular resource it will dispatch to the custom code anyway AFAIK. And yeah, there's information like "base type" which you only know at runtime, unless we make the user specify it in the .gdns file as well (would only be necessary for global classes then)

ClassDB::bind_method(D_METHOD("get_class_documentation"), &NativeScript::get_class_documentation);
ClassDB::bind_method(D_METHOD("get_method_documentation", "method"), &NativeScript::get_method_documentation);
ClassDB::bind_method(D_METHOD("get_signal_documentation", "signal_name"), &NativeScript::get_signal_documentation);
ClassDB::bind_method(D_METHOD("get_property_documentation", "path"), &NativeScript::get_property_documentation);

ADD_PROPERTYNZ(PropertyInfo(Variant::STRING, "class_name"), "set_class_name", "get_class_name");
ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "library", PROPERTY_HINT_RESOURCE_TYPE, "GDNativeLibrary"), "set_library", "get_library");
ADD_GROUP("Script Class", "script_class");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it common to include the trailing _ here? Shouldn't make a difference in the editor, just wondering 😄

@karroffel
Copy link
Contributor

reeeeebase :)

@willnationsdev
Copy link
Contributor Author

Lol, gotcha. I'll get right on that in about an hour

@willnationsdev willnationsdev changed the title Add NativeScript support for script classes via virtual methods. Add NativeScript support for script classes via exported script properties. Aug 15, 2018
Copy link
Contributor

@karroffel karroffel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good and can be merged, but this will probably make some underlying bugs (leaks) more annoying. This is probably a good thing so I have a reason to fix them more timely :)

@willnationsdev
Copy link
Contributor Author

@karroffel Ok, cool. I don't have the ability to merge things, so someone else will have to do that. ;-)

@akien-mga akien-mga merged commit 968b31e into godotengine:master Aug 16, 2018
@willnationsdev willnationsdev deleted the script-class-ns branch August 16, 2018 18:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants