Public properties and fields

BeginnerProgrammer

When you declare a public property or field in a script, the property becomes accessible in Game Studio from the script component properties.

Property in Game Studio

You can attach the same script to multiple entities and set different property values on each entity.

Note

Public properties or fields must be serializable to be used in Game Studio.

Add a public property or field

This script has a public property (DelayTimeOut):

  1. public class SampleSyncScript : StartupScript
  2. {
  3. // This public member will appear in Game Studio
  4. public float DelayTimeOut { get; set; }
  5. }

Game Studio shows the DelayTimeOut property in the script component properties:

Public property appears in the Property Grid

Note

As a general rule, if you want to display the property or field in Game Studio, getters and setters should do as little as possible. For example, they shouldn't try to call methods or access Xenko runtime API.

For example, the following code will create problems, as it tries to access Entity.Components, which is only available at runtime:

  1. public class SampleSyncScript : StartupScript
  2. {
  3. private float delayTimeOut;
  4. // This public member will appear in Game Studio
  5. public float DelayTimeOut
  6. {
  7. get { return delayTimeOut; }
  8. set
  9. {
  10. delayTimeOut = value;
  11. Entity.Components.Add(new SkyboxComponent());
  12. }
  13. }
  14. }

If you want to include code like this in a property or field, hide it so Game Studio doesn't display it (see below).

Hide properties or fields in the Property Grid

If you don't want Game Studio to show a property in the Property Grid, you can:

  • declare your member internal or private, or
  • use the DataMemberIgnore attribute like this:
  1. // This public property isn't available in Game Studio
  2. [DataMemberIgnore]
  3. public float DelayTimeOut { get; set; }

Game Studio no longer shows the property:

Public property been hidden with ```[DataMemberIgnore]```

See also