원본 vanilla.js 에서 옮겨 오기
바닐라엔진은 vanilla.js 를 러스트로 옮긴 엔진입니다. 두 엔진은 기능 차이를 두지 않고, 공개 API 는 원본의 쓰임새를 따릅니다. 다른 것은 이름 꼴과, 러스트의 소유와 빌림 규칙에 맞춰 바꾼 몇 가지 꼴뿐입니다. 이 장에서는 vanilla.js 로 쓴 게임 코드를 바닐라엔진으로 옮길 때 무엇을 어떻게 바꾸는지 봅니다. 이름 바꾸기, 클래스 상속 대신 트레이트와 공통 상태 구조체, 노드 참조 대신 NodeId, 전역 엔진과 인자로 받는 엔진, 콜백과 Rc<RefCell>, null 대신 Option, 예외 대신 Result, 선택 인자 객체 대신 옵션 구조체, 두 엔진이 같이 쓰는 파일 차례입니다. 이 장은 러스트 판에만 있습니다.
이름 바꾸기
이름은 러스트 규칙(Rust API Guidelines C-CASE)을 따릅니다. 원본 이름을 이 규칙으로 바꾸면 러스트 판의 이름이 됩니다. 이름을 줄이지 않는 것은 원본과 같습니다.
| 갈래 | 원본 (vanilla.js) | 바닐라엔진 |
|---|---|---|
| 함수, 메서드 | setFillColor, getLocalPosition |
set_fill_color, get_local_position (snake_case) |
| 필드, 변수, 모듈 | referenceResolutionSize, autoResizeOnWindowResize |
reference_resolution_size, auto_resize_on_window_resize |
| 타입, 트레이트 | WorldNode, UIButton |
WorldNode, UIButton (UpperCamelCase, 그대로) |
| 열거 값 | ButtonState.normal |
ButtonState::Normal (UpperCamelCase) |
| 상수, 연관 상수 | Colors.white, Pivot.topLeft |
Colors::WHITE, Pivot::TOP_LEFT (SCREAMING_SNAKE_CASE) |
| 생성자 | new Engine(configuration), Vector2.create(1, 2) |
Engine::new(engine_configuration), Vector2::create(1.0, 2.0) |
| 예약어 이름 | 두 엔진 모두 예약어를 이름으로 쓰지 않습니다 | cooldown.activate(), use_program(gl), move_pointer(x, y), repeat_while(..), quadratic::ease_in (원본 activate(), useProgram(), movePointer(x, y), repeatWhile(..), quadratic.easeIn) |
| 노드 객체를 돌려주던 함수 | getParent(), getChildren(), getSiblings() |
get_parent_id, get_child_ids, get_sibling_ids (핸들을 돌려줍니다) |
use vanilla::{Colors, Cooldown, Pivot, Vector2};
assert_eq!(Colors::WHITE, "#ffffff");
assert_eq!(Pivot::TOP_LEFT, Vector2::create(0.0, 0.0));
assert_eq!(Pivot::MIDDLE_CENTER, Vector2::create(0.5, 0.5));
let mut cooldown = Cooldown::new(0.5);
assert!(cooldown.activate());
assert!(!cooldown.activate());
cooldown.tick(0.5);
assert!(cooldown.activate());- 수는
f64입니다. 소수점 없는 정수 글자(640)는f64자리에 쓸 수 없으니640.0으로 씁니다. JS 수의 글을 그대로 내야 하는 곳(Format,LocalStorage)만f64입니다. - 색을 받는 함수는 대부분
impl Into<Color>를 받아"#ffcc33"같은 글과Color::create(0.2, 0.25, 0.35, 1.0)을 함께 받습니다. 원본new Color(r, g, b, a)는Color::create(r, g, b, a)입니다. - 파일과 문서 안의 이름은 바꾸지 않습니다. UI 문서(
.scene.json)의contentSize같은 키, 반사 저장의LineWidth같은 키는 원본 그대로라 두 엔진이 같은 파일을 읽습니다.
첫 씬 옮기기
원본의 노드와 컴포넌트 장에 있는 씬입니다.
import { Engine, EngineConfiguration, Scene, WorldNode, Paint, Vector2, Color } from "./build/vanilla.js";
class NodeScene extends Scene {
initialize(engine) {
super.initialize(engine);
const root = this.getRoot();
const panel = new WorldNode();
panel.setName("Panel");
panel.setContentSize(Vector2.create(400, 240));
panel.setLocalPosition(Vector2.create(640, 360));
const panelPaint = panel.addComponent(Paint);
panelPaint.setColor(new Color(0.2, 0.25, 0.35, 1));
panelPaint.setRoundSize(16);
root.addChild(panel);
const badge = new WorldNode();
badge.setName("Badge");
badge.setContentSize(Vector2.create(64, 64));
badge.setLocalPosition(Vector2.create(400, 0));
const badgePaint = badge.addComponent(Paint);
badgePaint.setColor("#ffcc33");
badgePaint.setRoundSize(32);
panel.addChild(badge);
}
preDraw(graphic) {
super.preDraw(graphic);
const engine = this.getEngine();
const viewManager = engine.getViewManager();
viewManager.applyViewRect(graphic);
}
}
const engineConfiguration = new EngineConfiguration();
engineConfiguration.referenceResolutionSize = Vector2.create(1280, 720);
engineConfiguration.autoResizeOnWindowResize = true;
const engine = new Engine(engineConfiguration);
engine.run(new NodeScene());바닐라엔진으로 옮기면 이렇습니다.
use vanilla::{Color, Engine, EngineConfiguration, Graphic, Paint, Scene, SceneBase, Vector2};
struct NodeScene {
scene_base: SceneBase,
}
impl Scene for NodeScene {
fn get_scene_base(&self) -> &SceneBase {
&self.scene_base
}
fn get_scene_base_mut(&mut self) -> &mut SceneBase {
&mut self.scene_base
}
fn initialize(&mut self, _engine: &Engine) {
let root_id = self.scene_base.get_root_id();
let node_tree = self.scene_base.get_node_tree_mut();
let panel_id = node_tree.create_child(root_id, "Panel");
let panel = &mut node_tree[panel_id];
panel.set_content_size(Vector2::create(400.0, 240.0));
panel.set_local_position(Vector2::create(640.0, 360.0));
let panel_paint = panel.add_component(Paint::new());
panel_paint.set_color(Color::create(0.2, 0.25, 0.35, 1.0));
panel_paint.set_round_size(16.0);
let badge_id = node_tree.create_child(panel_id, "Badge");
let badge = &mut node_tree[badge_id];
badge.set_content_size(Vector2::create(64.0, 64.0));
badge.set_local_position(Vector2::create(400.0, 0.0));
let badge_paint = badge.add_component(Paint::new());
badge_paint.set_color("#ffcc33");
badge_paint.set_round_size(32.0);
}
fn pre_draw(&mut self, graphic: &mut Graphic) {
let engine = Engine::get_engine().unwrap();
engine.get_view_manager().apply_view_rect(graphic);
}
}
fn main() {
let mut engine_configuration = EngineConfiguration::new();
engine_configuration.reference_resolution_size = Vector2::create(1280.0, 720.0);
engine_configuration.auto_resize_on_window_resize = true;
let engine = Engine::new(engine_configuration);
let node_scene = NodeScene {
scene_base: SceneBase::new(),
};
if let Err(error) = engine.run(Box::new(node_scene)) {
eprintln!("{}", error);
}
}class NodeScene extends Scene은SceneBase를 품은 구조체와impl Scene입니다. 반드시 쓰는 것은 공통 상태를 내주는get_scene_base,get_scene_base_mut둘입니다.- 훅은
&Engine을 인자로 받습니다. 원본this.getEngine()자리입니다. - 원본
super.initialize(engine)같은 상위 호출은 없습니다. 트레이트의 기본 구현이 비어 있기 때문입니다. 다만tick과draw는 기본 구현이 노드 트리를 갱신하고 그리므로, 덮어쓸 때는 원본super.tick,super.draw자리에self.scene_base.tick(time_delta),self.scene_base.draw(graphic)을 부릅니다. new WorldNode()와root.addChild(panel)은node_tree.create_child(root_id, "Panel")한 줄입니다. 부모 없이 만들려면create_node(이름)뒤에add_child(부모, 자식)입니다.addComponent(Paint)는 클래스를 받지만add_component(Paint::new())는 값을 받고, 붙인 컴포넌트의&mut를 돌려줍니다.engine.run은 창이나 GL 을 만들지 못하면Err(까닭)을 돌려줍니다.- 원본의 씬
load(engine)는 러스트 판도load(engine)입니다. 애셋은 거기서 요청해 씬에 맡깁니다. (async load는 "async/await" 장)
클래스 상속 대신 트레이트와 공통 상태 구조체
러스트에는 클래스 상속이 없습니다. 공통 상태는 구조체로 품고, 덮어쓰기는 트레이트 함수로 둡니다.
| 원본 | 바닐라엔진 |
|---|---|
class X extends Scene |
필드 scene_base: SceneBase 와 impl Scene for X |
class X extends GameScene |
필드 game_scene_base: GameSceneBase, impl GameScene for X, vanilla::game_scene!(X); |
class X extends UIScene |
필드 UISceneBase, impl UIScene for X, vanilla::ui_scene!(X); |
class X extends Component |
필드 component_base: ComponentBase 와 impl Component for X (get_component_base, get_component_base_mut, get_component_type) |
class X extends Asset |
필드 AssetBase 와 impl Asset for X (get_asset_base, get_asset_base_mut) |
Node, ComponentNode, TransformNode, WorldNode |
WorldNode 하나. 원본에서 노드인 위젯과 UINode 는 컴포넌트입니다 |
위젯의 상속 (UIButton extends UIControl) |
상위 몫을 필드로 품고 함수를 넘깁니다. (UIButton 은 UIControl 을, ShaderSprite 는 Sprite 를 품습니다) 반사는 #[vanilla::reflect(base = 필드)] 로 잇습니다 |
game_scene!, ui_scene! 매크로는 그 씬에 Scene 을 지어 주어, 바탕이 resize, layout, post_draw 를 스스로 부르게 합니다.
컴포넌트를 옮기면 이렇습니다.
class Spin extends Component {
#degreesPerSecond;
constructor() {
super();
this.#degreesPerSecond = 90;
}
setDegreesPerSecond(degreesPerSecond) {
this.#degreesPerSecond = degreesPerSecond;
}
tick(timeDelta) {
const node = this.getNode();
const rotation = node.getLocalRotation();
node.setLocalRotation(rotation + this.#degreesPerSecond * timeDelta);
}
}use vanilla::{Component, ComponentBase, ComponentContext, Engine, EngineConfiguration, NodeTree};
struct Spin {
component_base: ComponentBase,
degrees_per_second: f64,
}
#[vanilla::reflect]
impl Spin {
pub fn new() -> Spin {
Spin {
component_base: ComponentBase::new(),
degrees_per_second: 90.0,
}
}
pub fn set_degrees_per_second(&mut self, degrees_per_second: f64) {
self.degrees_per_second = degrees_per_second;
}
}
#[vanilla::reflect]
impl Component for Spin {
fn get_component_base(&self) -> &ComponentBase {
&self.component_base
}
fn get_component_base_mut(&mut self) -> &mut ComponentBase {
&mut self.component_base
}
fn get_component_type(&self) -> &'static str {
"Spin"
}
fn tick(&mut self, context: &mut ComponentContext, time_delta: f64) {
let transform = context.get_transform_mut();
let local_rotation = transform.get_local_rotation();
transform.set_local_rotation(local_rotation + self.degrees_per_second * time_delta);
}
}
let engine = Engine::new(EngineConfiguration::new());
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let spinner_id = node_tree.create_child(root_id, "Spinner");
node_tree[spinner_id].add_component(Spin::new()).set_degrees_per_second(180.0);
node_tree.tick(&engine, 0.5);
assert_eq!(node_tree[spinner_id].get_local_rotation(), 90.0);- 원본
#비공개 필드는 구조체 필드(pub없이)입니다. - 원본
this.getNode()자리는 훅이 받는ComponentContext입니다. 자기 트랜스폼은get_transform_mut, 자기 노드는get_node_mut, 트리 전체는get_node_tree_mut, 엔진은get_engine으로 꺼냅니다. - 같은 노드의 다른 컴포넌트는
context.get_node_mut().get_component_mut::<T>()로 찾습니다. 부르고 있는 컴포넌트 자신은 그동안 노드에서 잠깐 빠져 있어 이 길로는 찾을 수 없습니다. draw는 노드 대신 자기 트랜스폼(&NodeTransform)을 받습니다. 원본this.getNode().getContentSize()는transform.get_content_size()입니다. 그리는 동안 트리는 바뀌지 않습니다.- 붙을 때 자식 노드를 만드는 컴포넌트(위젯 같은 것)는
node_tree.add_component(node_id, 컴포넌트)로 붙여야attach_to_tree가 불립니다. 원본attach(node)자리입니다.
노드 참조 대신 NodeId
원본은 부모가 children[] 으로 자식 객체를 들고 자식이 parent 로 거꾸로 가리킵니다. 바닐라엔진은 씬의 NodeTree 가 모든 노드를 저장소 하나에 담고, 부모와 자식을 NodeId 로 잇습니다. NodeId 는 칸 번호와 세대 번호라서 지운 노드의 핸들은 다시 쓰이지 않습니다.
const root = this.getRoot();
const panel = new WorldNode();
panel.setName("Panel");
root.addChild(panel);
const label = new WorldNode();
label.setName("Label");
panel.addChild(label);
console.log(label.getParent() === panel);
panel.removeChild(label);
root.addChild(label);use vanilla::NodeTree;
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let panel_id = node_tree.create_child(root_id, "Panel");
let label_id = node_tree.create_child(panel_id, "Label");
assert_eq!(node_tree.get_parent_id(label_id), Some(panel_id));
assert_eq!(node_tree.get_child_ids(panel_id), [label_id]);
node_tree.remove_child(panel_id, label_id);
assert_eq!(node_tree.get_parent_id(label_id), None);
assert!(node_tree.add_child(root_id, label_id));
node_tree.destroy_node(panel_id);
assert!(node_tree.get_node(panel_id).is_none());
assert!(node_tree.get_node(label_id).is_some());- 노드는
node_tree[node_id]로 꺼내 설정합니다. 지운 노드의 핸들을 이렇게 꺼내면 멈추므로, 있을지 모를 때는get_node,get_node_mut이 돌려주는Option을 봅니다. - 원본에서 노드 객체를 필드에 들던 자리는
NodeId를 듭니다.NodeId는Copy라 마음대로 복사하고 콜백에 넘깁니다. 아직 만들지 않은 노드는Option<NodeId>로 둡니다. remove_child,remove_from_parent는 떼어 내기만 하고,destroy_node는 자손까지 지웁니다.tick이나 터치를 도는 중에 부르면 비활성으로 두었다가 다 돈 뒤에 지웁니다.- 원본
getParent,getChildren,getSiblings는 핸들을 돌려주는get_parent_id,get_child_ids,get_sibling_ids입니다. 찾기(find_child_by_name,find_descendant_by_name같은 것)도 핸들을 돌려줍니다. - 씬 코드가 컴포넌트를 쥔 동안에는 같은 트리의 다른 노드를 함께 빌릴 수 없습니다. 둘을 함께 만질 때는
node_tree.with_component_mut::<T, _>(node_id, |component, tree| ...)를 쓰고, 자주 쓰는 것은 트리를 받는*_in_tree함수(UIWindow::close_in_tree,ParticleSystem::emit_in_tree같은 것)로 따로 있습니다.
전역 엔진과 인자로 받는 엔진
원본은 Engine.getEngine(), this.getEngine() 으로 어디서든 엔진을 꺼냅니다. 바닐라엔진도 Engine::get_engine()(원본 Engine.getEngine()), SceneBase::get_engine()(원본 Scene.getEngine()), UIView::get_engine() 으로 전역 엔진을 꺼냅니다. Engine::new 직후부터 씬의 훅, 컴포넌트의 tick 과 draw, 콜백, async 작업 안 어디서나 꺼내고, 늘 같은 &'static Engine 입니다. 엔진 함수는 모두 &self 라서 꺼낸 엔진으로 바로 고칩니다. 매니저는 get_input_manager() 가 Ref, get_input_manager_mut() 이 RefMut 을 줍니다. 한 문장 안에서 쓰고, let 으로 쥔 채 같은 매니저를 또 빌리면 함수 이름을 적은 패닉으로 멈춥니다. 아래 길로도 받습니다. 모두 같은 엔진입니다.
| 자리 | 엔진을 받는 길 |
|---|---|
씬의 훅 가운데 원본이 엔진을 받는 것 (load, initialize, finalize, unload) |
인자 engine: &Engine (원본 load(engine) 처럼) |
씬의 다른 훅 (create, tick, pre_draw, draw, touch_press, resize 같은 것) |
Engine::get_engine() (원본처럼 인자는 time_delta, graphic, view_input_position 같은 것만) |
컴포넌트의 tick, 터치, 휠 |
context.get_engine() (&Engine) |
콜백 (UIButtonEvent 같은 것) |
콜백이 받는 ComponentContext |
| async 작업 | engine_context.with(|engine| ...), scene_context.with(|scene, engine| ...) |
컴포넌트의 draw, 엔진을 받지 않는 함수 |
Engine::get_engine() |
인스펙터의 게터 목록에서 원본 UIView.getEngine 은 뺍니다(원본은 setEngine 으로 준 값, 러스트는 전역 엔진을 꺼내는 함수). 반사 속성 목록에도 Engine 이 없습니다.
원본 engine.getGraphic() 은 engine.get_graphic()(변경은 get_graphic_mut())입니다. 엔진을 받는 위 자리 어디서나 GL 자원을 짓습니다. 다른 것은 둘입니다.
- 씬의 그리기 훅(
pre_draw,draw,post_draw,draw_on_load,draw_gizmos)은graphic: &mut Graphic을 인자로 받습니다. 엔진이 든 그래픽을 잠깐 꺼내 넘긴 것이라 그 안에서는engine.get_graphic()이None이고 인자의graphic을 씁니다. 그리기 밖에서 엔진과 그래픽을 함께&mut로 쓰려면engine.with_graphic(|engine, graphic| ...)입니다. - 엔진을 받지 않는 컴포넌트의
attach는 그래픽에도 닿지 않습니다. GL 자원은 컴포넌트를 짓는 자리(씬의load)나 컴포넌트의tick에서 짓습니다.
콜백과 Rc
원본의 화살표 함수는 this 와 바깥 변수를 마음대로 붙잡습니다. 바닐라엔진의 콜백은 Box<dyn FnMut(...)> 로 받아 컴포넌트가 들고 있으므로, 씬의 &mut self 같은 빌린 값을 붙잡을 수 없습니다. 다른 노드는 NodeId 를 붙잡아 콜백이 받는 문맥으로 찾고, 씬과 나눠 써야 하는 값은 Rc<Cell> 이나 Rc<RefCell> 에 담습니다.
let clickCount = 0;
const closeButton = close.addComponent(UIButton);
closeButton.setClickEvent(() => {
panel.setActive(false);
clickCount += 1;
});use std::cell::Cell;
use std::rc::Rc;
use vanilla::{ComponentContext, Engine, EngineConfiguration, NodeTree, TouchKind, UIButton, Vector2};
let engine = Engine::new(EngineConfiguration::new());
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let panel_id = node_tree.create_child(root_id, "Panel");
let close_id = node_tree.create_child(root_id, "Close");
node_tree[close_id].set_content_size(Vector2::create(40.0, 40.0));
let click_count = Rc::new(Cell::new(0));
let event_click_count = click_count.clone();
let close_button = node_tree.add_component(close_id, UIButton::new()).expect("button");
close_button.set_click_event(Box::new(move |_button: &mut UIButton, context: &mut ComponentContext| {
context.get_node_tree_mut()[panel_id].set_active(false);
event_click_count.set(event_click_count.get() + 1);
}));
node_tree.dispatch_touch(close_id, &engine, TouchKind::Press, Vector2::zero());
node_tree.dispatch_touch(close_id, &engine, TouchKind::Release, Vector2::zero());
assert!(!node_tree[panel_id].is_active());
assert_eq!(click_count.get(), 1);- 원본 콜백이 받던 버튼은 그대로 첫 인자로 오고, 둘째 인자
ComponentContext로 트리와 엔진에 닿습니다. - 콜백 게터(
UIButton::get_click_event같은 것)와 갈아 끼운 저장소(LocalStorage::get_backend,PersistedStore::get_storage_backend)는Rc<RefCell<...>>로 돌려줍니다. 부르는 쪽이borrow_mut로 빌려 씁니다. 엔진과 부르는 쪽이 같은 함수를 나눠 쥐고, 부르는 동안 다시 부르는 것을 막습니다. - 씬 구조체의 값을 콜백에서 바꾸고 싶으면 그 값을
Rc<Cell<T>>나Rc<RefCell<T>>필드로 두고 복제본을 콜백에 넘깁니다.
null 대신 Option
원본에서 null 이나 undefined 를 돌려주던 함수는 Option 을 돌려줍니다. 받는 쪽은 if let, match 로 가립니다.
use vanilla::{NodeTree, Paint, Text};
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let panel_id = node_tree.create_child(root_id, "Panel");
node_tree[panel_id].add_component(Paint::new());
if let Some(paint) = node_tree[panel_id].get_component_mut::<Paint>() {
paint.set_color("#ffcc33");
}
assert!(node_tree[panel_id].get_component::<Text>().is_none());
assert_eq!(node_tree.find_child_by_name(root_id, "Panel"), Some(panel_id));
assert_eq!(node_tree.find_child_by_name(root_id, "Missing"), None);- 원본
getComponent(Paint)가null이던 자리는get_component::<Paint>()의None입니다. - 원본에서
null을 받던 세터는Option을 받습니다. 원본sprite.setImage(null)은sprite.set_image(None)입니다. 반사 값에서는None이 원본null자리인ReflectValue::Null입니다.
예외 대신 Result
러스트는 예외를 던지지 않습니다. 실패할 수 있는 함수는 Result<T, String> 을 돌려주고 Err 에 까닭을 담습니다. 원본 try/catch 자리는 match 이고, 같은 꼴의 함수 안에서는 ? 로 넘깁니다.
use vanilla::{NodeTree, Timeline, UIDocument};
fn count_document_children(json_text: &str) -> Result<usize, String> {
let mut document = UIDocument::new();
let mut node_tree = NodeTree::new();
let root_id = document.from_json_text(&mut node_tree, json_text)?;
Ok(node_tree.get_child_count(root_id))
}
assert!(count_document_children("{").is_err());
match Timeline::load_from_text("not json") {
Ok(_timeline) => {
panic!("unexpected");
}
Err(reason) => {
assert!(!reason.is_empty());
}
}- async 작업 안에서 원본
await가 던지던 자리는 손잡이의await가 돌려주는Result입니다. 원본reject는Promise<Result<T, E>>의reject(까닭)입니다. - 셰이더를 짓지 못하는 것처럼 한 효과만 실패하는 일은 한 번 알리고 건너뜁니다. 프레임은 멈추지 않습니다.
- 실패한 애셋도 끝난 것으로 쳐서 씬이 멈추지 않는 것은 원본과 같습니다.
선택 인자 객체 대신 옵션 구조체
원본은 { decayRate: 9, frequency: 42 } 같은 객체로 선택 인자를 받습니다. 러스트에는 이름 붙은 선택 인자가 없어 기본값을 가진 옵션 구조체를 받습니다. 바꿀 것만 적고 나머지는 ..기본값 으로 채웁니다.
const shaker = new Shaker({ decayRate: 10 });
shaker.addShake(8);use vanilla::{Shaker, ShakerOptions};
let mut shaker = Shaker::new(ShakerOptions {
decay_rate: 10.0,
..ShakerOptions::default()
});
shaker.add_shake(8.0);
assert!(shaker.is_shaking());ToonDrawOptions, RenderTargetOptions, UIConfirmWindowOptions, FloatingTextOptions 같은 옵션 구조체가 모두 이 꼴입니다. 엔진이 필드를 더해도 ..Default::default() 로 지은 코드는 그대로 돕니다.
async/await 와 반사
- 원본
async load,await asset.load(경로),await Wait.seconds(1),fadeScreen(async () => ...)는 같은 꼴의 러스트async/await로 옮깁니다. 다른 점은await다음 줄에서this를 바로 쓰지 않고scene_context.with(|scene, engine| ...)안에서 만지는 것입니다. 자세한 것은 "async/await" 장에 있습니다. - 원본은 실행 중에 모든 객체의
get,is,set함수를 훑습니다. 바닐라엔진은 컴포넌트의impl블록과impl Component블록에#[vanilla::reflect]를 붙인 타입만 반사됩니다. UI 문서에 저장하거나 개발자 도구 인스펙터에 보일 게임 컴포넌트는 매크로를 붙입니다. 자세한 것은 "반사 매크로 #[reflect]" 장에 있습니다.
두 엔진이 같이 쓰는 파일
애셋 파일과 저작 도구가 저장한 파일은 두 엔진이 같은 꼴로 읽고 씁니다. 옮길 때 파일은 그대로 둡니다.
| 파일 | 원본에서 짓는 곳 | 바닐라엔진에서 읽는 곳 |
|---|---|---|
UI 문서 .scene.json |
UI 에디터(tools/uieditor), UIDocument.toJsonText |
UIDocument::from_json_text, to_json_text (키 차례, 탭 들여쓰기, 수의 글꼴까지 원본이 낸 글과 같습니다). 에디터가 등록하는 종류는 register_editor_types 한 줄 |
타임라인 문서 .timeline.json |
타임라인 편집기(tools/timelineeditor) |
Timeline::load_from_text, Timeline::load_from_url |
| 스프라이트 아틀라스 JSON 과 쪽 그림 | tools/atlas.cjs |
SpriteAtlas::load (원본과 같은 두 꼴) |
언어 묶음 languages.json 과 언어 파일 |
게임의 애셋 | languagepack (load_language_list, load_language) |
UI 글꼴 목록 fonts.json, fonts-<묶음>.json |
게임의 애셋 | uifont (load_ui_font) |
빌드 정보 version.json |
배포 빌드 | BuildInfo::load |
- 애셋 경로는 원본과 같은 상대 경로입니다. 데스크탑은
EngineConfiguration.asset_directory_paths의 폴더에서 바로 읽고, 웹은 원본처럼asset_base_url과 경로를 이어 받습니다. 게임 코드는 같습니다. - 저작 도구와 파일 꼴은 "저작 도구와 두 엔진이 같이 쓰는 파일" 장에 있습니다.
옮기지 않은 원본 파일
원본 src/ 의 파일 가운데 러스트 판에 같은 이름의 파일이 없는 것은 다른 파일에 합쳤거나 일부러 옮기지 않은 것입니다. (자세한 목록은 저장소의 ARCHITECTURE.md 9장)
| 원본 | 바닐라엔진 |
|---|---|
base/node.js, core/node/componentnode.js, core/node/transformnode.js |
WorldNode 와 NodeTree 에 합침 |
core/scenemanager.js |
Engine 의 load_scene, unload_scene, get_all_loaded_scenes (씬 객체 대신 SceneId) |
core/touchraycaster.js |
SceneBase::tick_touch, NodeTree 의 누름 판정, TouchRecognizer 에 합침 |
resource/jsonasset.js |
TextAsset 과 같은 파일의 JsonAsset |
base/object.js, base/singleton.js, base/collection 의 Dictionary, List, Queue, Set, Stack |
러스트 언어와 표준(트레이트, Any, enum, Vec, HashMap 같은 것)이 같은 일을 합니다 |
base/reflection.js |
실행 중 반사는 #[vanilla::reflect], createGUID 는 create_guid |
deprecated/camera.js |
회전과 begin/end 까지 Camera2D 가 덮습니다 |
브라우저가 하던 일 (캔버스 2D, window.alert, confirm, prompt, html lang 과 meta) |
Canvas2D, Engine::alert, confirm, prompt (와 _async), Document |
관련 타입
Scene,SceneBase,GameScene,GameSceneBase,UIScene,UISceneBase,game_scene!,ui_scene!Component,ComponentBase,ComponentContext,Asset,AssetBaseNodeTree,NodeId,WorldNode,NodeTransformEngine,EngineConfiguration,ViewManagerUIButtonEvent,Shaker,ShakerOptions,Cooldown,Colors,Color,PivotUIDocument,Timeline,SpriteAtlas,BuildInfo,languagepack,uifont
Porting from vanilla.js
Vanilla Engine is vanilla.js ported to Rust. The two engines keep no feature gap, and the public API follows the original's usage. What differs is the naming style and a few patterns changed to fit Rust's ownership and borrowing rules. This chapter shows what to change, and how, when porting game code written for vanilla.js to Vanilla Engine: renaming, traits and shared state structs instead of class inheritance, NodeId instead of node references, the global engine and the engine passed as an argument, callbacks and Rc<RefCell>, Option instead of null, Result instead of exceptions, option structs instead of option objects, and the files both engines share. This chapter exists only in the Rust edition.
Renaming
Names follow the Rust rules (Rust API Guidelines C-CASE). Converting an original name with these rules gives the Rust edition's name. Not abbreviating names is the same as the original.
| Kind | Original (vanilla.js) | Vanilla Engine |
|---|---|---|
| Functions, methods | setFillColor, getLocalPosition |
set_fill_color, get_local_position (snake_case) |
| Fields, variables, modules | referenceResolutionSize, autoResizeOnWindowResize |
reference_resolution_size, auto_resize_on_window_resize |
| Types, traits | WorldNode, UIButton |
WorldNode, UIButton (UpperCamelCase, unchanged) |
| Enum values | ButtonState.normal |
ButtonState::Normal (UpperCamelCase) |
| Constants, associated constants | Colors.white, Pivot.topLeft |
Colors::WHITE, Pivot::TOP_LEFT (SCREAMING_SNAKE_CASE) |
| Constructors | new Engine(configuration), Vector2.create(1, 2) |
Engine::new(engine_configuration), Vector2::create(1.0, 2.0) |
| Reserved-word names | Neither engine uses a reserved word as a name | cooldown.activate(), use_program(gl), move_pointer(x, y), repeat_while(..), quadratic::ease_in (original activate(), useProgram(), movePointer(x, y), repeatWhile(..), quadratic.easeIn) |
| Functions that returned node objects | getParent(), getChildren(), getSiblings() |
get_parent_id, get_child_ids, get_sibling_ids (they return handles) |
use vanilla::{Colors, Cooldown, Pivot, Vector2};
assert_eq!(Colors::WHITE, "#ffffff");
assert_eq!(Pivot::TOP_LEFT, Vector2::create(0.0, 0.0));
assert_eq!(Pivot::MIDDLE_CENTER, Vector2::create(0.5, 0.5));
let mut cooldown = Cooldown::new(0.5);
assert!(cooldown.activate());
assert!(!cooldown.activate());
cooldown.tick(0.5);
assert!(cooldown.activate());- Numbers are
f64. An integer literal without a decimal point (640) cannot be used where anf64is expected, so write640.0. Only places that must produce JS number text exactly (Format,LocalStorage) usef64. - Most functions that take a color take
impl Into<Color>, so they accept both text such as"#ffcc33"andColor::create(0.2, 0.25, 0.35, 1.0). The originalnew Color(r, g, b, a)isColor::create(r, g, b, a). - Names inside files and documents do not change. Keys such as
contentSizein the UI document (.scene.json) and keys such asLineWidthin reflection saving stay as in the original, so both engines read the same files.
Porting a First Scene
This is the scene from the original's nodes and components chapter.
import { Engine, EngineConfiguration, Scene, WorldNode, Paint, Vector2, Color } from "./build/vanilla.js";
class NodeScene extends Scene {
initialize(engine) {
super.initialize(engine);
const root = this.getRoot();
const panel = new WorldNode();
panel.setName("Panel");
panel.setContentSize(Vector2.create(400, 240));
panel.setLocalPosition(Vector2.create(640, 360));
const panelPaint = panel.addComponent(Paint);
panelPaint.setColor(new Color(0.2, 0.25, 0.35, 1));
panelPaint.setRoundSize(16);
root.addChild(panel);
const badge = new WorldNode();
badge.setName("Badge");
badge.setContentSize(Vector2.create(64, 64));
badge.setLocalPosition(Vector2.create(400, 0));
const badgePaint = badge.addComponent(Paint);
badgePaint.setColor("#ffcc33");
badgePaint.setRoundSize(32);
panel.addChild(badge);
}
preDraw(graphic) {
super.preDraw(graphic);
const engine = this.getEngine();
const viewManager = engine.getViewManager();
viewManager.applyViewRect(graphic);
}
}
const engineConfiguration = new EngineConfiguration();
engineConfiguration.referenceResolutionSize = Vector2.create(1280, 720);
engineConfiguration.autoResizeOnWindowResize = true;
const engine = new Engine(engineConfiguration);
engine.run(new NodeScene());Ported to Vanilla Engine, it looks like this.
use vanilla::{Color, Engine, EngineConfiguration, Graphic, Paint, Scene, SceneBase, Vector2};
struct NodeScene {
scene_base: SceneBase,
}
impl Scene for NodeScene {
fn get_scene_base(&self) -> &SceneBase {
&self.scene_base
}
fn get_scene_base_mut(&mut self) -> &mut SceneBase {
&mut self.scene_base
}
fn initialize(&mut self, _engine: &Engine) {
let root_id = self.scene_base.get_root_id();
let node_tree = self.scene_base.get_node_tree_mut();
let panel_id = node_tree.create_child(root_id, "Panel");
let panel = &mut node_tree[panel_id];
panel.set_content_size(Vector2::create(400.0, 240.0));
panel.set_local_position(Vector2::create(640.0, 360.0));
let panel_paint = panel.add_component(Paint::new());
panel_paint.set_color(Color::create(0.2, 0.25, 0.35, 1.0));
panel_paint.set_round_size(16.0);
let badge_id = node_tree.create_child(panel_id, "Badge");
let badge = &mut node_tree[badge_id];
badge.set_content_size(Vector2::create(64.0, 64.0));
badge.set_local_position(Vector2::create(400.0, 0.0));
let badge_paint = badge.add_component(Paint::new());
badge_paint.set_color("#ffcc33");
badge_paint.set_round_size(32.0);
}
fn pre_draw(&mut self, graphic: &mut Graphic) {
let engine = Engine::get_engine().unwrap();
engine.get_view_manager().apply_view_rect(graphic);
}
}
fn main() {
let mut engine_configuration = EngineConfiguration::new();
engine_configuration.reference_resolution_size = Vector2::create(1280.0, 720.0);
engine_configuration.auto_resize_on_window_resize = true;
let engine = Engine::new(engine_configuration);
let node_scene = NodeScene {
scene_base: SceneBase::new(),
};
if let Err(error) = engine.run(Box::new(node_scene)) {
eprintln!("{}", error);
}
}class NodeScene extends Scenebecomes a struct holding aSceneBaseplusimpl Scene. The two functions you must write areget_scene_baseandget_scene_base_mut, which hand out the shared state.- Hooks receive
&Engineas an argument, in place of the originalthis.getEngine(). - There are no superclass calls such as the original
super.initialize(engine), because the trait's default implementations are empty. However, the defaults oftickanddrawupdate and draw the node tree, so when you override them, callself.scene_base.tick(time_delta)andself.scene_base.draw(graphic)where the original calledsuper.tickandsuper.draw. new WorldNode()plusroot.addChild(panel)is the single linenode_tree.create_child(root_id, "Panel"). To create a node without a parent, usecreate_node(name)and thenadd_child(parent, child).addComponent(Paint)takes a class, whileadd_component(Paint::new())takes a value and returns a&mutto the attached component.engine.runreturnsErr(reason)when it cannot create the window or GL.- The original scene
load(engine)is alsoload(engine)in the Rust edition, where assets are requested and handed to the scene (async loadis covered in the "async/await" chapter).
Traits and Shared State Structs Instead of Class Inheritance
Rust has no class inheritance. Shared state is embedded as a struct, and overrides are trait functions.
| Original | Vanilla Engine |
|---|---|
class X extends Scene |
A scene_base: SceneBase field and impl Scene for X |
class X extends GameScene |
A game_scene_base: GameSceneBase field, impl GameScene for X, vanilla::game_scene!(X); |
class X extends UIScene |
A UISceneBase field, impl UIScene for X, vanilla::ui_scene!(X); |
class X extends Component |
A component_base: ComponentBase field and impl Component for X (get_component_base, get_component_base_mut, get_component_type) |
class X extends Asset |
An AssetBase field and impl Asset for X (get_asset_base, get_asset_base_mut) |
Node, ComponentNode, TransformNode, WorldNode |
A single WorldNode. Widgets that are nodes in the original, and UINode, are components |
Widget inheritance (UIButton extends UIControl) |
Embed the superclass part as a field and forward functions (UIButton embeds UIControl, ShaderSprite embeds Sprite). Reflection is chained with #[vanilla::reflect(base = field)] |
The game_scene! and ui_scene! macros generate Scene for that scene, so the base calls resize, layout, and post_draw on its own.
Porting a component looks like this.
class Spin extends Component {
#degreesPerSecond;
constructor() {
super();
this.#degreesPerSecond = 90;
}
setDegreesPerSecond(degreesPerSecond) {
this.#degreesPerSecond = degreesPerSecond;
}
tick(timeDelta) {
const node = this.getNode();
const rotation = node.getLocalRotation();
node.setLocalRotation(rotation + this.#degreesPerSecond * timeDelta);
}
}use vanilla::{Component, ComponentBase, ComponentContext, Engine, EngineConfiguration, NodeTree};
struct Spin {
component_base: ComponentBase,
degrees_per_second: f64,
}
#[vanilla::reflect]
impl Spin {
pub fn new() -> Spin {
Spin {
component_base: ComponentBase::new(),
degrees_per_second: 90.0,
}
}
pub fn set_degrees_per_second(&mut self, degrees_per_second: f64) {
self.degrees_per_second = degrees_per_second;
}
}
#[vanilla::reflect]
impl Component for Spin {
fn get_component_base(&self) -> &ComponentBase {
&self.component_base
}
fn get_component_base_mut(&mut self) -> &mut ComponentBase {
&mut self.component_base
}
fn get_component_type(&self) -> &'static str {
"Spin"
}
fn tick(&mut self, context: &mut ComponentContext, time_delta: f64) {
let transform = context.get_transform_mut();
let local_rotation = transform.get_local_rotation();
transform.set_local_rotation(local_rotation + self.degrees_per_second * time_delta);
}
}
let engine = Engine::new(EngineConfiguration::new());
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let spinner_id = node_tree.create_child(root_id, "Spinner");
node_tree[spinner_id].add_component(Spin::new()).set_degrees_per_second(180.0);
node_tree.tick(&engine, 0.5);
assert_eq!(node_tree[spinner_id].get_local_rotation(), 90.0);- The original's
#private fields are struct fields (withoutpub). - In place of the original
this.getNode(), hooks receive aComponentContext. Get your own transform withget_transform_mut, your own node withget_node_mut, the whole tree withget_node_tree_mut, and the engine withget_engine. - Other components on the same node are found with
context.get_node_mut().get_component_mut::<T>(). The component being called is briefly taken out of the node meanwhile, so it cannot find itself this way. drawreceives its own transform (&NodeTransform) instead of the node. The originalthis.getNode().getContentSize()istransform.get_content_size(). The tree does not change while drawing.- Components that create child nodes when attached (such as widgets) must be attached with
node_tree.add_component(node_id, component)so thatattach_to_treeis called. This is where the original hadattach(node).
NodeId Instead of Node References
In the original, a parent holds its child objects in children[] and each child points back with parent. In Vanilla Engine, the scene's NodeTree keeps every node in a single store and links parents and children with NodeId. A NodeId is a slot number plus a generation number, so the handle of a destroyed node is never reused.
const root = this.getRoot();
const panel = new WorldNode();
panel.setName("Panel");
root.addChild(panel);
const label = new WorldNode();
label.setName("Label");
panel.addChild(label);
console.log(label.getParent() === panel);
panel.removeChild(label);
root.addChild(label);use vanilla::NodeTree;
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let panel_id = node_tree.create_child(root_id, "Panel");
let label_id = node_tree.create_child(panel_id, "Label");
assert_eq!(node_tree.get_parent_id(label_id), Some(panel_id));
assert_eq!(node_tree.get_child_ids(panel_id), [label_id]);
node_tree.remove_child(panel_id, label_id);
assert_eq!(node_tree.get_parent_id(label_id), None);
assert!(node_tree.add_child(root_id, label_id));
node_tree.destroy_node(panel_id);
assert!(node_tree.get_node(panel_id).is_none());
assert!(node_tree.get_node(label_id).is_some());- Nodes are fetched and configured with
node_tree[node_id]. Fetching a destroyed node's handle this way panics, so when the node may be gone, check theOptionreturned byget_nodeorget_node_mut. - Where the original kept node objects in fields, keep a
NodeId.NodeIdisCopy, so copy it freely and pass it into callbacks. Nodes not created yet are held asOption<NodeId>. remove_childandremove_from_parentonly detach, whiledestroy_nodedestroys the node with its descendants. Called duringtickor touch handling, it deactivates the node and destroys it after the traversal ends.- The original
getParent,getChildren, andgetSiblingsareget_parent_id,get_child_ids, andget_sibling_ids, which return handles. Searches (find_child_by_name,find_descendant_by_name, and so on) also return handles. - While scene code holds a component, it cannot borrow other nodes of the same tree at the same time. To touch both, use
node_tree.with_component_mut::<T, _>(node_id, |component, tree| ...); common cases have separate*_in_treefunctions that take the tree (such asUIWindow::close_in_treeandParticleSystem::emit_in_tree).
The Global Engine and the Engine as an Argument
The original gets the engine from anywhere with Engine.getEngine() or this.getEngine(). Vanilla Engine also takes the global engine with Engine::get_engine() (the original Engine.getEngine()), SceneBase::get_engine() (the original Scene.getEngine()) and UIView::get_engine(). It can be taken anywhere right after Engine::new: scene hooks, a component's tick and draw, callbacks, inside async tasks, and it is always the same &'static Engine. All engine functions are &self, so you change things directly through the engine you took. Managers come as Ref from get_input_manager() and RefMut from get_input_manager_mut(). Use them within one statement; holding one with let and borrowing the same manager again stops with a panic that names the function. It is also received through the ways below. All are the same engine.
| Place | How it receives the engine |
|---|---|
Scene hooks that take the engine in the original (load, initialize, finalize, unload) |
The engine: &Engine argument (like the original load(engine)) |
Other scene hooks (create, tick, pre_draw, draw, touch_press, resize, and so on) |
Engine::get_engine() (as in the original, the arguments are only time_delta, graphic, view_input_position and the like) |
A component's tick, touch, wheel |
context.get_engine() (&Engine) |
Callbacks (such as UIButtonEvent) |
The ComponentContext the callback receives |
| async tasks | engine_context.with(|engine| ...), scene_context.with(|scene, engine| ...) |
A component's draw, functions that do not receive the engine |
Engine::get_engine() |
The original UIView.getEngine is left out of the inspector's getter list (in the original it is the value given with setEngine, in Rust a function that takes the global engine). Engine is not in the reflected property list either.
The original engine.getGraphic() is engine.get_graphic() (get_graphic_mut() to change it). You build GL resources in any of the places above that receive the engine. Two things differ.
- The scene's drawing hooks (
pre_draw,draw,post_draw,draw_on_load,draw_gizmos) receivegraphic: &mut Graphicas an argument. It is the engine's graphic taken out for a moment and passed in, so inside themengine.get_graphic()isNoneand you use thegraphicargument. To use the engine and the graphic together as&mutoutside drawing, callengine.with_graphic(|engine, graphic| ...). - A component's
attach, which does not receive the engine, cannot reach the graphic either. Build GL resources where you create the component (the scene'sload) or in the component'stick.
Callbacks and Rc
The original's arrow functions capture this and outer variables freely. Vanilla Engine takes callbacks as Box<dyn FnMut(...)> and the component keeps them, so they cannot capture borrowed values such as the scene's &mut self. Capture a NodeId and reach other nodes through the context the callback receives, and put values shared with the scene in an Rc<Cell> or Rc<RefCell>.
let clickCount = 0;
const closeButton = close.addComponent(UIButton);
closeButton.setClickEvent(() => {
panel.setActive(false);
clickCount += 1;
});use std::cell::Cell;
use std::rc::Rc;
use vanilla::{ComponentContext, Engine, EngineConfiguration, NodeTree, TouchKind, UIButton, Vector2};
let engine = Engine::new(EngineConfiguration::new());
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let panel_id = node_tree.create_child(root_id, "Panel");
let close_id = node_tree.create_child(root_id, "Close");
node_tree[close_id].set_content_size(Vector2::create(40.0, 40.0));
let click_count = Rc::new(Cell::new(0));
let event_click_count = click_count.clone();
let close_button = node_tree.add_component(close_id, UIButton::new()).expect("button");
close_button.set_click_event(Box::new(move |_button: &mut UIButton, context: &mut ComponentContext| {
context.get_node_tree_mut()[panel_id].set_active(false);
event_click_count.set(event_click_count.get() + 1);
}));
node_tree.dispatch_touch(close_id, &engine, TouchKind::Press, Vector2::zero());
node_tree.dispatch_touch(close_id, &engine, TouchKind::Release, Vector2::zero());
assert!(!node_tree[panel_id].is_active());
assert_eq!(click_count.get(), 1);- The button the original callback received still comes as the first argument, and the second argument,
ComponentContext, reaches the tree and the engine. - Callback getters (such as
UIButton::get_click_event) and swapped-in storage backends (LocalStorage::get_backend,PersistedStore::get_storage_backend) are returned asRc<RefCell<...>>. The caller borrows them withborrow_mut. The engine and the caller share the same function, and calling it again while it is being called is prevented. - To change a value of the scene struct from a callback, keep that value in an
Rc<Cell<T>>orRc<RefCell<T>>field and pass a clone to the callback.
Option Instead of null
Functions that returned null or undefined in the original return an Option. The receiving side tells the cases apart with if let or match.
use vanilla::{NodeTree, Paint, Text};
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let panel_id = node_tree.create_child(root_id, "Panel");
node_tree[panel_id].add_component(Paint::new());
if let Some(paint) = node_tree[panel_id].get_component_mut::<Paint>() {
paint.set_color("#ffcc33");
}
assert!(node_tree[panel_id].get_component::<Text>().is_none());
assert_eq!(node_tree.find_child_by_name(root_id, "Panel"), Some(panel_id));
assert_eq!(node_tree.find_child_by_name(root_id, "Missing"), None);- Where the original
getComponent(Paint)wasnull,get_component::<Paint>()returnsNone. - Setters that accepted
nullin the original take anOption. The originalsprite.setImage(null)issprite.set_image(None). In reflected values,NoneisReflectValue::Null, the place of the originalnull.
Result Instead of Exceptions
Rust does not throw exceptions. Functions that can fail return Result<T, String> and put the reason in Err. The original try/catch becomes match, and inside a function of the same shape, ? passes the error on.
use vanilla::{NodeTree, Timeline, UIDocument};
fn count_document_children(json_text: &str) -> Result<usize, String> {
let mut document = UIDocument::new();
let mut node_tree = NodeTree::new();
let root_id = document.from_json_text(&mut node_tree, json_text)?;
Ok(node_tree.get_child_count(root_id))
}
assert!(count_document_children("{").is_err());
match Timeline::load_from_text("not json") {
Ok(_timeline) => {
panic!("unexpected");
}
Err(reason) => {
assert!(!reason.is_empty());
}
}- Inside an async task, where the original
awaitthrew, theawaitof a handle returns aResult. The originalrejectisreject(reason)on aPromise<Result<T, E>>. - When only one effect fails, such as a shader that cannot be built, it is reported once and skipped. The frame does not stop.
- Failed assets still count as finished so the scene does not get stuck, the same as the original.
Option Structs Instead of Option Objects
The original takes optional arguments as an object such as { decayRate: 9, frequency: 42 }. Rust has no named optional arguments, so functions take an option struct with default values. Write only what you change and fill the rest with ..default.
const shaker = new Shaker({ decayRate: 10 });
shaker.addShake(8);use vanilla::{Shaker, ShakerOptions};
let mut shaker = Shaker::new(ShakerOptions {
decay_rate: 10.0,
..ShakerOptions::default()
});
shaker.add_shake(8.0);
assert!(shaker.is_shaking());Option structs such as ToonDrawOptions, RenderTargetOptions, UIConfirmWindowOptions, and FloatingTextOptions all follow this pattern. When the engine adds a field, code built with ..Default::default() keeps working.
async/await and Reflection
- The original
async load,await asset.load(path),await Wait.seconds(1), andfadeScreen(async () => ...)port to Rustasync/awaitof the same shape. The difference is that instead of usingthisright on the line afterawait, you touch it insidescene_context.with(|scene, engine| ...). The details are in the "async/await" chapter. - The original scans the
get,is, andsetfunctions of every object at run time. In Vanilla Engine only types with#[vanilla::reflect]on the component'simplblock andimpl Componentblock are reflected. Add the macro to game components that you save in UI documents or show in the developer tools inspector. The details are in the "Reflection Macro #[reflect]" chapter.
Files Both Engines Share
Asset files and files saved by the authoring tools are read and written in the same format by both engines. Leave the files as they are when porting.
| File | Made by (original side) | Read in Vanilla Engine by |
|---|---|---|
UI document .scene.json |
The UI editor (tools/uieditor), UIDocument.toJsonText |
UIDocument::from_json_text, to_json_text (the output matches the original's text down to key order, tab indentation, and number formatting). The kinds the editor registers take one line: register_editor_types |
Timeline document .timeline.json |
The timeline editor (tools/timelineeditor) |
Timeline::load_from_text, Timeline::load_from_url |
| Sprite atlas JSON and page images | tools/atlas.cjs |
SpriteAtlas::load (the same two formats as the original) |
Language packs: languages.json and per-language files |
The game's assets | languagepack (load_language_list, load_language) |
UI font lists fonts.json, fonts-<group>.json |
The game's assets | uifont (load_ui_font) |
Build information version.json |
The release build | BuildInfo::load |
- Asset paths are the same relative paths as in the original. The desktop reads straight from the folders in
EngineConfiguration.asset_directory_paths, and the web fetchesasset_base_urljoined with the path, like the original. The game code is the same. - The authoring tools and file formats are covered in the "Authoring Tools and Files Shared by Both Engines" chapter.
Original Files That Were Not Ported
Original src/ files with no Rust file of the same name were either merged into other files or deliberately not ported (the full list is in chapter 9 of the repository's ARCHITECTURE.md).
| Original | Vanilla Engine |
|---|---|
base/node.js, core/node/componentnode.js, core/node/transformnode.js |
Merged into WorldNode and NodeTree |
core/scenemanager.js |
Engine's load_scene, unload_scene, get_all_loaded_scenes (SceneId instead of scene objects) |
core/touchraycaster.js |
Merged into SceneBase::tick_touch, NodeTree's press test, and TouchRecognizer |
resource/jsonasset.js |
JsonAsset in the same file as TextAsset |
base/object.js, base/singleton.js, Dictionary, List, Queue, Set, Stack in base/collection |
The Rust language and standard library (traits, Any, enum, Vec, HashMap, and so on) do the same job |
base/reflection.js |
Run-time reflection is #[vanilla::reflect]; createGUID is create_guid |
deprecated/camera.js |
Camera2D covers it, including rotation and begin/end |
Work the browser did (2D canvas, window.alert, confirm, prompt, html lang and meta) |
Canvas2D, Engine::alert, confirm, prompt (and _async), Document |
Related Types
Scene,SceneBase,GameScene,GameSceneBase,UIScene,UISceneBase,game_scene!,ui_scene!Component,ComponentBase,ComponentContext,Asset,AssetBaseNodeTree,NodeId,WorldNode,NodeTransformEngine,EngineConfiguration,ViewManagerUIButtonEvent,Shaker,ShakerOptions,Cooldown,Colors,Color,PivotUIDocument,Timeline,SpriteAtlas,BuildInfo,languagepack,uifont