Jump to content




computer shuts down on os.pullEventRaw() override

api computer lua

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

#1 cmdpwnd

  • Members
  • 128 posts
  • LocationHere

Posted 31 December 2015 - 08:49 PM

I'm trying to make a processManager of sorts and when I override os.pullEventRaw() the computer just shuts off immediately which gives me no time to see of any kind of error etc and then when i right click on the computer again it just boots back into craftOS. Any help appreciated.

--processManager.lua

pullEventRaw_Backup = os.pullEventRaw

local function _init_()
	if not _PM_RUN_STATUS then
		print('loading api')
		_PM_RUN_STATUS=1
		--define coroutines
		co_func = coroutine.create(func)
		return
	end
end

local function func()
	print('func is running')
	local received = {}
	for i=1,#interface.tInterfaceWrap do
		local event = {os.pullEventRaw()}
		if event and event[2] == "modem_message" and event[6] then
			table.insert(received,event[6])
		end
	end
	return received
end

function _G.os.pullEventRaw(sFilter)
	print('os.pullEventRaw is called')
  while true do
	local event = {pullEventRaw_Backup()}
	print('event pulled')
	if coroutine.status(co_func) == "suspended" then
		print('resuming coroutine')
	  coroutine.resume(co_func, table.unpack(event))
	end
	if sFilter == event[1] or not sFilter then
	  return table.unpack(event)
	end
  end
end

_init_()

CORRECT ANSWER HERE

Edited by jacky500, 07 March 2016 - 02:28 AM.


#2 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 31 December 2015 - 09:09 PM

I'm assuming that you know how to handle coroutines. Here are some things that make this piece of code weird and non-working:
  • _G.pullEventRaw_Backup is set to the first return value of an os.pullEventRaw call, there's no way it'll be a function you can call later in _G.os.pullEventRaw
  • _init_ is global
  • co_func is local and thus the coroutine created from func can never be referenced again, although you try to reference it in _G.os.pullEventRaw
  • func is global
  • received is local to the for loop inside func and thus func always returns nothing
Also, I have no idea what the table interface and the variable _PM_RUN_STATUS do. The way this code is now, I can't figure out what you're trying to achieve and can't really help.

Edited by LBPHacker, 31 December 2015 - 09:09 PM.


#3 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 31 December 2015 - 09:32 PM

Why do you even overwrite os.pullEvent? You can just have a function main with the while true do loop.

#4 cmdpwnd

  • Members
  • 128 posts
  • LocationHere

Posted 01 January 2016 - 05:59 AM

View PostLBPHacker, on 31 December 2015 - 09:09 PM, said:

I'm assuming that you know how to handle coroutines. Here are some things that make this piece of code weird and non-working:
  • _G.pullEventRaw_Backup is set to the first return value of an os.pullEventRaw call, there's no way it'll be a function you can call later in _G.os.pullEventRaw
  • _init_ is global
  • co_func is local and thus the coroutine created from func can never be referenced again, although you try to reference it in _G.os.pullEventRaw
  • func is global
  • received is local to the for loop inside func and thus func always returns nothing
Also, I have no idea what the table interface and the variable _PM_RUN_STATUS do. The way this code is now, I can't figure out what you're trying to achieve and can't really help.

Fixed.

_init_() is just a one time run function when the file itself is called on. PM_RUN_STATUS just lets that function know whether it should run or not.
tInterfaceWrap is a table that contains wrapped peripherals in the form: tInterfaceWrap[side] = peripheral.wrap(side) where side resolves to a string such as "top","bottom" etc.

#5 cmdpwnd

  • Members
  • 128 posts
  • LocationHere

Posted 01 January 2016 - 06:24 AM

Ok I fixed it. Thanks for the pointers, I was in such a hurry that I quickly threw it up here for some advice. I'll put the answer in a spoiler in the OP at the bottom.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users