-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c711fba
commit 2ce503c
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Scene {#examples-engine-scene} | ||
|
||
@brief Using the @ref scene-plugin plugin. | ||
|
||
This example shows how the @ref scene-plugin plugin can be used to spawn a | ||
scene from an asset. | ||
|
||
The plugin function is included from the @ref engine/scene/plugin.hpp header. | ||
You may need to include other headers, depending on what you want to access. | ||
|
||
@snippet scene/main.cpp Adding the plugin | ||
|
||
Let's start by taking a look at a scene file. | ||
@include assets/sub.cubos | ||
|
||
A scene file has two parts: the `imports` and the `entities`. | ||
Each entity is a list of components, with their values set. | ||
So in this file we have two entities, one named `root` and the other named `child`. | ||
`root` has a single component, `num`, with a value of 1. | ||
`child` has two components, a `parent` and a `num`. | ||
In this sample, `num` is used so we can later identify the entities. | ||
|
||
@warning Make sure all the components in a scene file are added to CUBOS., or it will raise an error. | ||
|
||
Let's look at a different scene file now, to talk about `imports`, which is the way we can put subscenes within other scenes. | ||
|
||
@include assets/main.cubos | ||
|
||
This file imports the asset with id `cd007ba2-ee0d-44fd-bf36-85c829dbe66f`, which is the scene we looked at in the previous file. | ||
It gives this scene the deisgnation of `sub1`. | ||
It then imports the very same scene again, but this time with the name `sub2` instead. | ||
This allows us to have two instances of the same subscene. | ||
|
||
Under `entities`, we can reference these labels to change some particular values of the subscenes. | ||
For example, by referencing `sub1.root` we are making local changes to the `root` entity of that instance of the subscene. | ||
The result of the changes we make to both `sub1.root` and `sub2.root` is that the parent of these entities will be set to be the `main` entity. | ||
|
||
Now that we have our scene file, let's get CUBOS. to load it. | ||
The first thing we're going to need is a reference to the scene asset. | ||
For the purposes of this sample we can simply use an hardcoded reference to the asset. | ||
|
||
@snippet scene/main.cpp Setting the asset | ||
|
||
Then we'll need a system that spawns that scene. | ||
To do this we simply get the Scene object from the asset, and then spawn its entities. | ||
`commands.spawn` will create in the world a copy of every entitiy defined in the scene. | ||
It won't remove the entities already there, so if you want to close a scene, you'll have to do it yourself. | ||
|
||
@snippet scene/main.cpp Spawning the scene | ||
|
||
And now we'll have this system run at startup. | ||
It is not required, you can spawn scenes at any point in the execution of the program, | ||
but for the purpose of demonstrating the functionality, spawning once at startup is enough. | ||
|
||
@snippet scene/main.cpp Adding the system | ||
|
||
Finally, let's print the loaded scene, to check that everything is working correctly. | ||
|
||
@snippet scene/main.cpp Displaying the scene | ||
|
||
If you run the sample, it should give you a list that has: | ||
- an entity with `num` set to 0, with no parent. This is the `main` entity. | ||
- two entities with `num` set to 1, with same parent, who has `num` set to 0. These are the `root` entities of each instance of the subscene. | ||
- two entities with `num` set to 2, with different parents, but both of them having `num` set to 1. These are the `child` entities of each instance of the subscene. |