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

Discuss any general programming issues here

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

Postby Bugala » Sun Aug 13, 2017 3:42 pm

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
Bugala
 
Posts: 645
Joined: Sun Feb 14, 2010 8:11 pm

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

Postby airsoftsoftwair » Tue Aug 15, 2017 8:37 pm

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.
User avatar
airsoftsoftwair
 
Posts: 2240
Joined: Fri Feb 12, 2010 3:33 pm
Location: Germany

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

Postby Bugala » Tue Aug 15, 2017 11:11 pm

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?
Bugala
 
Posts: 645
Joined: Sun Feb 14, 2010 8:11 pm

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

Postby airsoftsoftwair » Sat Aug 19, 2017 12:11 pm

No, I don't think there is because there are no pointers in Hollywood.
User avatar
airsoftsoftwair
 
Posts: 2240
Joined: Fri Feb 12, 2010 3:33 pm
Location: Germany


Return to General programming

Who is online

Users browsing this forum: No registered users and 4 guests

cron