Any way to save current eventhandler option?

Discuss any general programming issues here
Post Reply
Bugala
Posts: 825
Joined: Sun Feb 14, 2010 7:11 pm

Any way to save current eventhandler option?

Post by Bugala » Wed Oct 02, 2019 11:35 am

What I would like to do is following:

Code: Select all

InstallEventHandler(OnMouseUp, Function () Resume EndFunction)
What however worries me is that maybe I have another Eventhandler already done and now this would be replacing the old one, even I didnt mean to, since this one is meant to be in use only temporarily.

Therefore what I would need is something like this:

Code: Select all

funcforsafekeeping = CurrentEventHandler(OnMouseUp)

InstallEventHandler(OnMouseUp, Function () 
					InstallEventHandler(OnMouseUp, funcforsafekeeping)
					Resume() 
			       EndFunction)
Is there any way to do this, and if not, then for a wish list an option to see and save what is current func for some event like OnMouseUp.

User avatar
Clyde
Posts: 198
Joined: Sun Feb 14, 2010 12:38 pm
Location: Dresden / Germany

Re: Any way to save current eventhandler option?

Post by Clyde » Wed Oct 02, 2019 2:05 pm

Well, I am unsure about the syntax you use, but if you do it like it is said in the manual you could do things like this:

Code: Select all

Function callbackFunction1()

EndFunction

Function callbackFunction2()

EndFunction

InstallEventHandler({ OnMouseUp = callbackFunction1 })

; And later you would simple write:

InstallEventHandler({ OnMouseUp = callbackFunction2 })
Currently using: Hollywood 8 with Windows IDE

Bugala
Posts: 825
Joined: Sun Feb 14, 2010 7:11 pm

Re: Any way to save current eventhandler option?

Post by Bugala » Wed Oct 02, 2019 4:25 pm

Otherwise yeah, but then my class wouldnt be independent anymore. I am trying to keep that class independent so I can move it from one program to another. If I have to keep track of it manually, then I might forget to do that one time, and will have great difficulty in figuring out why something isnt working the way it is supposed to be.

That is why I like to have a system to store the current OnMouseUp function, so I dont need to worry what function it is using before, that it can be using whatever before and will be replaced back to it after the temporary use.

SamuraiCrow
Posts: 242
Joined: Fri May 15, 2015 5:15 pm
Location: Ft. Collins, Colorado USA

Re: Any way to save current eventhandler option?

Post by SamuraiCrow » Wed Oct 02, 2019 9:11 pm

Isn't an event just a table variable? You should be able to pass that in as a parameter or assign it to another variable as needed. You should even be able to insert it into another table!

Bugala
Posts: 825
Joined: Sun Feb 14, 2010 7:11 pm

Re: Any way to save current eventhandler option?

Post by Bugala » Wed Oct 02, 2019 9:55 pm

Thats what I think too, but how?

for when using command:

Code: Select all

InstallEventHandler(  {  OnMouseUp = function  }  )
There is clearly table going there, but where is the table?

it looks like it could be accessible through TableX.OnMouseUp, but what is the TableX?

SamuraiCrow
Posts: 242
Joined: Fri May 15, 2015 5:15 pm
Location: Ft. Collins, Colorado USA

Re: Any way to save current eventhandler option?

Post by SamuraiCrow » Thu Oct 03, 2019 8:05 am

It's the first parameter passed into the event handler. See https://hollywood-mal.com/docs/html/hol ... ndler.html for details.

Bugala
Posts: 825
Joined: Sun Feb 14, 2010 7:11 pm

Re: Any way to save current eventhandler option?

Post by Bugala » Thu Oct 03, 2019 8:47 am

I still don't see how I can get it if I don't know it?

That lets put it like this, first there is this part of code that I cant access nor know:

Code: Select all

rando = rnd(2)
if rando=0
	InstallEventHandler({OnMouseUp=func1})
else
	InstallEventHandlet({OnMouseUp=func2})
endif
supposing I have no access to "rando", I have no knowledge if OnMouseUp has func1 or func2.

So at the point I arrive at this point:

Code: Select all

OnMouseUpTemporarilySaved = OnMouseUp
Installeventhandler(OnMouseUp = Function ()
						debugprint("this is just for temporary use, used once")
						InstallEventHandler({OnMouseUp = OnMouseUpTemporarilySaved})
				EndFunction
I would need to have a way to save the OnMouseUp function, like in that first line, but how do I access this OnMouseUp function to save it, since by just using OnMouseUp is obviously not working. It has to be some sort of tableX.OnMouseUp would be my guess, but what is the name of the tableX?

SamuraiCrow
Posts: 242
Joined: Fri May 15, 2015 5:15 pm
Location: Ft. Collins, Colorado USA

Re: Any way to save current eventhandler option?

Post by SamuraiCrow » Thu Oct 03, 2019 6:44 pm

@Bugala

Have you considered making a wrapper function your handler instead and having it keep track of the functions yourself?

Bugala
Posts: 825
Joined: Sun Feb 14, 2010 7:11 pm

Re: Any way to save current eventhandler option?

Post by Bugala » Thu Oct 03, 2019 8:20 pm

hmm.. That is an interesting thought.

I was about to settle for another solution, but now that you mention that, going to give some thought about that one, for that would also solve one problem why i was about to move away from the original idea of saving OnMouseUp temporarily.

Bugala
Posts: 825
Joined: Sun Feb 14, 2010 7:11 pm

Re: Any way to save current eventhandler option?

Post by Bugala » Fri Oct 04, 2019 8:10 am

In case someone needs the possibility to save or even manually start one of the event functions, here is a small test based upon SamuraiCrows suggestion:

Code: Select all

global t_EventHandlers = {}
funccopy = InstallEventHandler
InstallEventHandler = Function (table)
	ForEach(table, Function (ID, item)
		t_EventHandlers[ID] = item
		       EndFunction)
	funccopy(table)

            EndFunction

InstallEventHandler( {OnMouseUp = Function () DebugPrint("a test") EndFunction} )
t_EventHandlers.OnMouseUp()
A little explanation of the code.

I am first making a global table called t_EventHandlers. The purpose of This table is to have every events function stored.

Next I make a copy of the function (InstallEventHandler), this is necessary since if I use InstallEventHandler directly at a later point, it will end in an endless recursive loop (=calling itself endless times).

Then comes finally the time where I replace the current InstallEventHandler function with my own version.

Notice that basically all commands in Hollywood are functions, and you can replace them with your own, and this is what I am doing here, although in this case I wouldn't really call it replacing the command, but rather expanding it, since I include the old command to the new command as well.

First I will check all the table items that come with the event handler, and put them to the t_EventHandlers table.
As an example, there is now "OnMouseUp = Function" this means there will be t_EventHandlers.OnMouseUp existing after that. similarly there could be t_EventHandlers.OnMouseDown or anything else of Hollywoods Events.

After this part is done, I am making a call to a funccopy (which is a copy of original InstallEventHandler) using the same arguments user passed to my InstallEventHandler.

Then it is time for a test.

I use InstantEventHandler to make one OnMouseUp option with a function that simply debugprints "a test".

then I call this function directly by calling it from that t_EventHandlers table, and it works.

Post Reply