User Tools

Site Tools


custom-building-function

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
custom-building-function [2022/03/30 10:33] – created minotoriouscustom-building-function [2022/04/30 19:23] (current) minotorious
Line 1: Line 1:
-[[api/building_function|BUILDING_FUNCTION]] classes are registerable types in Foundation that define the function a.+[[api/building_function|BUILDING_FUNCTION]] classes are registerable types in Foundation that define what purpose  certain [[api/building_part|BUILDING_PART]] will serve in the game.
  
 For reference all available [[api/building_function|BUILDING_FUNCTION]] classes can be found in [[api/building_function|this]] section of the API. For reference all available [[api/building_function|BUILDING_FUNCTION]] classes can be found in [[api/building_function|this]] section of the API.
  
-===== Creating & Using a Custom BUIDING_FUNCTION ===== +===== Creating & Using a Custom BUILDING_FUNCTION ===== 
-==== Step 1: Defining an BUILDING_FUNCTION Class using a Lua Table ====+==== Step 1: Defining BUILDING_FUNCTION Class using a Lua Table ====
 To create a custom [[api/building_function|BUILDING_FUNCTION]] class we will need to make use of the ''mod:registerClass(AssetTable)'' function providing it with an associated Lua Table. To create a custom [[api/building_function|BUILDING_FUNCTION]] class we will need to make use of the ''mod:registerClass(AssetTable)'' function providing it with an associated Lua Table.
  
-Here you decide what properties your custom [[api/building_function|BUILDING_FUNCTION]] object will contain.+Here you decide what properties your custom [[api/building_function|BUILDING_FUNCTION]] object will contain. Furthermore you can override default [[api/building_function|BUILDING_FUNCTION]] methods as well as define custom ones.
 <code lua> <code lua>
 local MY_BUILDING_FUNCTION = { local MY_BUILDING_FUNCTION = {
Line 18: Line 18:
     }     }
 } }
 +</code>
 +Now that the base [[api/building_function|BUILDING_FUNCTION]] Lua Table is ready we can define functions for it before registering it.
  
 +All [[api/building_function|BUILDING_FUNCTION]]s have three base functions that can be overriden, ''activateBuilding'', ''reloadBuildingFunction'', and ''removeBuildingFunction''.
 +
 +''activateBuilding'' is called the first time a [[api/building_function|BUILDING_FUNCTION]] is instantiated in game be it because the [[api/building_part|BUILDING_PART]] it was defined on was built or because it was assigned by the player. This function can be used to set up any necessities for the [[api/building_function|BUILDING_FUNCTION]] to work, for example in the case of [[api/building_function_workplace|BUILDING_FUNCTION_WORKPLACE]] this function creates the [[api/comp_workplace|COMP_WORKPLACE]] necessary for assigning workers to the [[api/building_part|BUILDING_PART]].
 +
 +''reloadBuildingFunction'' is called upon save game loading. This function usually serves as a back-call to the ''activateBuilding'' function to ensure any necessities are present and initialised after a reload.
 +
 +''removeBuildingFunction'' is called when a [[api/building_function|BUILDING_FUNCTION]] is manually unassigned by the player. This function is used to remove components or other elements that should no longer be present once the function is removed from the [[api/building_part|BUILDING_PART]].
 +
 +For example below we set up a function that upon activation logs its properties and upon reload executes a back-call to its activation function.
 +<code lua>
 +function MY_BUILDING_FUNCTION:activateBuilding(gameObject)
 +    mod:log("Property1: " .. tostring(self.Property1))
 +    mod:log("Property2: " .. tostring(self.Property2))
 +    mod:log("Property3: " .. tostring(self.Property3))
 +    
 +    return true
 +end
 +
 +function MY_BUILDING_FUNCTION:reloadBuildingFunction(gameObject)
 +    self:activateBuilding(gameObject)
 +end
 +</code>
 +And finally we register our custom [[api/building_function|BUILDING_FUNCTION]] class.
 +<code lua>
 mod:registerClass(MY_BUILDING_FUNCTION) mod:registerClass(MY_BUILDING_FUNCTION)
 </code> </code>
Line 41: Line 67:
 In case you omit any properties you don't yet need to explicitly set they will use their default values. As we have done with ''Property2'' above. In case you omit any properties you don't yet need to explicitly set they will use their default values. As we have done with ''Property2'' above.
  
-==== Step 3: Using an Instance of an BUILDING_FUNCTION Class ====+==== Step 3: Using an Instance of BUILDING_FUNCTION Class ====
 When you want to use your new [[api/building_function|BUILDING_FUNCTION]] instance in a [[api/building_part|BUILDING_PART]] registration you can use the string Id to reference it by name. When you want to use your new [[api/building_function|BUILDING_FUNCTION]] instance in a [[api/building_part|BUILDING_PART]] registration you can use the string Id to reference it by name.
 <code lua> <code lua>
custom-building-function.1648650810.txt.gz · Last modified: 2022/03/30 10:33 by minotorious

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki