Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBP] - Templates for new UDT/Class Management

Author
Message
Chris Tate
DBPro Master
8
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 4th Jul 2015 00:05 Edited at: 8th Jul 2015 22:03
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

The second uses Matrix1 with lookup dictionaries and requires the following helper functions for all similar template implementations: + Code Snippet

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

+ Code Snippet

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.

Login to post a reply

Server time is: 2017-07-22 03:53:17
Your offset time is: 2017-07-22 03:53:17