RBXScriptSignal
This commit is contained in:
parent
da169ade70
commit
14fdcd630e
@ -7,7 +7,8 @@ mod color3;
|
|||||||
mod cframe;
|
mod cframe;
|
||||||
mod vector3;
|
mod vector3;
|
||||||
pub mod instance;
|
pub mod instance;
|
||||||
mod number_sequence;
|
mod script_signal;
|
||||||
mod color_sequence;
|
mod color_sequence;
|
||||||
|
mod number_sequence;
|
||||||
|
|
||||||
pub use runner::{Runner,Runnable,Error};
|
pub use runner::{Runner,Runnable,Error};
|
||||||
|
79
src/runner/script_signal.rs
Normal file
79
src/runner/script_signal.rs
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
use std::{cell::RefCell,rc::Rc};
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct ScriptSignal{
|
||||||
|
// Emulate the garbage roblox api.
|
||||||
|
// ScriptConnection should not exist.
|
||||||
|
// :Disconnect should be a method on ScriptSignal, and this would be avoided entirely.
|
||||||
|
callbacks:Rc<RefCell<Vec<mlua::Function>>>,
|
||||||
|
}
|
||||||
|
pub struct ScriptConnection{
|
||||||
|
signal:ScriptSignal,
|
||||||
|
function:mlua::Function,
|
||||||
|
}
|
||||||
|
impl ScriptSignal{
|
||||||
|
pub fn new()->Self{
|
||||||
|
Self{
|
||||||
|
callbacks:Rc::new(RefCell::new(Vec::new())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// This eats the Lua error
|
||||||
|
pub fn fire(&self,args:mlua::MultiValue){
|
||||||
|
// Make a copy of the list in case Lua attempts to modify it during the loop
|
||||||
|
let functions=self.callbacks.borrow().clone();
|
||||||
|
for function in functions{
|
||||||
|
//wee let's allocate for our function calls
|
||||||
|
if let Err(e)=function.call::<mlua::MultiValue>(args.clone()){
|
||||||
|
println!("Script Signal Error: {e}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn connect(&self,function:mlua::Function)->ScriptConnection{
|
||||||
|
self.callbacks.borrow_mut().push(function.clone());
|
||||||
|
ScriptConnection{
|
||||||
|
signal:self.clone(),
|
||||||
|
function,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl ScriptConnection{
|
||||||
|
pub fn position(&self)->Option<usize>{
|
||||||
|
self.signal.callbacks.borrow().iter().position(|function|function==&self.function)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl mlua::UserData for ScriptSignal{
|
||||||
|
fn add_methods<M:mlua::UserDataMethods<Self>>(methods:&mut M){
|
||||||
|
methods.add_method("Connect",|_lua,this,f:mlua::Function|
|
||||||
|
Ok(this.connect(f))
|
||||||
|
);
|
||||||
|
// Fire is not allowed to be called from Lua
|
||||||
|
// methods.add_method("Fire",|_lua,this,args:mlua::MultiValue|
|
||||||
|
// Ok(this.fire(args))
|
||||||
|
// );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl mlua::FromLua for ScriptSignal{
|
||||||
|
fn from_lua(value:mlua::Value,_lua:&mlua::Lua)->Result<Self,mlua::Error>{
|
||||||
|
match value{
|
||||||
|
mlua::Value::UserData(ud)=>Ok(ud.borrow::<Self>()?.clone()),
|
||||||
|
other=>Err(mlua::Error::runtime(format!("Expected {} got {:?}",stringify!(ScriptSignal),other))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl mlua::UserData for ScriptConnection{
|
||||||
|
fn add_fields<F:mlua::UserDataFields<Self>>(fields:&mut F){
|
||||||
|
fields.add_field_method_get("Connected",|_,this|{
|
||||||
|
Ok(this.position().is_some())
|
||||||
|
});
|
||||||
|
}
|
||||||
|
fn add_methods<M:mlua::UserDataMethods<Self>>(methods:&mut M){
|
||||||
|
methods.add_method("Disconnect",|_,this,_:()|{
|
||||||
|
if let Some(index)=this.position(){
|
||||||
|
this.signal.callbacks.borrow_mut().remove(index);
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user