GodotSharp icon indicating copy to clipboard operation
GodotSharp copied to clipboard

Exported variables

Open neikeq opened this issue 9 years ago • 11 comments

Implement the Export attribute including a PropertyUsage enum (Actually, the PROPERTY_USAGE_* constants will be exposed; preferable as enum constants) and a PropertyHint (PROPERTY_HINT_* constants will be exposed too) interface with its implementations classes.

There is range for improvement here in comparison with GDScript's keyword. C# is a statically typed language so the type does not need to be inferred or manually specified. C# supports enums, so the enum hint is unnecessary.

neikeq avatar Aug 06 '16 20:08 neikeq

Has this been implemented?

ghost avatar Oct 07 '17 02:10 ghost

Yes, but there are still improvements we can make compared to GDScript. e.g.: Filling hint_string automatically for enums.

neikeq avatar Oct 07 '17 19:10 neikeq

Great! Are there any examples about how to export variables as in GDScript?

ghost avatar Oct 07 '17 19:10 ghost

The most basic one would be:

[Export]
float speed = 200f;

neikeq avatar Oct 07 '17 19:10 neikeq

Nice to know this is working... I think it also needs to be public, so

[Export]
public float speed = 200f;

NathanWarden avatar Oct 07 '17 20:10 NathanWarden

BTW, is that ok? Should we also allow it for non-public fields as well? Another TODO is to make it work for properties as well.

neikeq avatar Oct 07 '17 20:10 neikeq

Thanks for asking. Since we're explicitly placing an attribute, I think it would be good to do it for private fields too. You may want to change these fields per node in the editor, but still keep the code clean by keeping them private from other classes.

NathanWarden avatar Oct 07 '17 20:10 NathanWarden

Yeah it should be public and I don't know if it makes sense to expose also private ones. Normally private variables are manipulated in C# either through methods or through a public propriety with getter/setter logic.

Valentactive avatar Oct 07 '17 20:10 Valentactive

Yes, I think the good practice is to allow it only for public members. The problem is C# attributes cannot be made to target only public fields, so it would be a runtime error printed by the editor or silently ignoring it like it's done right now.

neikeq avatar Oct 07 '17 21:10 neikeq

As an example, in Unity they automatically export all public variables, but you can place an attribute in order to expose a private field to the editor. Just so I don't misrepresent myself, I like having to add an attribute on all exported variables, as this keeps the inspector clean :) (IE. exposing all public variables automatically can get messy)

Several Unity developers/plugins do expose private variables to the inspector. I think the most common reason for this is that there are sometimes values you want the user to be able to change as initial values, but they shouldn't be changed at runtime. So they're exposed to the inspector, but not via the API.

If a person feels they should only have exported variables be public, they can always simply choose not to put the export attribute on any of their private variables. In other words, allowing for both public and private variables to be exported allows everybody to work according to their own style.

NathanWarden avatar Oct 07 '17 21:10 NathanWarden

@neikeq New script variables appear in the property editor only after restarting the editor. I tried saving the script file, closing and reopening the scene but it hasn't worked so far.

ghost avatar Oct 08 '17 00:10 ghost