How can I share data between scripted objects?

Discussion about Mods for Prison Architect

Moderator: NBJeff

User avatar
Ghost314
level1
level1
Posts: 24
Joined: Thu Dec 24, 2015 9:23 pm

How can I share data between scripted objects?

Postby Ghost314 » Thu Dec 31, 2015 5:12 pm

I found myself in a position where I want one scripted object to communicate directly to another. In other words, I've got a game Object A with a lua script, and inside that script, I get a reference to a completely different type of game Object B, which has a totally different lua script, and I want the lua script attached to Object A to pass on some information to the lua script attached to Object B.

My first thought was to declare a global variable in one script and set it's value from the other, but this doesn't seem to work. Global variables declared in Object B seem to be invisible to Object A, even when using the 'explore' option in the debugger window. I'm guessing this has something to do with lua scripts running in isolated environments?

Not wanting to do anything as complicated as opening sockets or storing data in files, I thought the next best option might be to get/set the value of an existing object property that's not being used for anything else by the objects in question, but that's cryptic as all hell. I'm curious if anyone has a better solution for this problem.
User avatar
Brento666
level3
level3
Posts: 290
Joined: Wed Sep 02, 2015 9:23 pm

Re: How can I share data between scripted objects?

Postby Brento666 » Tue Jan 05, 2016 8:39 pm

Heya Ghost,

Meant to reply sooner, but had this tab open with about 30k other tabs :mrgreen:

-Simple types like strings and floats can be r/w by both internal and external scripts;

Example sequence that sure does works for me;

Code: Select all

object 1: this.Enabled = true

object 1: if this.Enabled then --checks as true

object 2 with ref to obj 1: if obj1.Enabled then --now checks out as true..

object 2 with ref to obj 1: obj1.Enabled = nil

object 2 with ref to obj 1: if obj1.Enabled then --now checks out as false..

also object 1: if this.Enabled then --now checks as false.


Hope that illustrates that simple types can be shared; number/Boolean/string values are no problem at all..!

Just don't try and share(/store) nested lists...

ps. Also the simple type (non local) variables are serializable (so they persist after reload).
Whereas nested lists do NOT serialize(!)

Hope that translates and helps, let me know if something is unclear!
User avatar
Ghost314
level1
level1
Posts: 24
Joined: Thu Dec 24, 2015 9:23 pm

Re: How can I share data between scripted objects?

Postby Ghost314 » Sun Jan 31, 2016 6:32 pm

I finally got back around to doing this, my original mistake was attempting to declare global variables in one script and use them in another like so:

Code: Select all

someVar = 5

instead I needed to use:

Code: Select all

this.someVar = 5

since you mentioned that nested lists were a problem, I figured I would be able to share a simple one dimensional array between objects, but from what I've learned about lua regarding arrays, it seems that all arrays in lua are basically tables, and even a simple one entry table doesn't seem to work:

Code: Select all

this.someArray = {}

function Create()
   this.someArray['x'] = 5
end

This fails in-game telling me that someArray is a nil value, but when defined as a local, it works without issue. I thought I might be able to work around the issue by having a local table and sharing a function or coroutine type that does something with the table, but both of these fail in a similar manner.

What I would really like to do at this point, is have a single table, that can be accessed and modified by all of my lua scripts. I was planning to do this by creating a single instance of an object with no visible game sprite, attaching a script to it, and having that script define a table that other scripts can see. Is there perhaps an easier way to go about doing this? Can I somehow define a new table in a global scope that other scripts can pick up on? If not then the only way I might be able to accomplish this is by declaring a bunch of simple types meant to represent elements of a table with a fixed size.

Return to “Modding”

Who is online

Users browsing this forum: No registered users and 5 guests