Notes on System Verbs


Bitwise Verbs

The Bitwise verbs are useful when you want to use integers as flags to hold your own values. You can use the Bitwise Operator '&' instead of BitTest().

Master Cogs

One cog is designated as the Master Cog so that other cogs in the game can use GetMasterCog() to get a reference to it. These other cogs would use a command like SendMessage(GetMasterCog(), user0); to send a message to the master. Endlevel and startup cogs are usually made the master so they can easily receive messages from other cogs in the level.

Load Resource Verbs

The LoadRes verbs allow you to get a reference to sounds, models, keyframes, and templates without using the symbols section. There is no known difference between a resource loaded with the symbols and a resource loaded by the LoadRes verbs. They will both work the same. But with these verbs, you do not have to define an intermediate variable. So instead of:
temp=LoadTemplate("shieldrecharge");
CreateThing(temp, player);
You can use:
CreateThing(LoadTemplate("shieldrecharge"), player);

Heap Verbs

The Heap Verbs are used to dynamically create storage for variable values. You don't often need to do this in cog, but it is useful when you don't know exactly how many values that you'll have to store. Unfortuneatly, the Heap Verbs cannot be used to create global variables - each cog works with its own heap and no other cog can access it.

Let's say that you want to create five storage slots in memory. You would use HeapNew(0, 5) to create the five new slots. Imagine that it looks like this:

The last memory slot in the heap cannot be used. A sixth slot (slot 5) is created so that we have five available slots.

In advanced programming languages, special "pointer" variables are used to store memory addresses. The variables point to a specific location in memory. But in Cogscript, it is not so complicated; pointers are regular integers which hold the value of a slot number. However, these integers will still be called pointers by this reference because the concept is important to understand.

None of the slots have a value until you use HeapSet(pointer, value) to assign a value to one of the slots. If you try to use HeapGet() to return the value of a slot before it has been assigned a value, JK will crash (or if there is no heap, it will return zero). This next diagram shows the result of HeapSet(0, 999):

Now, the value, 999, is stored in the first memory slot. To retrieve it, use HeapGet(0).

Now suppose that you need to erase ("free") the heap's memory slots. You would use HeapFree(). This verb will remove all of the storage space that JK has allotted for the cog. A cog's heap is not affected by Reset().