dispatchEvent not working and crashing

i am trying this code to dispatch events

================================================
local temp = display.newGroup()

local something = function(event)
print(“here !!”)
end

temp:addEventListener(“aaa”,something)

local event = {name=“aaa”,target = temp}
timer.performWithDelay (1000, function () temp:dispatchEvent(event) end)

when I run the code I get this error:


Runtime error
?:0: attempt to index a nil value
stack traceback:
[C]: ?
?: in function ‘?’
?: in function <?:214>

What is going on?

Regards
elvan
[import]uid: 12167 topic_id: 8500 reply_id: 308500[/import]

I was getting that error too in instances where I had an event dispatching inside of another event handler.

But I think your issue might be scoping, it’s confusing your custom event with the timer event. Try changing the name of your custom event object to “myEvent” and pass that in the closed timer function. If you still get the error, maybe there’s a bug with dispatching events inside of event handlers.

local myEvent = {name="aaa",target = temp} timer.performWithDelay (1000, function(myEvent) temp:dispatchEvent(myEvent) end) [import]uid: 4596 topic_id: 8500 reply_id: 30457[/import]

that’s not right either. your myEvent inside the function(…) is a parameter not the original myEvent

you can test that here:
[lua]timer.performWithDelay (1000, function (myEvent) print("name: "…tostring(myEvent.nzme)) end) [/lua]

you will get nil, you should get “aaa”

it doesnt work if you remove myEvent as the function parameter either [import]uid: 6645 topic_id: 8500 reply_id: 30502[/import]

Nested handler?

[code]
local function onTimer( event )
local myEvent = {name=“aaa”,target = temp}
temp:dispatchEvent(myEvent)
end

timer.performWithDelay (1000, onTimer )
[/code] [import]uid: 4596 topic_id: 8500 reply_id: 30503[/import]

function runs, but ends up with this

[lua]Runtime error
?:0: attempt to index a nil value
stack traceback:
[C]: ?
?: in function ‘?’
?: in function <?:214>[/lua]

in all examples :S [import]uid: 6645 topic_id: 8500 reply_id: 30504[/import]

Sounds like a bug to me [import]uid: 4596 topic_id: 8500 reply_id: 30505[/import]

It gets weirder…please try:

[lua]local temp = display.newGroup()

local something = function(event)
print(“here !!”)
end

temp:addEventListener(“aaa”,something)
temp.aaa = something; temp:addEventListener(“aaa”,temp) – add table event listener
temp:addEventListener(“aaa”,something)
temp:addEventListener(“aaa”,something)

local event = {name=“aaa”,target = temp}

timer.performWithDelay (1000, function (e) temp:dispatchEvent(event) end)[/lua]

No errors as long as you add a table event listener to the mix…

Going thru some permutations, it doesn’t seem to matter whether you add the normal function handler before of after the table event listener - as long as the latter is there all works fine.

Very intriguing bug…

-FrankS.

[import]uid: 8093 topic_id: 8500 reply_id: 30636[/import]

you don’t need all that… just

[lua]local temp = display.newGroup()

local something = function(event)
print(“here !!”)
end

temp.aaa = something;
temp:addEventListener(“aaa”,temp) – add table event listener

local event = {name=“aaa”,target = temp}
timer.performWithDelay (1000, function (e) temp:dispatchEvent(event) end) [/lua] [import]uid: 6645 topic_id: 8500 reply_id: 30641[/import]

and for your initial example
[lua]local temp = display.newGroup()

local something = function(event)
print(“here !!”)
end

temp.aaa=something
temp:addEventListener(“aaa”,temp)

local event = {name=“aaa”,target = temp}
timer.performWithDelay (1000, function () temp:dispatchEvent(event) end)[/lua]

odd bug, but solution kind of makes sense, as it follows the standard pattern we’ve seen for collision listeners etc
[lua]obj.collision = someFunction
obj:addEventListener(“collision”, obj)[/lua] [import]uid: 6645 topic_id: 8500 reply_id: 30643[/import]

Sorry that I wasn’t clear in my example.

The fact that it works with a table event listener and not with a function event listener is weird.

The fact that it works with both a table event listener and a function event listener as long as the table event listener is also registered, is weirder. This implies that the function event handler doesn’t crash anymore when the table listener is registered…

That was what I was trying to show in my example and hopefully that will help Ansca to fix it, because the weirder the behavior, the more obscure the bug… :wink:

-FrankS.
[import]uid: 8093 topic_id: 8500 reply_id: 30645[/import]

I’m not seeing this bug until I try to build for iOS only (Android builds are fine).

No bugs in the simulator, but the build process comes up with

Runtime error  
 ?:0: attempt to index a nil value  
stack traceback:  
 [C]: ?  
 ?: in function '?'  
 ?: in function '?'  
 ?: in function <?:419>  

There are only 2 files with at least 419 lines, and neither of those lines try to index anything!

Any ideas?

If this is a bug, has it been reported? [import]uid: 52386 topic_id: 8500 reply_id: 37008[/import]