Components system

From Star Ruler 2 Wiki
Jump to: navigation, search


Components are declared in data/components/ and are defined in server/components/ and shadow/components/.

You declare a method in data/components/ and it gets added to the relevant object class prefixed with Component_, and then automatically redirected to that function in your component class using asynchronous threading.

Cargo as an Example

Object.Cargo in cargo.txt gets added to a non-polymorphic virtual interface of Object in the engine. It defines Component_Cargo for overriding it's interface members.

Any object that has Cargo in its states list in objects.txt gets the methods defined in cargo.txt, which run from (server-side) server/components/ and (client-side) shadow/components/

So although the method getCargo doesn't exist on Object, when you have an Object called obj, if it is an Object.Cargo, calling obj.getCargo() will have the desired result.

It calls Cargo's getCargo() which runs from server/components/ on the server and shadow/components/ on the client.

Method of Operation

The engine intercepts the calls on Object and then sends them through to either the server or client side implementation. This might even be over the network for methods marked "remote", where the GUI calls the method and it gets sent over the internet to the host. It's all so the scripts have to care very little about multiplayer or the fact that objects are spread over many threads.