Replies: 3 comments
-
My English is not very good, so I'm using translation software as much as possible. I apologize if my expression is unclear. |
Beta Was this translation helpful? Give feedback.
0 replies
-
I thought and tried to wrap EventListeners like this: struct _EventListeners;
#[derive(Clone)]
struct EventListeners(Arc<Mutex<_EventListeners>>); , but I used such black magic: methods.add_function("add", |lua, (this, fun): (LuaAnyUserData, LuaFunction)| {
let list = this.user_value::<LuaValue>()?;
let list = match list {
LuaNil => {
let new_list = lua.create_table()?;
let list = new_list.clone();
this.set_user_value(new_list)?;
list
}
LuaValue::Table(t) => t,
_ => panic! ("listeners? no!"),
};
list.raw_insert(list.raw_len() + 1, fun)?;
let len = list.raw_len();
println! ("{}", len);
Ok(())
}); which interrupted my train of thought. Regarding this black magic, I referred to ~/discussions/30 |
Beta Was this translation helpful? Give feedback.
0 replies
-
pub struct KissaEventNode {
name: String,
}
impl LuaUserData for KissaEventNode {
fn add_fields<'lua, F: LuaUserDataFields<'lua, Self>>(fields: &mut F) {
fields.add_field_method_get("name", |_, this| Ok(this.name.clone()));
fields.add_field_function_get("listeners", |lua, this| {
let list = this.user_value::<LuaValue>()?;
let list = match list {
LuaNil => {
let new_list = lua.create_table()?;
let list = new_list.clone();
this.set_user_value(new_list)?;
list
}
LuaValue::Table(t) => t,
_ => panic!("listeners? no!"),
};
Ok(list)
});
fields.add_field_method_set("name", |_, this, value: String| {
this.name = value;
Ok(())
});
}
fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) {
methods.add_function(
"addlistener",
|lua, (this, fun): (LuaAnyUserData, LuaFunction)| {
let list = this.user_value::<LuaValue>()?;
let list = match list {
LuaNil => {
let new_list = lua.create_table()?;
let list = new_list.clone();
this.set_user_value(new_list)?;
list
}
LuaValue::Table(t) => t,
_ => panic!("listeners? no!"),
};
list.raw_insert(list.raw_len() + 1, fun)?;
Ok(())
},
);
methods.add_function(
"invoke",
|lua, (this, param): (LuaAnyUserData, LuaTable)| {
let list = this.user_value::<LuaValue>()?;
let list = match list {
LuaNil => {
let new_list = lua.create_table()?;
let list = new_list.clone();
this.set_user_value(new_list)?;
list
}
LuaValue::Table(t) => t,
_ => panic!("listeners? no!"),
};
for v in list.pairs::<usize, LuaFunction>() {
let (_, v) = v?;
v.call::<LuaTable, _>(param.clone())?;
}
Ok(())
},
);
}
}
impl KissaEventNode {
pub fn new(event_name: String) -> Self {
KissaEventNode { name: event_name }
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
BERADQ
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
The result obtained is as follows:
However, the expected result is:
It seems like I can't find the same issue or discussion.
Is it an error in my code? Or is this how it's supposed to be?🙏
Beta Was this translation helpful? Give feedback.
All reactions