RF2 Component Based Entities
Posted: Tue Dec 08, 2009 11:12 pm
After playing with the scripting system a bit, I was able to develop a component based entity system for RF2. A component based entity is an entity that is made up of many components. Think of a Windows GUI application. The application is nothing more than a WinMain() function. The components of the application are what actually do the work. Those components are Forms (Windows), controls (list box, combo box, text box, label, button, etc), logic (external libraries). So for RF2, there will be 2 types of components...hard coded and scripted. Hard coded components will be components that are specific to the RF2 C++ code and can be modified only by editing properties. Scripted components will be made using Squirrel classes and can be anything you want them to be.
So far, I have 2 hard coded components:
-- Actor - assigns a mesh to your entity
-- Animation - assigns a skeleton to your entity and applies animation
Planned components are:
-- Physics - Physics properties for an object
-- Inventory - Object can hold an inventory
-- Brain - AI related properties and scripts
-- Conversation - Object can hold a conversation
-- Debris - Object can leave a mess when it is destroyed
-- Fader - Fades out an object
I don't have any scripted components yet.
There will be a GUI in the RF2 Editor to create these objects and save them to the format they need to be saved in. The format is just a text file that is parsed using a custom parser that is derived from the Ogre Script parser interfaces. The test object is the robot mesh. To define it, I did the following:
The format is simple. The word object tells us to create a new object template and call it Robot. Inside the brackets are the components and their properties. The properties have default values so if you don't want to define one, you don't have to. In this case, I am assigning an actor component and setting the mesh property to robot.mesh and the material property to Examples/Robot. Upon object instance creation, the actor component will use robot.mesh for the mesh file and Examples/Robot as the material.
Components will update every frame with the object. So the hierarchy is as follows:
-- Object instance created from object template
-- Object instance added to world
-- World updates object instances
-- Object instances update component instances
-- Component instances apply logic based on properties
The combinations you could use would be endless and you are not stuck using the same old entities again and again as you can create new components using script. I see this as a great community building tool as the more prominent scripters can create components for the community as components will work from game to game as long as all scripts are included with them.
I used the following Squirrel code to create the object to display in my test scene. You could also change the properties of an object's components to change the behavior of that object during runtime. It will only affect the instance that you are working on and not the template. If you create another object, the changes you made to the previous object will not carry over.
So far, I have 2 hard coded components:
-- Actor - assigns a mesh to your entity
-- Animation - assigns a skeleton to your entity and applies animation
Planned components are:
-- Physics - Physics properties for an object
-- Inventory - Object can hold an inventory
-- Brain - AI related properties and scripts
-- Conversation - Object can hold a conversation
-- Debris - Object can leave a mess when it is destroyed
-- Fader - Fades out an object
I don't have any scripted components yet.
There will be a GUI in the RF2 Editor to create these objects and save them to the format they need to be saved in. The format is just a text file that is parsed using a custom parser that is derived from the Ogre Script parser interfaces. The test object is the robot mesh. To define it, I did the following:
Code: Select all
// test.rfobject
object Robot
{
actor
{
mesh robot.mesh
material Examples/Robot
}
}
Components will update every frame with the object. So the hierarchy is as follows:
-- Object instance created from object template
-- Object instance added to world
-- World updates object instances
-- Object instances update component instances
-- Component instances apply logic based on properties
The combinations you could use would be endless and you are not stuck using the same old entities again and again as you can create new components using script. I see this as a great community building tool as the more prominent scripters can create components for the community as components will work from game to game as long as all scripts are included with them.
I used the following Squirrel code to create the object to display in my test scene. You could also change the properties of an object's components to change the behavior of that object during runtime. It will only affect the instance that you are working on and not the template. If you create another object, the changes you made to the previous object will not carry over.
Code: Select all
local rob = RFObject("Robot");
rob.setPosition(Vector3(0.0, 99.0, 0.0));
rob.setScale(Vector3(0.50, 0.50, 0.50));