Jump to content

Say Hello to Leadwerks 5 Shared Objects

Josh

1,182 views

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.



7 Comments


Recommended Comments

Quote

almost easier to use c++ now

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.

Share this comment


Link to comment
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.

Share this comment


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.

Share this comment


Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

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.

  • Blog Entries

    • By Josh in Josh's Dev Blog 0
      What's new
      EAX audio effects for supported hardware. Source class renamed to "Speaker". Plane joint for 2D physics, so now you can make Angry Birds with Vulkan graphics. Fixed DPI issues with fullscreen mode. Added impact noise to barrels, fixed Lua collision function not being called. Script functions now start with "Entity:" instead of "Script:", i.e. Entity:Update() instead of Script:Update(). Additionally, four examples can be run showing various functionality. Double-click on the .bat files to launch a different demo:
      First-person shooter game. 2D physics demonstration. Advanced 2D drawing with text, rotation, and scaling. Multi-camera setup. We're now in a closed beta. Some members have been added to the list of beta testers and can access the private forum now.

    • By reepblue in reepblue's Blog 6
      Loading sounds in Leadwerks has always been straight forward. A sound file is loaded from the disk, and with the Source class emits the sound in 3D space. The sound entity also has a play function, but it's only really good for UI sounds. There is also Entity::EmitSound() which will play the sound at the entity's location. (You can also throw in a Source, but it'll auto release the object when it's done.)
      While this is OK for small games, larger games in which sounds may change might mean you have to open your class, and adjust the sounds accordingly. What if you use the sound in multiple places and you're happy with the volume and pitch settings from an earlier implementation? You could just redefine the source in a different actor, but why should you?
      A solution I came up with comes from SoundScripts from the Source Engine. With that engine, you had to define each sound as a SoundScript entry. This allowed you to define a sound once, and it allowed for other sound settings such as multiple sounds per entry. I thought this over, and with JSON, we can easily create a similar system for Leadwerks 4 and the new engine.
      I first started with a dummy script so I can figure out how I wanted the end result to be.
      { "soundData": { "Error": { "file": "Sound/error.wav", "volume": 1.0, "pitch": 1.0, "range": 0.25 }, "RandomSound": { "files": { "file1": "Sound/Test/tone1.wav", "file2": "Sound/Test/tone2.wav", "file3": "Sound/Test/tone3.wav" }, "volume": 1.0, "pitch": 1.0, "range": 0.25 } } } In this script, we have two sound entries. We have an error sound (Which is suppose to be the fall back sound for an invalid sound entry) and we have a sound entry that holds multiple files. We want a simple, straight forward. entry like "Error" to work, while also supporting something "RandomSound" which can be used for something like footstep sounds.
      The script is streamed and stored into multiple structs in a std::map at the application start. We use the key for the name, and the value is the struct.
      typedef struct { std::string files[128]; char filecount; float volume; float pitch; float range; bool loopmode; } sounddata_t; std::map<std::string, sounddata_t> scriptedsounds; Also notice that we don't store any pointers, just information. To do the next bit, I decided to derive off of the engine's Source class and call it "Speaker". The Speaker class allows us to load sounds via the script entry, and support multiple sounds.
      You create one like this, and you have all the functionalities with the Source as before, but a few differences.
      // Speaker: auto speaker = CreateSpeaker("RandomSound"); When you use Play() with the speaker class and if the sound entry has a "files" table array, it'll pick a sound at random. You can also use PlayIndex() to play the sound entry in the array. I also added a SetSourceEntity() function which will create a pivot, parent to the target entity. From there, the Play function will always play from the pivot's position. This is a good alternative to Entity::EmitSound(), as you don't need to Copy/Instance the Source before calling the function as that function releases the Source as mentioned earlier. Just play the speaker, and you'll be fine! You can also change the sound entry at anytime by calling SetSoundEntry(const std::string pSoundEntryName); The creation of the Speaker class will start the JSON phrasing. If it has already been done, it will not do it again.
      Having sounds being loaded and stored like this opens up a lot of possibles. One thing I plan on implementing is a volume modifier which will adjust the volume based on the games volume setting.Right now, it uses the defined volume setting. It's also a part of another system I have in the works.
    • By Josh in Josh's Dev Blog 1
      I've been doing some work on the sound system in Leadwerks 5 beta, and I added EAX effects in. If you have a dedicated sound card this can be used to add some nice reverb effects that make your sound environment sound a lot more real:
      Here's the simplest usage:
      auto fx = LoadSoundEffect("Sound/FX/sewerpipe.json"); auto listener = CreateListener(world); listener->SetEffect(fx); This will apply the effect to all mono sources. Stereo sources are assumed to be music or GUI noises, and will be unaffected. Eventually, the way I see this being used is a script attached to a CSG brush that changes the listener's EAX effect when the player enters and leaves the volume, but the above shows the API approach.
      I exported all the EAX presets into JSON files like so. You can load one of the existing files, or if you are feeling really creative you can try making your own:
      { "AirAbsorptionGainHF": 0.99426, "DecayHFLimit": 0, "DecayHFRatio": 0.89, "DecayLFRatio": 0.41, "DecayTime": 2.76, "Density": 1.0, "Diffusion": 0.82, "EchoDepth": 0.17, "EchoTime": 0.13, "Gain": 0.316228, "GainHF": 0.281838, "GainLF": 0.0891251, "HFReference": 2854.4, "LateReverbGain": 0.891251, "LateReverbPan": [0.0, 0.0, 0.0], "LFReference": 107.5, "LateReverbDelay": 0.02, "ModulationDepth": 0.0, "ModulationTime": 0.25, "ReflectionsDelay": 0.029, "ReflectionsGain": 0.354813, "ReflectionsPan": [0.0, 0.0, -0.0], "RoomRolloffFactor": 0.0 } Here's the full list of available presets:
      CastleSmallroom CastleMediumroom CastleLongpassage CastleLargeroom CastleHall CastleCupboard CastleCourtyard CastleAlcove FactoryAlcove FactoryShortPassage FactoryMediumRoom FactoryLongPassage FactoryLargeRoom FactoryHall FactoryCupboard FactoryCourtyard FactorySmallRoom IcepalaceAlcove IcepalaceShortPassage IcepalaceMediumRoom IcepalaceLongPassage IcepalaceLargeroom IcepalaceHall IcepalaceCupboard IcepalaceCourtyard IcepalaceSmallRoom SpacestationAlcove SpacestationMediumRoom SpacestationShortpassage SpacestationLongPassage SpacestationLargeRoom SpacestationHall SpacestationCupboard SpacestationSmallRoom WoodenAlcove WoodenShortPassage WoodenMediumRoom WoodenLongPassage WoodenLargeRoom WoodenHall WoodenCupboard WoodenSmallRoom WoodenCourtyard SportEmptyStadium SportSquashCourt SportSmallSwimmingPool SportLargeSwimmingPool SportGymnasium SportFullStadium SportStadiumTannoy Workshop SchoolRoom PractiseRoom Outhouse Caravan Dome Tomb PipeSmall DomeSaintPauls PipeLongThing PipeLarge PipeResonant OutdoorsBackyard OutdoorsRollingPlains OutdoorsDeepCanyon OutdoorsCreek OutdoorsValley MoodHeaven MoodHell MoodMemory DrivingCommentator DrivingPitGarage DrivingInCarRacer DrivingInCarSports DrivingFullGrandstand DrivingEmptyGrandstand DrivingTunnel CityStreets CitySubway CityMuseum CityLibrary CityUnderpass Dustyroom Chapel SmallWaterRoom I might consider implementing Steam Audio in the future (formerly Phonon) but for now OpenAL does everything I want.
×
×
  • Create New...