Share variables between scripts

Hello,

Is it possible to share variables between scripts ?
Not parameters, but single variables, or tables.

Yes, using require.

With require i could share only the the names, but not the run-time values.
For example,
i defined a table in a script
loaded it with require on two independent scripts, that each of them run its own update()
if i change the data from the shared table inside one script - it doesn’t change in another.
Is it expected behaviour?

I think that is indeed expected behavior. To truly share values, one script needs to be “required” as a module to the other (creating the dependency).

The module script could still run scheduled callbacks, but you can’t require a module twice in independent instances and expect global state to persist, so far as I know.

1 Like

Is it possible to enable a truly data sharing behaviour ?

I’m just trying to overcame the limitation that i can’t schedule asynchronously two different functions in the same script, and i tried to run my tasks in separate scripts, but in that case i need to share some data between them.
And now i got stuck also in this direction.

I think you can overcome these issues with the constructs in place. Perhaps do some research on Lua coding practices in general.

Writing a basic scheduler is quite simple. Then external variables are shared.

tasks={}
local val=0
function tasks:A()
print("A: ", val )
val=val+1
end
function tasks:B()
print("B:", val)
end

local function Update()
for k,v in pairs(tasks) do
v()
end

IIRC this won’t work with require.

Or consider separating the business logic of each script into its own module and then using a master/controller script that requires both and schedules the tasks as applicable.

Pass the shared data as function arguments.

That is potentially safer, and better for encapsulation but there is a lot of possible leakage due to pass by reference for tables that I would consider going for explicit shared states as they can be more obvious when debugging.

Thank you.
I know this approach. It it not really asynchronous.
All tasks run on same context (update).
I have to run fast, so i need that the other tasks will not use the same time slot, but got their own.

What you mean Master/Controller script?

Running task with different frequencies isn’t that hard, just way more text than I was willing to write on the phone :sweat_smile:. You could use coroutines if implemented to facilitate yielding for long running tasks.

As far as i know, coroutines not implemented in Ardupilot LUA.

Whatever it is that you’re trying to do won’t be faster by splitting it into multiple scripts. I don’t think we multi-thread Lua processing time.

Run a single script at the fastest update needed, and trigger each bit of logic when needed. That’s the best you’re gonna do.

That’s the key question.
Can you check it please?
A i said, i need to run fast, 75-100 Hz, an in this case I’m afraid to exceed the cycle time if I’ll run all my tasks on the same context.

You’ll be lucky to run significant tasks at that rate, and they won’t be faster in separate scripts.

You don’t have to run all tasks on the same schedule, but I think you’re going to want a single script if you want any chance of success.

Rather than making me do your homework (which is impossible anyway since you’re being vague, which makes me skeptical to provide support), why don’t you just try it?

1 Like

I will definitely try.
I just thought i could save some time, by getting answers from experienced developers here.