Type System |
In this article you will get a brief overview over the articy type system. In a nutshell, it is a faster pre built form of C# reflection. It allows you to get information about types, classes and properties without using C# reflection thus not suffering from its performance impact.
This topic contains the following sections:
As mentioned before it is basically a slight but faster variation of the C# reflection. You can use it to get all meta information about the used types and their properties. The TypeSystem takes advantage of the fact, that the plugin, when importing your data, creates C# classes for every type found in the export. At this point it also builts a database of the type information found when importing, allowing you to access those information much faster at runtime. Lets look at an example.
// grab the type object for Entity var entityType = ArticyTypeSystem.GetArticyType<Entity>(); // iterate over all properties inside the Entity class foreach(var prop in entityType.Properties) { // print out the property name Debug.Log(prop.TechnicalName); }
There are many different ways how to get an ArticyType, using ArticyTypeSystem is just one. Lets see some different ways:
// grab an object var obj = ArticyDatabase.GetObject("Chr_Manfred") as Character; // You can ask each articy object to return its type. var objType = obj.GetArticyType();
The Typesystem (as does reflection) can be a bit hard to understand and especially to understand its potential, later you will get some ideas and see some examples of how you can utilize the type system for your project. But first lets look at more things you can do with the type system:
At the heart of the type system, lies the ArticyType which stores information about a single class or enums that was generated by the plugin to hold your articy:draft project data. The ArticyType stores all relevant information about the represented type, like its TechnicalName, a list of its Properties and provides you with convenience methods to filter out specific properties.
var entityType = ArticyTypeSystem.GetArticyType<Character>(); // access the class name Debug.Log(entityType.TechnicalName); // Prints out "Character" // ask about its features foreach(var featureName in entityType.Features) { // print out the feature technical name Debug.Log(featureName); } // grab only its general properties foreach(var prop in entityType.GetProperties("*", ArticyTypeProperties.General)) { // print out the feature technical name Debug.LogFormat("The property {0} has the Type {1}", prop.TechnicalName, prop.PropertyType); }
While the ArticyType holds info about the type itself, a ArticyPropertyInfo contains info about a specific property found on the ArticyType. The ArticyType has various ways to get one or more properties, like GetProperties. And the ArticyPropertyInfo has different information about the property, like its type, its name and even allows you to change a property on an object in a fully dynamic way.
var characterType = ArticyTypeSystem.GetArticyType<Character>(); // grab the displayname property info ArticyPropertyInfo displayNameProperty = characterType.GetProperty("DisplayName"); // get the type of the property (Its the C# Type object) Debug.Log(displayNameProperty.PropertyType.Name); // Prints out "string" // access a template property ArticyPropertyInfo backstoryProperty = characterType.GetProperty("Basic.Backstory"); Debug.Log(backstoryProperty.IsTemplateProperty); // Prints out "True" // gets it template property constraint, MaxLength var maxLength = backstoryProperty.GetConstraint(ArticyPropertyConstraint.MaxLength); // get an object from the database. object obj = ArticyDatabase.GetObject("Chr_Main"); // using the type property information, we can try to set a property // without directly acessing it. This only works, if the obj actually // has the property, but without our C# code explicitly stating it. backstoryProperty.SetValue(obj, "Once upon a time ...");
Character Sheet
// grab the player character entity var playerCharacter = ArticyDatabase.GetObject("Chr_PlayerCharacter") as Character; ArticyType playerCharacterType = playerCharacter.GetArticyType(); // Access its template properties found in the Attributes feature List<ArticyPropertyInfo> attributes = playerCharacterType.GetPropertiesInFeature<AttributesFeature>(); // iterate over all attributes and create a text label foreach(ArticyPropertyInfo attr in attributes) { // access the displayname of the found property, and then use the property // to access the value from the player character string labelText = string.Format("{0}: {1}", attr.DisplayName, attr.GetValue(playerCharacter)); // ... Do something with the label text, like updating your UI } // Output example: // Strength: 12 // Dexterity: 10 // Intelligence: 8 // Charisma: 9
Simple Save System
// iterate over each object foreach(var obj in ArticyDatabase.Objects) { var objType = obj.GetArticyType(); // start storing information about the object // using Json.NET, or BinaryWriter or something suitable BeginSave(obj.Id, obj.InstanceId); // iterate over each property found on the object foreach(ArticyPropertyInfo prop in objType.Properties(); { // and create a record for it. AddProperty(prop.Name, prop.GetValue(obj)); } EndSave(); }