rustyscreeps / screeps-arena-game-api Goto Github PK
View Code? Open in Web Editor NEWTyped bindings to the Screeps Arena in-game API for WASM Rust AIs
License: MIT License
Typed bindings to the Screeps Arena in-game API for WASM Rust AIs
License: MIT License
When I try to call creep.id() and print it's results, it panics with
Error:
unwrap_throwfailed at imports.wbg.__wbindgen_throw (file:///user/screeps-arena-starter-rust:424:15) at <anonymous>:wasm-function[414]:0x28dce at <anonymous>:wasm-function[293]:0x258fe at <anonymous>:wasm-function[382]:0x284d1 at <anonymous>:wasm-function[92]:0x14f04 at <anonymous>:wasm-function[250]:0x247a2 at <anonymous>:wasm-function[132]:0x1a1cf at <anonymous>:wasm-function[378]:0x2847b at <anonymous>:wasm-function[311]:0x2604e at <anonymous>:wasm-function[92]:0x14f04
I only get this issue with creeps, not structures.
From debugging, it seems to work if I instead call js_sys::Reflect::get(creep, JsValue::from("id") )
But it actually returns a JsValue number instead of string. I think the cause is that the JS API is inconsistient with storing the ID attribute as strings or numbers.
Also another issue is that it panics when you call creep.id() before it spawns. In which case js_sys::Reflect returns undefined attribute instead.
The API doesn't behave strictly as documented.
According to the docs, when spawnCreep is successful, it returns a javascript structure containing a Creep object ( {object: the_creep}
).
However the creep object is not as described in the documentation - every field is undefined while the documentation states for example that is is of type string. This means that the API crashes if you try and access any member of this blank object.
The solution I think is to treat the return value of spawnCreep not as a Creep
object, but as another type (e.g. CreepPlaceholder
or SpawningCreep
). Then that object can have id() -> Option<JsString>
and the existing Creep
object can can continue with id() -> JsString
.
The less good alternative is to change the spec of Creep so that all of its attribute functions return Option
- but this would make the code harder to work with and less readable, in the 99% of the code base that doesn't deal with spaning, as you'll constantly be having to destructure the enum. Not very ergonomic.
Both of these choices would be breaking changes to existing codebases, however changing the return type of spawnCreep would cause smaller breakage to a smaller area of the code which makes it the better approach not just in the long term but also during the migration.
There are 2 ways we could "fix" this without making a breaking change:
So I think an api breaking change is unavoidable, but will be for the greater good.
objects/impls/creep.rs has BodyPart (representing a section of a creep, which has some hit points)
objects/impls/arena/body_part.rs also has BodyPart (representing an object that can be picked up in ctf mode)
The structures are not the same; maybe their names could be different.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.