Structure of LUA files

LUA files, regardless of whether they are for buildings, units, etc., always have the same structure.

## Example file - Morphium Tower

Let's take the following file as an example, the lua file for the morphium tower, "tower\_alien\_influence.lua"

```
local tower = require("lua/buildings/defense/tower.lua")
require("lua/utils/table\_utils.lua")

class 'tower\_alien\_influence' ( tower )

function tower\_alien\_influence:\_\_init()
	tower.\_\_init(self,self)
end

function tower\_alien\_influence:OnInit()
    tower.OnInit(self)

    self.cfsm = self:CreateStateMachine()
    self.cfsm:AddState( "checker", { execute="OnExecuteChecker", interval=1.0 } )
    self.cfsm:ChangeState("checker")
    self.created = false

    self.active = true
	self.selected = {}
    self.radius = self.data:GetFloatOrDefault("radius", 6)
	self.interval = self.data:GetFloatOrDefault("interval", 1)
	self.shieldBp = self.data:GetStringOrDefault("shield\_bp", "buildings/defense/shield\_generator/shield")
	self.healthChild = INVALID\_ID

    self.fsm = self:CreateStateMachine()
	self.fsm:AddState( "working", {execute="OnWorkInProgress",exit="OnWorkExit", interval=self.interval} )

	self:RegisterHandler( self.entity, "SpikeAmmoFiredEvent",  "OnSpikeAmmoFiredEvent" )
end

function tower\_alien\_influence:OnSpikeAmmoFiredEvent( evt )
	AnimationService:StartAnim( self.entity, "working", false )
end

function tower\_alien\_influence:OnExecuteChecker( state )
    if (self.working == self.created ) then
        return
    end

    if ( self.working ) then
		local workingTime = BuildingService:GetWorkingTime( self.entity )
        if ( workingTime and workingTime > 1.0 ) then
            local influenceComponent =EntityService:CreateComponent(self.entity, "InfluenceComponent")
            local influenceComponentHelper = reflection\_helper(influenceComponent)
            influenceComponentHelper.radius = 13
            influenceComponentHelper.energy\_radius = 0
            influenceComponentHelper.emissive\_radius = 5
            self.created = true
        end
    else
	    EntityService:RemoveComponent(self.entity, "InfluenceComponent")
        self.created = false
    end

end

function tower\_alien\_influence:OnWorkInProgress( state )
	if ( self.healthChild == INVALID\_ID or HealthService:GetHealth( self.healthChild)== -1 ) then
		self.healthChild = EntityService:SpawnAndAttachEntity( self.shieldBp, self.entity )
		return
	end
	local objects = FindService:FindEntitiesByTypeInRadius( self.entity, "building", self.radius )

	for i = 1, #objects do
		if( IndexOf( self.selected, objects[i] ) == nil and BuildingService:IsBuildingFinished( objects[i] ) )
		then
			ItemService:AddHealthLink( objects[i], self.healthChild )
			Insert( self.selected, objects[i] )
		end
	end

	for i = #self.selected,1,-1  do
		if( IndexOf( objects, self.selected[i] ) == nil ) then
			ItemService:RemoveHealthLink( self.selected[i], self.healthChild )
			Remove( self.selected, self.selected[i] )
		end
	end
end

function tower\_alien\_influence:OnWorkExit( state )
	for i = #self.selected,1,-1  do
		ItemService:RemoveHealthLink( self.selected[i], self.healthChild )
	end
	Clear( self.selected )
end

function tower\_alien\_influence:OnActivate()
	self.fsm:ChangeState("working")
end

function tower\_alien\_influence:OnDeactivate()
	local state = self.fsm:GetState("working")
	if ( state ~= nil ) then
		state:Exit()
	end
end

return tower\_alien\_influence

```

## Explanation

Line 1 is needed since the morphium tower is a tower and requires the normal tower functions as a base to start on.
 4
 6 - 8
 10 - 29
 30 - 97
 99