Added names to objects
This commit is contained in:
@@ -466,6 +466,32 @@ fn register_read_api(engine: &mut Engine, board: &BoardRef, board_queue: &BoardQ
|
||||
.collect()
|
||||
},
|
||||
);
|
||||
|
||||
// my_name() -> String: the calling object's name, or "" if unnamed.
|
||||
let b = board.clone();
|
||||
engine.register_fn("my_name", move |ctx: NativeCallContext| -> String {
|
||||
let src = source_of(&ctx);
|
||||
b.borrow()
|
||||
.objects
|
||||
.get(&src)
|
||||
.and_then(|o| o.name.as_deref())
|
||||
.unwrap_or("")
|
||||
.to_string()
|
||||
});
|
||||
|
||||
// object_id_for_name(s) -> i64: id of the object with the given name, or 0 if none.
|
||||
let b = board.clone();
|
||||
engine.register_fn(
|
||||
"object_id_for_name",
|
||||
move |_ctx: NativeCallContext, name: ImmutableString| -> i64 {
|
||||
b.borrow()
|
||||
.objects
|
||||
.iter()
|
||||
.find(|(_, o)| o.name.as_deref() == Some(name.as_str()))
|
||||
.map(|(&id, _)| id as i64)
|
||||
.unwrap_or(0)
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/// Whether the object at `source` would bump something by moving in `dir`.
|
||||
|
||||
Reference in New Issue
Block a user