Jump to content
Sign in to follow this  
urbanswelt

[solved] event.register - event.unregister

Recommended Posts

Hello again,

i would like use event.register and unregister for a cleaner coding, if a step is done ,  i need the function not any more.

But i have a "little" problem with that.

 

API doc says :

function OnUpdate_HelloWorld()  --Update Hello World secret variables.end event.register("OnUpdate", "OnUpdate_HelloWorld")--some time later on...event.unregister("OnUpdate", "OnUpdate_HelloWorld")

i mean normaly easy but in my case O.o

 

I have 2 Buttons, if i press the first Button the event is triggered and follow my logic. All is ok

With the second Button i like unregister the event but i obtain this failure:

 

Gizmo64: error: xac_lua/xac_window_XAutoCopilot.lua:170: event.unregister(..) - nothing done.: XAutoCopilot_btnStart_OnClick

Here is my coding for that:

 

function XAutoCopilot_btnPreparation_OnClick()    gui.hideWidget(XAutoCopilot.widprep2)    dref.setIntV( xac_state_preparation, 1, 1 )    event.register("OnUpdate", "OnUpdate_XAutoCopilot_btnPreparation")endfunction OnUpdate_XAutoCopilot_btnPreparation()-- many if / then coding hereendfunction XAutoCopilot_btnStart_OnClick()        gui.hideWidget(XAutoCopilot.widstart2)        event.unregister("OnUpdate", "OnUpdate_XAutoCopilot_btnPreparation")end

I hope anyone can pointing me in the right direction again ;-)

 

-- Firmware v14.01.12.2256

Greetings Lars

Edited by urbanswelt

Share this post


Link to post
Share on other sites

Lars,

 

I can see one reason that you may have a problem….  I don't think you can call event.unregister() before you have called event.register().  So, in your code, if you were to call "XAutoCopilot_btnStart_OnClick()" before "XAutoCopilot_btnPreparation_OnClick()" you will get an error.

 

Without having all the code, I can't see your entire logic, but, I don't see why you need to use event.register/event.unregister.  The way you have it structured the "unregister" is negated by the "register" and vice versa..  You could simply call the function "OnUpdate_XAutoCopilot_btnPreparation()" on the first button click.  If you are trying to "start" a process and then "stop"it, you would probably be better off setting some flags.

Edited by JGregory

Share this post


Link to post
Share on other sites

event.unregister(...) has not been implemented yet.

 

I don't clearly understand why you need/want to use it.

 

Can you describe what you're trying to achieve in more detail?

Share this post


Link to post
Share on other sites
Hi Ben, 
this explains the error message. 
My idea was it to, easier reading for other people. 
 
Currently I have a coding this runs on a single update loop. 
I have With the event.register the chance the whole clearer to structure. (perfect !)
With event.unregister would be the functions completely closed. 
In my view, for the following points: 
Calculations in functions are definitely death (saving resources) 
I would have a defined end 
For me, a kind of self-protection ;-) 
 
Of course I can everything with if / then reach. Therefore, I have no problem with that.
I hope you can follow me a little bit ;-)
So works my little brain ^^
 
@JGregory
Thank you also for helping with this "little" issue.
I am not really a Fan from posting the holly code about 1000 lines, and say then find my failure ^^
In this case the logic was perfect finished for me, but Ben has write now ... not implemented yet, so the problem was found ;-)
But here my little project: https://github.com/urbanswelt/XAutoCopilot i rewrite the coding at this moment (global/local declaration ...)

Thank you very much Ben and JGregory !
Edited by urbanswelt

Share this post


Link to post
Share on other sites

If you want to continue with event.register(...) you can do this:

 

OnUpdate_XAutoCopilot_btnPreparation = nil

 

 

The next time Gizmo attempts to call your function it will find nil instead and automatically add your function name to a list of functions to be ignored.

 

Timers are a little better if you want to re-use your function.

Setting = nil will work if you never need your function again.

Share this post


Link to post
Share on other sites

Hello Ben,

 

thank you very much again, the Solution with "nil" is a good idea. I use in this project a couple of timers and it is also a nice work with that ;-)
I am happy with Gizmo and say thank you again for your work ! ;-)

 

Greetings Lars

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×