roblox object event
v1.1.0
@rbxts/object-event
这个针对Roblox-TS的NPM软件包允许开发人员在无需诉诸BindableEvents
情况下实现自定义事件。
这对于严重依赖库和其他游戏组件的OOP范式的项目特别有用。
该模块可以在纯LUA(U)脚本(通过ROJO或任何其他方式)或Roblox-Ts中使用(包括编译时类型检查)。
/////// Module.ts ///////
import ObjectEvent from "@rbxts/object-event"
const event = new ObjectEvent < [ number , String , Vector3 ] > ( )
event . Connect ( ( id , msg , position ) => {
// We can safely assume that:
// id is a number
// msg is a string
// position is a Vector3
} )
export event
/////// Waiter.server.ts ///////
import { event } from "./Module"
event . Fire ( 10 , "oof" , new Vector3 ( 1 , 2 , 3 ) ) // all good
event . Fire ( 10 , "oof" ) // will not compile!
export { }
允许任何数字的各种参数:
let event = new ObjectEvent < [ ... unknown ] > ( )
ObjectEvent
APIConnect(f)
- f
是一个将相应键入的参数并返回void
函数。返回ObjectEventConnection
Wait()
- 产生线程直到事件发射为止。返回相应键入的值。Fire(...)
- 开火。参数必须是事件的数字和类型。Event
- RBXScriptSignal
类似于Connect()
和Wait()
的接口,以防万一您更喜欢将其用作bindableEvent。SubscribedConnections
一个ObjectEventConnection
s的数组,当前正在侦听事件的所有连接。 ObjectEventConnection
APIDisconnect()
- 与事件断开连接Reconnect()
- 恢复断开连接IsConnected()
- 如果连接正在收听事件(这不是disconnect()'ed),则返回true
,否则为false
。Event
- 与此连接关联的ObjectEvent
Listener
- 与此连接关联的功能