Keen:Entity Component Overview: Difference between revisions
CptTwinkie (talk | contribs) mNo edit summary |
CptTwinkie (talk | contribs) mNo edit summary |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
{{SEO|image_url=http://www.medievalengineerswiki.com/images/ | {{SEO|image_url=http://www.medievalengineerswiki.com/images/b/b7/KeenLogoBig.png|description=Entity components consist of a couple of important methods, and it is important to know what they do and what their intended purpose is.}} | ||
{{Keen:OCH}} | {{Keen:OCH}} | ||
Line 33: | Line 33: | ||
<br /> | <br /> | ||
Example:<br /> | Example:<br /> | ||
< | <syntaxhighlight lang="csharp" collapse="false" line> | ||
private MyNameOfComponentDefinition m_definition = null; | private MyNameOfComponentDefinition m_definition = null; | ||
Line 40: | Line 40: | ||
m_definition = definition as MyNameOfComponentDefinition; | m_definition = definition as MyNameOfComponentDefinition; | ||
} | } | ||
</ | </syntaxhighlight> | ||
==Serialize / Deserialize== | ==Serialize / Deserialize== | ||
The Serialize and Deserialize methods are called when the game is being saved or loaded respectively. Serialize returns an object builder which you can receive through base.Serialize(copy). Deserialize receives the object builder from the world and you can read your data from there.<br /> | The Serialize and Deserialize methods are called when the game is being saved or loaded respectively. Serialize returns an object builder which you can receive through base.Serialize(copy). Deserialize receives the object builder from the world and you can read your data from there.<br /> | ||
Line 48: | Line 48: | ||
<br /> | <br /> | ||
Example:<br /> | Example:<br /> | ||
< | <syntaxhighlight lang="csharp" collapse="false" line> | ||
public override bool IsSerialized | public override bool IsSerialized | ||
{ | { | ||
Line 71: | Line 71: | ||
return ob; | return ob; | ||
} | } | ||
</ | </syntaxhighlight> | ||
<center><font style="font-size:80%; color: #bd3232;">Warning: Mods with custom object builders</font></center> | <center><font style="font-size:80%; color: #bd3232;">Warning: Mods with custom object builders</font></center> | ||
Line 82: | Line 82: | ||
<br /> | <br /> | ||
Example:<br /> | Example:<br /> | ||
< | <syntaxhighlight lang="csharp" collapse="false" line> | ||
public override void OnAddedToScene() | public override void OnAddedToScene() | ||
{ | { | ||
Line 98: | Line 98: | ||
MyUpdateComponent.Static.RemoveFromUpdate(Update); | MyUpdateComponent.Static.RemoveFromUpdate(Update); | ||
} | } | ||
</ | </syntaxhighlight> | ||
[[Category:Keen_Modding_Guides]] | [[Category:Keen_Modding_Guides]] |
Latest revision as of 20:16, 18 July 2022
OFFICIAL CONTENT NOTICE |
---|
OFFICIAL CONTENT This article contains official content from or verified by the developers at Keen Software House. This information is intended to be accurate at the time it is posted, but may become obsolete over time. If you find errors in this article please describe the errors in the Discussion Page. |
Entity components consist of a couple of important methods, and it is important to know what they do and what their intended purpose is.
Version: | 0.5 |
Attributes
Entity Components can be decorated with some attributes which in turn inform the game about important things regarding this entity component. Here follows a non-exhaustive list of Entity Component attributes:
MyComponent
Description: MyComponent attribute marks this class as an Entity Component. The type passed into MyComponent is the save data object builder used for saving data to the world.
Usage: [MyComponent(typeof(MyObjectBuilder_NameOfComponent)]
ReplicatedComponent
Description: ReplicatedComponent attribute marks this Entity Component as relevant for Replication. Required for sending messages across the network.
Usage: [ReplicatedComponent]
Constructor
The class constructor is a core C# feature that is called on each instantiation of the class. This goes for Entity Components as well.
The intended purpose of the constructor should be to initialize variables to their default values. It should never interface with any elements outside the entity component.
Init
The Init method is called when the entity component is initialized externally. This means that certain elements of the game are available.
Additionally, the argument for the Init method is the definition of the entity component. This is the parsed data from the SBC file, converted into a Definition class.
When an entity component is being initialized it is not yet assigned to its parent Entity, and thus the Entity parameter may still be null.
Additionally, when entity components are being initialized, the game may still be loading, and some elements of the game are not yet ready.
You should never interact with any entity components external to this entity component from the Init method.
In many places the game relies on the Init method only being called once. You should never call the Init method from within your code, doing so may cause unpredictable behaviour and instability!
Example:
private MyNameOfComponentDefinition m_definition = null;
public override void Init(MyEntityComponentDefinition definition)
{
m_definition = definition as MyNameOfComponentDefinition;
}
Serialize / Deserialize
The Serialize and Deserialize methods are called when the game is being saved or loaded respectively. Serialize returns an object builder which you can receive through base.Serialize(copy). Deserialize receives the object builder from the world and you can read your data from there.
The ObjectBuilder that is used by these methods are specified by the [MyComponent] attribute at the top.
Please be aware that Serialize and Deserialize are not called unless you override IsSerialized and return true.
Example:
public override bool IsSerialized
{
get { return true; }
}
public override void Deserialize(MyObjectBuilder_EntityComponent builder)
{
base.Deserialize(builder);
MyObjectBuilder_NameOfComponent myObjectBuilder = builder as MyObjectBuilder_NameOfComponent ;
m_myImportantVariable = myObjectBuilder.ImportantVariable;
}
public override MyObjectBuilder_EntityComponent Serialize(bool copy = false)
{
var builder = base.Serialize(copy);
MyObjectBuilder_NameOfComponent myObjectBuilder = builder as MyObjectBuilder_NameOfComponent ;
myObjectBuilder.ImportantVariable = m_myImportantVariable;
return ob;
}
OnAddedToScene / OnRemovedFromScene
The OnAddedToScene method is called whenever an entity component's parent entity is finished initializing and is added to the scene. At this point it is safe to interact with the parent Entity as well as other entity components and elements in the game. This is the moment where it is possible to register to events, get references to other components, etc.
OnRemovedFromScene is called right after an entity was taken out of the world. If you registered to any events in OnAddedToScene you should remove them here.
Example:
public override void OnAddedToScene()
{
base.OnAddedToScene();
// Register for update once every 1000 milliseconds (1 second)
MyUpdateComponent.Static.AddForUpdate(Update, 1000);
}
public override void OnRemovedFromScene()
{
base.OnRemovedFromScene();
// Unregister for updates
MyUpdateComponent.Static.RemoveFromUpdate(Update);
}