Accessing static data of functions

Paul Backus snarwin at gmail.com
Thu Oct 14 14:32:49 UTC 2021


On Thursday, 14 October 2021 at 14:21:21 UTC, Ogi wrote:
> Is there any way to get/set static variables defined in 
> functions from the outside? Also, is there any way to access 
> the types defined inside in function to get/set their static 
> members?

No.

> So far so good, I’m now able to serialize/deserialize game 
> objects (including private fields), and any global variables 
> defined in a plugin, and any static members of the types 
> defined in a plugin. But I don’t know what to do with the stuff 
> that could be defined inside of a function, I can’t find any 
> trait or a template that could access it.

Avoid storing any of your game's state in static variables.

You can refactor any function with a static variable into a 
struct method:

```d
// Before:
int fun()
{
     static int n = 123;
     return n;
}

// After:
struct S
{
     int n;
     int fun() { return n; }
}

auto s = S(123);
// call s.fun()
```


More information about the Digitalmars-d mailing list