Skip to content

[FEATURE] Make Static Fields As Shared State With Imports #30

Description

@RapperGF
  • NxScript version: DEV
  • Branch: GitHub (dev)

What problem does this solve?

Currently it is impossible to have shared state across scripts that use isolated vms. A script can import a module but this leads to local copies without mutable state.


Proposed solution

Instead of having static fields as local copies on the VM the compiler would emit STORE_STATIC/LOAD_STATIC
which would then be stored as a single shared map across all vms

in VM.hx

static var staticVars:Map<String, Value> = new Map();

Storing all static fields in a single map can allow haxe to share the state without running into sync issues.
we will not have the ability for fast path access and it will always be a map lookup.


Alternatives considered

Have you tried any workarounds? Are there other ways this could be solved?

It is possible to make a host helper class to grab globals from each script via key but this is tedious and inflexible.

import ScriptHelper;
ScriptHelper.get(mod, field);

I believe we also support using so it could be less friction but still unideal.


Example usage

This would be the most invasive way to have shared state all a script would need to do is import the module
and then use it as so.

in Stage.nx

public static var health:Float = 1;

in User Script

import Stage;
Stage.health = 100;
var hp = Stage.health;

It would also be mutable across scripts as the map is always shared for all vms.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions