PDA

View Full Version : io.open ... is there a way to enable this?



JimSocks
November 5th, 2020, 19:54
I need the ability to both write and read from an external .txt file from within the lua code. When I try this I get:


lua attempt to index global 'io' (a nil value)

Here is my implementation in the code:


local file = io.open( "StoredTableResults.txt", "a+" )

Can I enable the use of the io library? Any help will be GREATLY appreciated.

Trenloe
November 5th, 2020, 19:58
There are a lot of LUA libraries specifically not allowed in the FG API for security reasons. Giving direct access to the io library would be a major security issue.

More info here: https://fantasygroundsunity.atlassian.net/wiki/spaces/FGU/pages/4128914/Ruleset+-+Scripting#Fantasy-Grounds-Specific-Lua-Changes

JimSocks
November 5th, 2020, 20:26
Thanks Trenloe- quick question after reading that article:

It sounds like I can read and write to the Campaign Registry? If so, where can I learn more about how to do that?

Trenloe
November 5th, 2020, 20:41
It sounds like I can read and write to the Campaign Registry? If so, where can I learn more about how to do that?
It's basically a set of LUA tables that get stored in a specific file when the campaign exits, and read back in when the campaign loads again.

As mentioned in that article - they're CampaignRegistry and GlobalRegistry - the former is campaign specific and data for the campaign is stored in <FG app data>\campaigns\<campaign name>\CampaignRegistry.lua and the latter covers all campaigns and is stored in <FG app data>\GlobalRegistry.lua

Do a "find in files" in the CoreRPG ruleset for examples of how to store and access data in those LUA tables. A good example to look at is CampaignRegistry.sidebar which stores which buttons were displayed in the sidebar when the campaign exited.

JimSocks
November 5th, 2020, 23:10
Amazing. Thanks again good Sir!

I’ll be digging into that right after dinner!

JimSocks
November 10th, 2020, 05:56
So, I have found instances where the original code seems to write to the campaign registry through very simple syntax like:


CampaignRegistry.keyname = "value string"

But when I try to do this, it seems to have no effect, and nothing is added to CampaignRegistry.lua

I have to call it a night for now, but any suggestions you have would be greatly appreciated!

Trenloe
November 10th, 2020, 08:07
Have you exited the campaign and then checked the file contents? It doesn't write to the file when the campaign's running.

JimSocks
November 10th, 2020, 12:25
Have you exited the campaign and then checked the file contents? It doesn't write to the file when the campaign's running.

It doesn’t!? Well crap. That’s not gonna work anyways for what I need it for. Back to the drawing board.

I am trying to store a table of keys and values during an entire play session, even if the story template window is closed. I have tried putting the table outside of the onButtonPress() function (within CoreRPG/campaign/scripts/story_template_generate.lua) and even setting it as global, but for some reason it seems to be erased each time the storytemplate window is exited.

Do I maybe need to fiddle with it at the window class level in the xml? Hmm...

Trenloe
November 10th, 2020, 13:33
It doesn’t!? Well crap. That’s not gonna work anyways for what I need it for.
That's what I mentioned in my post above: "It's basically a set of LUA tables that get stored in a specific file when the campaign exits, and read back in when the campaign loads again." But that's just the file, the data is stored in memory. Only use CampaignRegistry if you need to persist data from one session to the next.


I am trying to store a table of keys and values during an entire play session, even if the story template window is closed. I have tried putting the table outside of the onButtonPress() function (within CoreRPG/campaign/scripts/story_template_generate.lua) and even setting it as global, but for some reason it seems to be erased each time the storytemplate window is exited.
Take a look at script block scope: https://fantasygroundsunity.atlassian.net/wiki/spaces/FGU/pages/4128914/Ruleset+-+Scripting#Script-Block-Scope

If a script is tied to a control or a windowclass then data in the associated scripts will only be available while that control/window is open. If you close it, the scope also closes and with it all of the variables and data from that scope.

If you want a set of variables to persist throughout a session then setup a global script package (declared in base.xml of a ruleset, or extension.xml of an extension), setup a data structure and store/read data to/from that package. DataCommon is an example global package used for storing data.

superteddy57
November 10th, 2020, 13:33
Then I would suggest a global LUA table. Something similar to DataCommon. You can keep this table static or insert or remove from the list to keep it ongoing.

Edit: Trenloe's response breaks it down better than I have. It didn't show when I was responding.

JimSocks
November 10th, 2020, 13:55
Thanks Trenloe/Superteddy56!

I totally read your words on that Trenloe- but I guess they didn’t register! My brain ain’t all that it used to be...

You can bet your bottom dollar I’ll be looking into datacommon today!

JimSocks
November 10th, 2020, 17:58
And BINGO- I have it figured out and working correctly!

Thanks guys!!! DataCommon ended up being EXACTLY what I needed!!!