Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lua: Source - sharing variables and init call sequence #4411

Closed
Alex00101 opened this issue Aug 20, 2024 · 10 comments
Closed

Lua: Source - sharing variables and init call sequence #4411

Alex00101 opened this issue Aug 20, 2024 · 10 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@Alex00101
Copy link

Alex00101 commented Aug 20, 2024

I'm experiencing a "strange" behavior in how Ethos is handling lua source - maybe this is as designed but does cause underised effects.

My source shell (simplified to illustrate the problem) looks like

local counter = 0

local function sourceinit(source)
    print("Init called:", counter )   
end

local function read(source)
    counter = storage.read("Some Key")
    print("Read called:", counter )   
end

local function write(source)
   -- saves counter to storage, works as expected
   storage.write("Some Key",counter)
end

local function wakeup(source)         
end

local function configure(source)
 -- adds Number edit to set the counter, works as expected, code skipped)
end

local function init()
    system.registerSource({key=THE_KEY, name=THE_NAME, init=sourceinit, wakeup=wakeup, 
                                configure = configure, read=read, write = write })
end

1st, when I start Ethos (sim or debug), the output is:

Init called 0
Read called: 0 -- or error on the new model, expected
Init called 0 -- second time?

So init is called two times. This is not a big deal on its own however with multiple model using the same source there is a problem.
I need counter to have default value of 0 for every new model. I create 1st model, run the source logic or use configure() to set the counter to say 10. All works as expected, 10 get saved to the model.bin file and retrieved on next model start.

Now I create the 2nd model (via wizard, not cloning). The source is disabled by default. So I go to the model settings/Lua and enable it. What I have as an output is:

Init called 10

As you may see the counter value was inherited from the previous model. My next step was to set it to default 0 in init() but then I bump into the issue that init is called 2 times, 2nd one after read so this overrides the stored value. I could also initialize counter with default on read (before storage.read) but than as read is not called when I'm enabling the source, so my default value is back to 10, while I need 0.

Is there workaround I am missing or perhaps the init should not be called 2 times?

Many thanks

@strgaltdel
Copy link

strgaltdel commented Aug 20, 2024

.. can you rule out that another script in the init handler also contains a “print(”Init called:”, counter )” statement ?

all init handlers are executed during boot, so another statement could explain your observation on startup

@Alex00101
Copy link
Author

Yes, ruled out. When I comment this print() out this output is no more.

@bsongis-frsky
Copy link
Collaborator

Your variable counter belongs to the script, not to the source. So it is common to all models using the same source!

@Alex00101
Copy link
Author

Alex00101 commented Aug 21, 2024

Understood. Is it possible then to fix the init call sequence so the init of the source is called only once when the model is loaded? Thanks

@bsongis-frsky
Copy link
Collaborator

bsongis-frsky commented Aug 21, 2024

init could be called a lot, as it is called as soon as the source must be reset (like flight reset with a switch)

@bsongis-frsky
Copy link
Collaborator

I am starting to think about splitting init into create and reset functions

@Alex00101
Copy link
Author

That would be great, or any other option that would allow me to initialise source per model. I understand there are likely other priorities but TBH I'm somewhat stuck with this. Really appreciate if you find time to improve this....

@bsongis-frsky bsongis-frsky self-assigned this Aug 21, 2024
@bsongis-frsky bsongis-frsky added this to the 1.5.14 milestone Aug 21, 2024
@bsongis-frsky bsongis-frsky added the enhancement New feature or request label Aug 21, 2024
@Alex00101
Copy link
Author

Thanks!

@bsongis-frsky
Copy link
Collaborator

I have just introduced a new function reset which is called on flight reset. Then the Lua source init is just called one time on model load

@Alex00101
Copy link
Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants