Write to another script's global variable?

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
Post Reply
violgamba
Posts: 10
Joined: Sat Apr 29, 2023 9:16 pm

Write to another script's global variable?

Post by violgamba »

Hi. New Dungeon Editor user here. I've hit an issue that I haven't been able to get past and I was hoping someone could help. I am interested in having a "global" script where game-wide variables are stored, such as flags representing player actions and choices. I have been able to read global variables thusly ("globals" is a script object on the map for these examples):

Code: Select all

if globals.script.f_undeadFood then ...
And if I set variables, such as f_undeadFood, from within the global script object, then things work as expected. This includes the variable's state being serialized along with the save game:

Code: Select all

function toggleUndeadFood()
	f_undeadFood = not f_undeadFood;
end
However, if I try to assign to the variables from a different script, such as this:

Code: Select all

globals.script.f_undeadFood = true;
It seems to override an implicit getter with a direct value rather than modifying the actual variable within the global script. This still updates what other scripts see as the variable's value, but the value change is not serialized along with the save game. Instead, saving the game after changing the value in this way produces this warning:

Code: Select all

Warning!  Game object property not saved: globals.script._userProperties
So, is it possible to modify a scripts global variables from a different script? If not, is there any other way to make game-wide global flags? I've already tried this:

Code: Select all

party.party.f_undeadFood = true;
Which had basically the same warning when saving the game:

Code: Select all

Warning!  Game object property not saved: party.party._userProperties
This party.party.* option has some benefit, actually, as it resolves a concern I have about flags being tied to specific maps.

Thanks for any help you can provide.

PS >> I recognize that I could write a setter method for each variable. However, I've written a branching dialogue system, and I expect to have a LOT of flags. Any overhead to flag management is pretty rough.
User avatar
Isaac
Posts: 3172
Joined: Fri Mar 02, 2012 10:02 pm

Re: Write to another script's global variable?

Post by Isaac »

I recognize that I could write a setter method for each variable. However, I've written a branching dialogue system, and I expect to have a LOT of flags. Any overhead to flag management is pretty rough.
Write a setter function that accepts both the variable name and the value to assign; optionally store your variables in a table that this setter function updates. Consider a getter function that returns the value of the name given.
_________________________

There is another option, it comes with caveats, but can be used with a bit of care.

The internal variable Config will accept direct assignments, and return their values like so:

Code: Select all

--single value
Config.critter = "rabbit"

--table array
Config.vars = {}
	Config.vars.hounds = 2
	Config.vars.foxes = 1

--assigning via script 
Config.vars.foxes = 2
--changes the value to 2
The contents of Config are not saved with the game, so you will have to manage that manually in your script (at every reload) if persistence is needed. A variable in some script_entity can be used to hold a copy of the Config contents; best to use a unique / personalized table name in Config with all of your variables inside it; to avoid name collisions, and make copying & restoring easier.

______________

The One Room Round Robin III mod offers the full source project, and it includes a branching dialog system among its many custom features.
violgamba
Posts: 10
Joined: Sat Apr 29, 2023 9:16 pm

Re: Write to another script's global variable?

Post by violgamba »

Thank you for this quick reply! I briefly considered generic getter/setter functions, but I wasn't sure how to set variables via string name. Turns out that Lua's _G and _ENV vars allow for this, but GrimRock script objects have no access to those vars. Your idea of holding variables in a table in the script object fixed the problem, though. Also, script objects are accessible from anywhere within the dungeon, which is essential.

I like the idea of using Config for managing global vars as it allows for code that isn't reliant on dungeon layout, making it portable and less error prone. My first attempts were based on this. However, it's not clear how to serialize data during saving because (A) I'm not aware of any pre-save events to trigger copying the data into a serializable form (B) I'm not sure how I could copy the data for serialization... except for maybe having a dungeon script object that copied the data into itself, which kind of ruins the point of having code not be reliant on dungeon layout.

Finally, I wonder if there is a good way to trigger code to run at every reload (for setting up the Config). Currently I'm doing this with a 0-interval Timer on the party object that initializes the Config, then turns itself off forevermore. It's certainly hacky, but onInit() doesn't run when loading a savegame.

EDIT: And I just figured out that the Timer trick doesn't work right either as the timer being disabled is serialize in the save game. The only way I know to do this is to keep the timer running and, at each activation, check for a variable that needs initialization.

Thanks for the link to One Room Round Robin. I look forward to seeing how dialogue is handled, on both the front and back-end. It looks like a fun project. If another One Room Round Robin occurs, I'd love to be involved.
minmay
Posts: 2768
Joined: Mon Sep 23, 2013 2:24 am

Re: Write to another script's global variable?

Post by minmay »

Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
violgamba
Posts: 10
Joined: Sat Apr 29, 2023 9:16 pm

Re: Write to another script's global variable?

Post by violgamba »

Thank you! It certainly fits what I'd experienced.
Post Reply