Variables created from editor and saved

Hi, I’m trying to make a window to create variables to be used at run-time.

Example
Purely create it from editor:

Then Later on be able to use it in game like

float speed = SomeRandomClass.AccessProperty("Speed");

The biggest issue I’m facing right now is how to store the contents of the table.
Any help is appreciated.

One issue with your example API is how would AccessProperty know what is the correct return type?
From the screenshot, it seems that you’re able to set up a number of different return values. This means that AccessProperty should be able to support these.

this will not work, since you cannot define method overloads that differ only by the return type, so you cannot define multiple AccessProperty methods with different return types (e.g: float, int, string, etc).

To implement this, I can think of a few different solutions:

  1. Source code generation. Think of something like T4 (read more here: Code Generation and T4 Text Templates - Visual Studio 2015 | Microsoft Learn). Once you define all the fields, you can run it through a processor that will generate a code file with all the different data types exposed as fields / properties. Each with its own correct type.

  2. Code generation - you can use Mono.Cecil (Mono.Cecil | Mono). You can generate a new assembly on the fly and store that into the project. This is the same as #1, only working at the IL level, generating a .DLL at the end of the process.

  3. Define a different API than the one you suggested, and use some sort of data “storage” (class? scriptableobject?).
    For example:

SomeRandomClass.GetFloat(string name)
SomeRandomClass.GetInt(string name)

Under the hood, you can use a dictionary (or multiple dictionaries - one for each data type) and return the value from that dictionary.

There are probably many different ways to solve this problem. These are only a few suggestions that can give you some food for thought on how to implement this.

1 Like

The different API idea may be the solution here. But I’ll definitely look into the other suggestions as well.