Posted: 4th Jul 2015 0:05
This user defined type array template can be used as a starting point for new 'classes' in your DBP program so you do not need to rewrite similar code for separate UDTs with similar utility functions. The template has some optional functionality you can omit; for example, teams may not need an update time and cameras may not need names.

Two important notes about this template set is that is the use of 1 based array systems, with item zero as a default instance with properties you can set for all new instances to be initialized with (such as monsters of a certain group all requiring a certain amount of hitpoints); but item zero never exists according to the template's existence check. The second note is that the templates feature an optional time requirement on class update calls to reduce excessive updates on certain class instances; a feature which should not be overused to the point of counter productivity by calling the timer function too often with multitudes of class instances, but handy nonetheless when such calls need not occur more than 20 or so times per second.

There are two versions of the template; the first for normal DBP usage, and the second for use with Matrix1; which also provides an option to add a class instance to a lookup dictionary; something you can use to find a class instance by group and ID. The first snippet names your instances with one string; the second names them with a group and key delimited with a dot. Choose the best version for the job, I tend to use the latter for class instances I want to group easily in the INI enhanced Matrix1 dictionary system and have searchable by group.

The snippets are supposed to be placed conveniently at hand for every time you create a new class, something the Indigo IDE has implemented rather well.

The snippet is used by performing a search and replace on all occurances of the field letter ;'k' in the first snippet, and 'q' in the second version for Matrix1. Replace these letters with the name of your class using the auto search and replace feature of your IDE. All entries of the letter 'k' in the template should place-hold all of the required prefixes for variables, functions and sub-routines accordingly; but you will need to fix any plural forms of your class name if it must end in 'es' or 'ies' such as with the name entities.

For example, performing a search and replace on all occurances of 'k' with the word 'player' will provide you with the following functions:

SetupPlayers: : A sub routine which initializes your players

AddPlayer( name, updateDelay ) : Adds a new player

PlayerExist(id) : Returns 1 if the player exists

Player$(id) : A debugging function which returns the name and ID of a player as a string even if the player does not exist; in which case the player would be named Undefined_Player[Index:id]

UpdatePlayers: : A sub routine to place your player update calls



The next example demonstrates all entries of 'k' replaced with 'Monster'

SetupMonsters: : A sub routine which initializes your Monsters

AddMonster( name, updateDelay ) : Adds a new Monster

MonsterExist(id) : Returns 1 if the Monster exists

Monster$(id) : A debugging function which returns the name and ID of a Monster as a string even if the Monster does not exist; in which case the Monster would be named Undefined_Monster[Index:id]

UpdateMonsters: : A sub routine to place your Monster update calls


This first snippet does not use Matrix1 or any lookup dictionary features
+ Code Snippet
//==//////////////////////////////////////////////////////////////////////==//
Type kType
  Name$
  UpdateDelay
  UpdateDue
Endtype

Setupks:
  Global Dim ks(0) as kType
Return
//==================================================
Function kExist(id)
  If id > 0
    If id <= Array Count(ks())
      ExitFunction 1
    EndIf     
  EndIf   
EndFunction 0
//==================================================
Function Addk( sName$, updateDelay )
  Array Insert At Bottom ks() : n = Array Count(ks())
  ks(n) = ks(0)
  If sName$ = ""
    sName$ =  "k[" + Str$(n) + "]"
  EndIf
  ks(n).Name$ = sName$
EndFunction  n
//==//////////////////////////////////////////////////////////////////////==//
Updateks:
  Local iCount
  Local id

  iCount = Array Count(ks())
  For id = 1 to iCount
    If UseInt( ks(id).IEnabled )    

      If ks(id).UpdateDue <= HiTimer()
        //----------------------

        //----------------------
        ks(id).UpdateDue = HiTimer() + ks(id).UpdateDelay
      EndIf

    EndIf
  Next id
  
Return 

//==================================================
Function k$(id)
  Local s$
  If kExist(id)
    s$ = ks(id).Name$
    If s$ = "" Then s$ = "k"
    s$ = s$ + "[Index: " + Str$(id) + "]"
  Else
    s$ = "Undefined_k[Index: " + Str$(id) + "]"
  EndIf   
EndFunction s$
//==================================================
Function Findk(sName$)
  n = Array Count(ks())
  Local id
  sName$ = Lower$( sName$ )
  For id = 1 to n
    If Lower$( ks(id).Name$ ) = sName$
      ExitFunction id
    endif
  next id
EndFunction 0


The second uses Matrix1 with lookup dictionaries and requires the following helper functions for all similar template implementations:
+ Code Snippet
//==================================================
Function SetKey( iLookup, group$, key$,  iVal )
  If Lookup Exist(iLookup)
    If Val( Lookup$( iLookup,  group$ + "/" + key$ , "") ) <> iVal
      Set Lookup iLookup,  group$ + "/" + key$, Str$(iVal)
    Endif
  Else
    // Error("Failed to set dictionary key " + group$ + "." + key$ + " because the lookup dictionary does not exist. ID: " + Str$(iLookup) )
  Endif
EndFunction
//==================================================
Function TryKey( iLookup, sGroup$ , k$, iDefault )
  If SEARCH LOOKUP ( iLookup, sGroup$ + "/" + k$ )    
    i = Val(Lookup$( iLookup, sGroup$ + "/" + k$, Str$(iDefault)))
  EndIf
EndFunction i
//==================================================
Function UseKey( iLookup, sGroup$ , key$ )
  If Lookup Exist(iLookup)
    i = Val( Lookup$( iLookup, sGroup$ + "/" + key$, "0") )
  Else
    i = 0
    Error("Failed to access dictionary key " + key$ + " because the lookup dictionary does not exist. ID: " + Str$(iLookup) )
  Endif
EndFunction i


After you added the functions in the second snippet box; this third snippet box contains the actual Matrix1 template:

+ Code Snippet
Type qType
  Name$
  UpdateDelay
  UpdateDue
Endtype

//==//////////////////////////////////////////////////////////////////////==//
Setupqs:
  Global Dim qs(0) as qType
  Global qData : qData = Reserve Free Lookup()
  Make Lookup qData
Return
//==================================================
Function qExist(id)
  If id > 0
    If id <= Array Count(qs())
      ExitFunction 1
    EndIf     
  EndIf   
EndFunction 0
//==================================================
Function Addq( sGrp$, sKey$, iUpdateDelay )
  Array Insert At Bottom qs() : n = Array Count(qs())
  qs(n) = qs(0)
  Local sq$ : sq$ = sGrp$ + "." + sKey$
  qs(n).Name$ = sq$
  qs(n).UpdateDelay = iUpdateDelay
  SetKey( qData, sGrp$, sKey$, n)
EndFunction  n
//==//////////////////////////////////////////////////////////////////////==//
Updateqs:
  Local iCount
  Local id

  iCount = Array Count(qs())
  For id = 1 to iCount
    

    If qs(id).UpdateDue <= HiTimer()
      //----------------------

      //----------------------
      qs(id).UpdateDue = HiTimer() + qs(id).UpdateDelay
    EndIf

    
  Next id
  
Return 

//==================================================
Function q$(id)
  Local s$
  If qExist(id)
    s$ = qs(id).Name$
    If s$ = "" Then s$ = "q"
    s$ = s$ + " [Index: " + Str$(id) + "]"
  Else
    s$ = "Undefined_q [Index: " + Str$(id) + "]"
  EndIf   
EndFunction s$

//==================================================
Function Findq(sName$)
  n = Array Count(ks())
  Local id
  sName$ = Fast Lower$( sName$ )
  For id = 1 to n
    If Fast Lower$( ks(id).Info.Name$ ) = sName$
      ExitFunction id
    endif
  next id
EndFunction 0

//==================================================
Function FindqByKey(sGroup$, sKey$)
  id = UseKey(qData, sGroup$, sKey$)
EndFunction 0


As you may have guessed, you could reword or add more detail to the template to fit your needs in consideration of the most frequent functions you keep on having to write for new features.