Is evt_tables func copy of func, or referen of func or what?

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

Is evt_tables func copy of func, or referen of func or what?

Post by Bugala »

I am trying to find out about innerworking of how Makebuttons evt_table works as I have trouble doing something.

What I am trying to do is following:

Code: Select all

myproto_obj = {}

Function myproto_obj:New()
	Local d = {}
	SetMetaTable(d, self)
	self.__index = self	
	Return(d)
EndFunction


function myproto_obj.LeftMouseDown(msg)
debugprint("default leftmousedown")
endfunction


function myproto_obj:AssignFuncToLeftMouseDown(func)
      self.LeftMouseDown = Func
endfunction

function func1 ()
debugprint("func1")
endfunction


MyObj = myproto_obj:New()

MakeButton(Nil, #SIMPLEBUTTON, 100, 100, 1000, 1000, {OnMouseDown=MyObj.LeftMouseDown})

MyObj:AssignFuncToLeftMouseDown(func1)

waitevent()

MyObj:LeftMouseDown()
What I am expecting to happen now when i click on this rectangle area is for "Func1" being printed twice: First from the waitevent event activation, and second from "MyObj:LeftMouseDown()" line of code at end of program.

However, what happens is that from event activation it prints "default leftmousedown" and then from the last line the expected "Func1"

Hence, indeed it does copy the new function (func1) on top of the old "myobj.leftmousedown" function, but when the button makes a call, instead of referencing towards the current "MyObj:LeftMouseDown" it is still referring to the old one, i suppose to "Myproto_obj.LeftMouseDown" function.

Is there a way to make makebuttons evt_table to go to the most current myobj.leftmousedown function, or do i have to make two functions to get it done? ie.

Code: Select all

function myobj.letfmousedown(msg)
myobj.therealleftmousedown(msg)
endfunction

function myobj.therealleftmousedown(msg)
debugprint("default left mouse down")
endfunction

function func1()
debugprint("func1")
endfunction

myobj.therealleftmousedown = func1
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Is evt_tables func copy of func, or referen of func or w

Post by airsoftsoftwair »

Bugala wrote: However, what happens is that from event activation it prints "default leftmousedown" and then from the last line the expected "Func1"
That's normal behaviour. At the time when you call MakeButton() MyObj.LeftMouseDown points to the function that prints "default leftmousedown". The other assignment happens after you call MakeButton() so MakeButton() cannot magically know the new reference.
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: Is evt_tables func copy of func, or referen of func or w

Post by Bugala »

ah, i see now. I had misunderstood here how referencing works and thought this was unique to evt_tables only.

Code: Select all

a = {text="a"}
b = a
c = b
b = {text="b"}
DebugPrint(c.text)
I always thought this would result in "b" being printed, but it is actually going to print "a".

I guess there is no other way then for me to achieve "b" being printed, than having that double function where evt_table refers to a Function A which executes Function B and then i can keep changing function B to achieve the "b" being printed. or is there?
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Is evt_tables func copy of func, or referen of func or w

Post by airsoftsoftwair »

No, I don't think there is because there are no pointers in Hollywood.
Post Reply