This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
rahph's profile picture

Defining custom events

Started by rahph, 23 March 2015 - 09:44 AM
rahph #1
Posted 23 March 2015 - 10:44 AM
Hello.
I've been working on new object API (that will add buttons etc.) and i want it to be easy in use. I mean:
Making button:

obj.createButton(top left corner: x cord, y cord, down right corner: x cord, y cord, text on button, color (0-15),id)
The problem is, that i do not know how to make custom event (i mean API user's code waits with event:

local evnt = { os.pullEvent("button_click") }
When this event code is waiting, my API waits for normal mouse click on button using

mouse_click
So how to make that custom event? Can it use multiple parameters? IS IT EVEN POSSIBLE? Oh. and can API access local variables in code that uses it?

Example code:

os.loadAPI("obj")
os.loadAPI("ccb") --another api i am working at. it is used to draw custom size box. ya know? buttons mostly base on it. (drawing button). also this api also prints text on box. so obj api is type of api that uses another api.
obj.createButton(1,1,5,5,test,4,3)
while true do
local evnt = { os.pullEvent("button_click")
if evnt == 3 then --tests for click of button with id 3
  term.setCursorPos(8, 8)
   print("writed")
  sleep(5)
  term.clear() --term.clear() clear entire terminal. it also HIDES button. You can always reviwe button with it's old parameters (cords,id,text,color) by using obj.reviweButton(id)
  obj.reviweButton(3)
end
end


Edited on 23 March 2015 - 10:02 AM
Bomb Bloke #2
Posted 23 March 2015 - 11:28 AM
os.queueEvent()

Oh. and can API access local variables in code that uses it?

Not directly, no, though the "using" code can pass data to the API by passing it as parameters when calling said API's functions.

(Or I suppose you could push the data through via custom events, too, if you wanted.)
Edited on 23 March 2015 - 10:31 AM
rahph #3
Posted 23 March 2015 - 12:02 PM
os.queueEvent()

Oh. and can API access local variables in code that uses it?

Not directly, no, though the "using" code can pass data to the API by passing it as parameters when calling said API's functions.

(Or I suppose you could push the data through via custom events, too, if you wanted.)
So how to use os.queueEvent? i don't understand most of tutorials
Lupus590 #4
Posted 23 March 2015 - 12:16 PM
http://computercraft.info/wiki/Os.queueEvent

os.queueEvent(eventName, param1, param2, param3)
where the paramitors (spelling) lines up directly with:
eventName, param1, param2, param3 = os.pullEvent()
Bomb Bloke #5
Posted 23 March 2015 - 12:29 PM
You'll also want to investigate the parallel API. You'll need your "event generating" code running in a coroutine alongside your "event listening" code in order to get the two to play together.