Difference between revisions of "Tips for mod coders"

From Legends of Aria Admin and Modding Wiki
Jump to: navigation, search
(Adding section of type of variables)
m (Constants, attributes and ObjVars)
Line 5: Line 5:
 
RegisterEventHandler(EventType.ModuleAttached,"template_name", function ( ... ) Blah Blah end)
 
RegisterEventHandler(EventType.ModuleAttached,"template_name", function ( ... ) Blah Blah end)
 
</syntaxhighlight>
 
</syntaxhighlight>
* When server starts with a backup, you will have to recover some of the information needed for the good-running of your object. You will have a event handler like below.
+
* When server starts with a backup, you will have to recover some of the information needed for the good-running of your object. You will have an event handler like below.
 
<syntaxhighlight lang="lua">
 
<syntaxhighlight lang="lua">
 
RegisterEventHandler(EventType.LoadedFromBackup,"", function ( ... ) Blah Blah end)
 
RegisterEventHandler(EventType.LoadedFromBackup,"", function ( ... ) Blah Blah end)

Revision as of 11:53, 13 September 2018

Constants, attributes and ObjVars

One thing to consider is the life-cycle of LoA Lua "objects":

  • When server initially starts (with no backups, etc), the module is loaded, you will have an event handler like here under in your code to process the basic initialization of your object.
RegisterEventHandler(EventType.ModuleAttached,"template_name", function ( ... ) Blah Blah end)
  • When server starts with a backup, you will have to recover some of the information needed for the good-running of your object. You will have an event handler like below.
RegisterEventHandler(EventType.LoadedFromBackup,"", function ( ... ) Blah Blah end)

If you haven't saved some of the information needed as ObjVars before a backup, that information will be lost after a restart of the server.
Thus, one thing you better think about, considering the life-cycle of objects, is choosing carefully how object "parameters" are stored in your objects, either as:

  • constants: Those values should not change during the life of your object.
    • Technically in Lua, there is no strict "const", i.e. Immutability, paradigm.
    • The best way to approach it, pragmatically and simply, is to UPPERCASE those constants names at the top of your Lua file to clearly identify them
  • attributes: those are parameters with mutability in time or context dependent value changes
    • You need to clearly identify is such attributes needs to be "transferred" between 2 server executions, or not.
    • If the value of such attributes needs to be recovered, you need to use the "ObjVars" method here under
    • If not, i.e. the value is temporary, you can just create the variable in the Lua code
  • ObjVars: Those are parameters (with a key, i.e. "Name", and a value) that will be saved in the backups and can be recovered after a server restart.
    • The SetObjVar and GetObjvar functions are to be used for that purpose
    • Do not forget to save, i.e. SetObjVar, any value that is modified during the lifetime of your object, esp. tables/arrays.

Looking for objects

In your scripts, you will certainly be needing to find objects. Even though CS provides plenty of functions to search, it might be difficult to find a particular object, esp. when using default templates provided by CS (and thus not modifiable).
The best option, not to have to create a specific template for such object, is to use "Custom Object Variables".
You can either add such variables directly from the SeedObjects.xml file of your module, or do it from Unity.

From Unity:

  1. Load your module, and its Seed Objects (cf. modding guide). Leave the "Seed Objects Editor" window opened.
  2. Select the given Seed Object in your hierarchy or Scene view
  3. in the "Seed Object Editor", press "+" under "Custom Object Variables". This will create a new entry.
  4. You can choose a "Name" for that object, which is going to be the "ObjVar" name in Lua
  5. You can define a type and value, which is going to be the value returned when you call GetObjVar("Name")


The XML that will be generated will look like this in SeedObjects.xml
Note that the "<Name>" tag isn't of use, the Lua GetName() function isn't related to that.

<DynamicObject>
            <Name>Door</Name>
            <Id>12</Id>
            <ObjectCreationParams>tudor_door_dark -20.809 1.714 206.889 0 90 0 1 1.09 1.315</ObjectCreationParams>
             <ObjVarOverrides>
                <StringVariable Name="DoorId">DoorTeam</StringVariable>
            </ObjVarOverrides>
</DynamicObject>

If you want to manually add it, you can edit the SeedObjects.xml file directly (making sure you don't have Unity running and seedobjects loaded) by adding the consistent <ObjVarOverrides> as shown above.

Note: this will just manually add that objvar to the object on creation when you resetworld or start with no backup

To find that object from your Lua script, you can now easily search for the custom object variable:

     local myDoor = FindObject(SearchObjVar("DoorId","DoorTeam"))