Skip to content

C#: Allow read-only fields and properties to be export variables #8502

@gustovafing

Description

@gustovafing

Describe the project you are working on

A fast-paced 3D multiplayer shooter.

Describe the problem or limitation you are having in your project

Multiple base classes (e.g. a weapon class) have properties that configure how the node behaves which should not be edited at runtime. However, these properties can't be readonly and also be accessible in the editor, which limits the safety that readonly properties can provide.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Create an ExportAttribute source generator to allow export variables to be applied during object construction, not after the constructor has been called.

Note - there might be a problem where the scene file has been edited to change an export variable, but because the project hasn't been recompiled and source regenerated the change won't actually apply.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

On project compile:

  1. Get all classes that contain a readonly field/property marked with a ExportAttribute
  2. Create/extend the classes constructor using a source generator to set values for readonly properties
  3. Apply all non-readonly export variables as usual.

If this enhancement will not be used often, can it be worked around with a few lines of script?

This can be worked around simply by not marking values as readonly. Alternatively, custom setters could be written to only allow values to be written once when export variables are applied.

Is there a reason why this should be core and not an add-on in the asset library?

This improves the usage of export variables in C# by reducing the restrictions placed on them, and isn't a major change to how they operate.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions