Say Hello to Leadwerks 5 Shared Objects
All classes in Leadwerks are derived from a base Object class. In Leadwerks 5 we separate simple and complex objects with the new SharedObject class.
Simple objects like a Vec3 ( a three-dimensional vector), an AABB (axis-aligned bounding box), and other items are all derived from the Object class. Simple objects are created with constructors. When we make one object equal to another the value is copied from one variable to another, but the two variables are still separate objects. Below, A and B have the same value but are separate objects:
Vec3 a = Vec3(1,2,3); Vec3 b = a;
Shared objects are things you don't want to copy, because they involve more than just some numbers. These always use C++11 shared pointers and use a Create function. Below, A and B refer to the same object:
shared_ptr<World> a = CreateWorld(); shared_ptr<World> b = a;
The SharedObject class has a couple of functions to make life easier. Instead of casting pointers with some funny syntax we can use the Cast() method. Here's an example of casting in Leadwerks 4:
Entity* entity = Model::Load("car.mdl"); Model* model = (Model*)entity;
And here's how it works in Leadwerks 5:
shared_ptr<Entity> entity = LoadModel("car.mdl"); shared_ptr<Model> model = entity->Cast<Model>();
Instead of using "this" inside a class method you can use Self() to get a shared pointer to the object itself:
class MyActor : public Actor { void MyFunction() { //MyActor* me = this; shared_ptr<SharedObject> me = Self(); } }
Self() will always return a shared_ptr<SharedObject> value, so you can use Cast() if you need a specific type of object (and match the behavior of "this"):
class MyActor : public Actor { void MyFunction() { //MyActor* me = this; shared_ptr<MyActor> me = Cast<MyActor>(); } }
Instead of calling delete or Release to destroy a shared object, all you have to do is set its variable to NULL:
shared_ptr<Model> model = LoadModel("car.mdl"); model = NULL;// poof!
And of course we can always use the auto keyword to make things really simple:
auto model = LoadModel("car.mdl");
Shared objects use automatic reference counting to give you the ease of use of a garbage-collected language, together with the blazing performance of modern C++. These features are set to make Leadwerks Game Engine 5 the easiest and most cutting-edge development system in the history of game programming.
- 3
6 Comments
Recommended Comments