Easily accessible storage of variables

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!

Moderator: GZDoom Developers

Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

Please bear in mind that the people helping you do not automatically know how much you know. You may be asked to upload your project file to look at. Don't be afraid to ask questions about what things mean, but also please be patient with the people trying to help you. (And helpers, please be patient with the person you're trying to help!)
Post Reply
User avatar
Apeirogon
Posts: 1605
Joined: Mon Jun 12, 2017 12:57 am

Easily accessible storage of variables

Post by Apeirogon »

I create inventory item which do effect function do some effect depending of cvar variables.

But at map with huge amount of actors, find cvar function inside of it cause extremely heavy lags, up to game freeze.

So, is there are any way to create local, mods, storage of variable, which can be acessed, and changed, by any actor? And I say not only about cvars.
User avatar
Matt
Posts: 9696
Joined: Sun Jan 04, 2004 5:37 pm
Preferred Pronouns: They/Them
Operating System Version (Optional): Debian Bullseye
Location: Gotham City SAR, Wyld-Lands of the Lotus People, Dominionist PetroConfederacy of Saudi Canadia
Contact:

Re: Easily accessible storage of variables

Post by Matt »

I assume you mean actor variables like stamina or mass, rather than cvars (console variables) like autoaim and allowcrouch.

In which case I too would like to know this - is there a generic "findvariable" function in ZS?
User avatar
Eliot_L
Posts: 34
Joined: Thu Mar 08, 2018 3:40 am
Contact:

Re: Easily accessible storage of variables

Post by Eliot_L »

I can try to answer this specifically regarding CVars. One thing I do is cache the result of FindCVar in an instance variable so I don't have to do it every tick. That might help, although I have not measured this to see if it actually makes a difference on speed. See this class, which does FindCVar inside PostBeginPlay, after which it can just call SetInt and GetInt on the CVar.

If it's still too slow after that tweak, then perhaps the Get call on the CVar is also slow. In that case, perhaps you could try making a global variable singleton (or maybe an EventHandler) that polls the CVar on Tick and caches the value in a global variable? I'm not sure if this would actually help... you'd have to try it and see.
User avatar
Gutawer
Posts: 469
Joined: Sat Apr 16, 2016 6:01 am
Preferred Pronouns: She/Her

Re: Easily accessible storage of variables

Post by Gutawer »

Eliot_L wrote:I can try to answer this specifically regarding CVars. One thing I do is cache the result of FindCVar in an instance variable so I don't have to do it every tick.
Be careful with this! CVar objects can't be saved to a save file properly so without the transient keyword, this will cause a crash upon saving. If you define the CVar as being transient CVar whatever; it won't crash on saving, but this keyword means that the object isn't serialised, so you need to get the object back upon loading a save (I believe that StaticEventHandlers allow you to use WorldLoaded for this).
User avatar
Eliot_L
Posts: 34
Joined: Thu Mar 08, 2018 3:40 am
Contact:

Re: Easily accessible storage of variables

Post by Eliot_L »

Thanks for the tip! I'll set mine to transient, they don't need to persist.
User avatar
Matt
Posts: 9696
Joined: Sun Jan 04, 2004 5:37 pm
Preferred Pronouns: They/Them
Operating System Version (Optional): Debian Bullseye
Location: Gotham City SAR, Wyld-Lands of the Lotus People, Dominionist PetroConfederacy of Saudi Canadia
Contact:

Re: Easily accessible storage of variables

Post by Matt »

Gutawer wrote:CVar objects can't be saved to a save file properly so without the transient keyword, this will cause a crash upon saving.
This applies to CVar objects only, right? Not any int, string, etc. that you might get from it?
User avatar
Eliot_L
Posts: 34
Joined: Thu Mar 08, 2018 3:40 am
Contact:

Re: Easily accessible storage of variables

Post by Eliot_L »

Yeah I think it's just to tell the ZScript runtime not to try to save/load the CVar object.

It would be awfully handy if there was a virtual function for a class getting deserialized so you could more easily handle the case of having to re-initialize your transient members, but I don't see such a thing at the moment. Or just a way to lazy-initialize them. For now I might get rid of this as it may be more trouble than it's worth to handle save/load scenario...
Post Reply

Return to “Scripting”