vanilladocs
vanillapunch.com
API / core/globalengine

core/globalengine

전역 엔진. (원본 Engine.getEngine() 의 System.vanillaEngine 자리. 언리얼 GEngine, 고도 Engine 싱글톤과 같은 꼴)

  • 엔진은 처음 한 번 만들고 지우지 않는다. Engine::new 가 엔진을 프로그램 끝까지 사는 자리('static)에 두고 &'static Engine 을 준다. 그 자리를 스레드 칸에 적어 두어, 만든 직후부터 씬 함수, draw, 컴포넌트, 위젯 콜백, async 작업 안 어디서나 Engine::get_engine() 이 같은 엔진을 준다. 원본처럼 마지막으로 만든 엔진을 준다.
  • 엔진의 공개 함수는 모두 &self 다. 고치는 칸은 안쪽 가변성(Cell, RefCell)으로 둔다. (고도의 러스트 연결 godot-rust 싱글톤과 Gd 의 bind, bind_mut 꼴) 그래서 씬 함수가 받은 engine 인자와 Engine::get_engine() 이 같은 엔진을 동시에 써도 러스트 빌림 규칙이 깨지지 않는다.
  • 매니저(시간, 뷰, 입력, 포인터, 소리, 그래픽)는 RefCell 안에 있다. get_x 는 Ref, get_x_mut 은 RefMut 을 준다. 같은 매니저를 RefMut 으로 쥔 채 또 빌리면(get_x 나 get_x_mut) 함수 이름과 매니저 이름을 적은 패닉으로 멈춘다. 빌린 것은 한 문장 안에서 쓰고 버린다. (let 으로 오래 쥐고 다른 엔진 함수를 부르지 않는다)
  • 다른 스레드에서 부르면 Err(EngineAccessError::OtherThread) 다. (엔진은 만든 스레드 전용이다. Engine 은 Sync 가 아니라 &Engine 을 다른 스레드로 보낼 수 없다. 웹은 스레드가 하나)

The global engine. (in place of System.vanillaEngine of the original Engine.getEngine(). The same form as Unreal's GEngine and Godot's Engine singleton)

  • The engine is created once and never removed. Engine::new puts the engine in a place that lives until the program ends ('static) and gives &'static Engine. That place is written in a thread slot, so right after creation Engine::get_engine() gives the same engine anywhere: scene functions, draw, components, widget callbacks, inside async tasks. Like the original, it gives the last created engine.
  • All public engine functions are &self. The fields they change use interior mutability (Cell, RefCell). (the form of singletons and Gd's bind, bind_mut in godot-rust, Godot's Rust binding) So the engine argument a scene function receives and Engine::get_engine() can use the same engine at the same time without breaking Rust's borrowing rules.
  • The managers (time, view, input, pointer, sound, graphic) are inside RefCell. get_x gives Ref and get_x_mut gives RefMut. Borrowing the same manager again (get_x or get_x_mut) while holding it as RefMut stops with a panic that names the function and the manager. Use what you borrow within one statement and let it go. (do not hold it long with let while calling other engine functions)
  • Called from another thread it is Err(EngineAccessError::OtherThread). (the engine belongs to the thread that created it. Engine is not Sync, so &Engine cannot be sent to another thread. The web has one thread)
예제Example만든 직후부터 어디서나 같은 엔진을 꺼낸다Take out the same engine anywhere right after it is created
use vanilla::{Engine, EngineConfiguration};

let engine = Engine::new(EngineConfiguration::new());
let global_engine = Engine::get_engine().unwrap();
assert!(std::ptr::eq(engine, global_engine));

EngineAccessErrorenum traits Clone, Copy, Debug, PartialEq, Eq, fmt::Display, std::error::Error

소스source

전역 엔진을 꺼내지 못한 까닭. (Engine::get_engine)

The reason the global engine could not be taken. (Engine::get_engine)

이름Name설명Description
NotCreated엔진을 만든 적이 없다. (원본 getEngine 이 undefined 인 자리)No engine was ever created. (where the original getEngine is undefined)
OtherThread엔진은 다른 스레드(엔진을 만든 주 스레드)에 있다. 엔진은 그 스레드에서만 꺼낸다.The engine is on another thread (the main thread that created the engine). The engine is taken only on that thread.