Carpet

Carpet

2M Downloads

Custom Events?

SnaveSutit opened this issue ยท 11 comments

commented

Basically you could define an event, that would defined by a script. Then any other scripts that listen for that event would be triggered when that event is triggered.
Something like
trigger_event('my_event')
would cause all listening scripts to run their set function:
set_event_listener('my_event', 'func')
Where 'my_event' is the event name, and 'func' is the function to run when the event is triggered.

If an event isn't defined, the listener never fires, so no need to check if an event exists when setting a listener

commented

I take it back, this is as of now not possible.

commented

good idea. Smells like interrupts. Sounds like a plan :)

commented

Would it be possible to include custom event parameters, as in trigger_event('my_event', my_params)? This could be used instead of having to define lots of very similar events, just define one event with one or more parameters. The event listener would get the parameters that were passed in the event trigger function, as in func(my_params) instead of just func().

A practical example: A library app could be installed that gets the proper tool to mine a given block type, which would use the custom event parameters to receive the block and to send data regarding the proper tool to mine the block.

commented

no need. Just do __on_player_clicks_block then based on the block, swap out the player's hand item for the correct tool

commented

should I consider it fully as an RPC system? What about return value? I dont believe apps should be blocked by these. Worst case scenario you will have a callback with a response on the first app side.

commented

@gnembon This is already possible man. You just have an app, say event_listener.sc, in which you define global_event, which is a boolean, true for however many ticks that that event is happening, or a number which reflects the event. Depends on the event. Then you do in event_user.sc import('event_listener','global_event'), and to use it do if(global_event=eventval,do_Stuff) where eventval is whatever value global_event has when the event is happenning. You can then call that if statement in __on_tick() so that you can check evry tick if its happening

commented

@Ghoulboy78 honestly, if it can be done as an external script so easily its better to have it by default :P

commented

@ImUrX No real point. Cos what specifically would you add? This method only works if you have multiple scripts at the same time. ALL you could add exra would be the trigger_event('name') and __on_event('name') or something to trigger whenever that event is triggerred.
But if you're in the same script, you just do the trigerring and listening in the same script, so if the condition happens, you just call the function. Generally you wouldn't need multiple scripts, especially as the scripts have got effectively unlimited length. It is unnecessary.

Tell me where you would need multiple scripts to acheive what you couldn't by just putting all the code into one large .sc file and properly spacing with comments?

commented

very nice, this issue should get closed

commented

Added in 5564363 with handle_event and signal_event.

See the Docs

commented

yep, its done and its glorious.