Jump to content




Defining custom events


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

#1 rahph

  • Members
  • 81 posts

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 by kok, 23 March 2015 - 11:02 AM.


#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 23 March 2015 - 11:28 AM

os.queueEvent()

View Postkok, on 23 March 2015 - 10:44 AM, said:

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 by Bomb Bloke, 23 March 2015 - 11:31 AM.


#3 rahph

  • Members
  • 81 posts

Posted 23 March 2015 - 12:02 PM

View PostBomb Bloke, on 23 March 2015 - 11:28 AM, said:

os.queueEvent()

View Postkok, on 23 March 2015 - 10:44 AM, said:

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

#4 Lupus590

  • Members
  • 2,028 posts
  • LocationUK

Posted 23 March 2015 - 12:16 PM

http://computercraft...i/Os.queueEvent

os.queueEvent(eventName, param1, param2, param3)
where the paramitors (spelling) lines up directly with:
eventName, param1, param2, param3 = os.pullEvent()

#5 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

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.





3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users