Jump to content




os.queueEvent - queuing tables


  • You cannot reply to this topic
7 replies to this topic

#1 grabie2

  • Members
  • 79 posts
  • LocationPoland

Posted 23 January 2013 - 07:04 AM

I went into strange problem, when I queued event with strings and tables, in os.pullEventRaw it returned strings and ALL tables were nil. Could someone explain to me why it doesn't work, maybe there is a solution(I don't want to do textutils.serialize, sometimes tables contain functions WITH upvalues)

thanks for replies!

#2 ChunLing

  • Members
  • 2,027 posts

Posted 23 January 2013 - 08:19 AM

Code.

#3 Eric

  • Members
  • 92 posts
  • LocationUK

Posted 23 January 2013 - 09:06 AM

When you use queueEvent, you pass the arguments through to the java code, then they bubble back through. Unfortunately, it is impossible to send tables in this way.

There are a number of ways to solve this. One way would be to wrap queueEvent and pullEvent in lua code that maintains an internal queue, and works with the couroutine API. I think this is possible - have a look at the source code for the parallel API to get ideas.

The easier solution is to just store metadata elsewhere, and use the standard event system to pass a key to that metadata.

eventExtraData = {}
id = 1
function queueMyEvent(...)
	eventExtraData[id] = {...}
	os.queueEvent("my event", id)
	id = id + 1
end



function pullMyEvent()
	local event = {os.pullEvent("my event")}
	local id = event[2]
	local data = eventExtraData[id]
	eventExtraData[id] = nil
	return "my event", unpack(data)
end


#4 grabie2

  • Members
  • 79 posts
  • LocationPoland

Posted 23 January 2013 - 09:52 AM

Thanks Eric, I thought that this is the case, but I used Lua only with C api, and that wasn't problem.
But instead of FILO queue that you shown I'll use FIFO, that'll be better for event queue ;)

Again thanks for quick response and help!
(Thread can be closed)

#5 Eric

  • Members
  • 92 posts
  • LocationUK

Posted 23 January 2013 - 09:58 AM

View Postgrabie2, on 23 January 2013 - 09:52 AM, said:

But instead of FILO queue that you shown I'll use FIFO, that'll be better for event queue ;)
Are you sure you thought that through? This may not apply to your case, but think about these events:
char f
char a
char i
char l
And a piece of code that gets them one by one and prints. With a FILO (a queue, hence queueEvent()), this will print "fail", but with a FIFO (a stack), you get "liaf".

#6 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 23 January 2013 - 10:06 AM

View PostEric, on 23 January 2013 - 09:58 AM, said:

View Postgrabie2, on 23 January 2013 - 09:52 AM, said:

But instead of FILO queue that you shown I'll use FIFO, that'll be better for event queue ;)
Are you sure you thought that through? This may not apply to your case, but think about these events:
char f
char a
char i
char l
And a piece of code that gets them one by one and prints. With a FILO (a queue, hence queueEvent()), this will print "fail", but with a FIFO (a stack), you get "liaf".

I disagree.... you press f-a-i-l and it queues "f" first, then "a", then "i", then "l", FIFO means "f" comes out first 'cos it went in first so you keep the order of events.

#7 Eric

  • Members
  • 92 posts
  • LocationUK

Posted 23 January 2013 - 10:15 AM

Derp. I was confused by grabie calling my code a FILO when it is in fact a fifo.

#8 grabie2

  • Members
  • 79 posts
  • LocationPoland

Posted 23 January 2013 - 10:17 AM

Oww right, derp.
I looked at your code and somehow seen FILO queue, derp.
Sorry for that.
(Thread can be closed)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users