Jump to content

Ultra Software Company Blog

  • entries
    185
  • comments
    1,247
  • views
    563,974

Contributors to this blog

Say Hello to Leadwerks 5 Shared Objects


Josh

2,996 views

 Share

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.

  • Upvote 3
 Share

6 Comments


Recommended Comments

On 9/16/2017 at 3:30 PM, jen said:

I would imagine LUA is now being maintained only for backwards compatibility. It would still be advantageous to have LUA, obviously. I just don't think it should be the primary language for the engine's creative environment. Then again marketing the engine with LUA as its primary language bids well to the newbies.

I'm really happy to see the upgrades though, this engine will be super accessible and flexible to use in the near future.

Lua will continue to be important because it is super convenient for entity scripts, and it's great for beginners.  Our implementation in Leadwerks 5 will work with shared pointers which will make everything a lot easier to manage.  With C++ in Leadwerks you have automatic reference counting that does everything except catch circular references.  Lua garbage collection is slower so I don't recommend it for VR or intensive code routines, but it does catch circular references.

  • Upvote 1
Link to comment

Plus I believe you said some time ago that the vast majority of your customers are using Lua, not C++ (it was some high number like 90%).  I imagine that hasn't changed much since.

Link to comment
Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...