There must be a logic behind every code. By trial and error you will never get far, and probably will result in a defective code. I will try to explain the basic for you.
First of all you must understand that a code is constantly running. By default Duke 3D works at 30 time units (tics) per second, meaning that if you simply add a code inside of an actor (sprite), the code will run 30 times each second.
useractor notenemy MYENEMY
addphealth 1
enda
The code above would basically increase the player health 1 point to a maximum of 100 points, and do so every tic, 30 times per second. There is no condition to it, so the moment you start the level, you would feel the effects.
An if condition basically makes the code works only under a specific circumstance. Example:
useractor notenemy MYENEMY
ifpdistl 1024
addphealth 1
enda
Here I am telling the game to only heal the player if his distance to the current actor is less than 1024 Build units (same as the map editor). However the command would still run 30 times every second non-stop once you meet the condition, but I will return to this subject later.
An else is used for the opposite effect. Example:
useractor notenemy MYENEMY
ifpdistl 1024
addphealth 1
else
addphealth 10
enda
This would heal the player by 10 points if the player is at a distance of 1024 units to the actor, otherwise it will be just 1 point.
A nullop is a void command, and is used as a hack to tell the game to do nothing. Example:
useractor notenemy MYENEMY
ifpdistl 1024
nullop
else
addphealth 1
enda
This basically tells the game to do nothing if the player is at less than 1024 units away, otherwise heal the player. In other words, it is the same as telling the game that the code should work only the player is 1024 or
more units away. Of course this is just an example, there is a ifpdistg command so you wouldn't need to do that in this case.
The {} are used to enclose a piece of the code that works under the if condition. This is because by default an if condition only take affect on the first command used after it (normally on the first line below it). In other words, the {} you want to make the same condition apply to multiple commands. For example:
useractor notenemy MYENEMY
ifpdistl 1024
{
addphealth 1
addammo PISTOL_WEAPON 1
}
enda
The code above heal the player by 1 point and increase the Pistol ammo by 1 shot if the player is at a distance of 1024 units. Below is a code that does the same thing, the difference is that the code above is more pleasing — and efficient.
useractor notenemy MYENEMY
ifpdistl 1024
addphealth 1
ifpdistl 1024
addammo PISTOL_WEAPON 1
enda
Now you must understand how to use the {} with multiple if conditions.
useractor notenemy MYENEMY
ifpdistl 1024
ifp pfacing
{
addphealth 1
addammo PISTOL_WEAPON 1
}
enda
The code above is very simply, the two commands will only work if the player is at a distance of 1024 units and is facing the current actor. However you can change it:
useractor notenemy MYENEMY
ifpdistl 1024
{
addphealth 1
ifp pfacing
addammo PISTOL_WEAPON 1
}
enda
Now the player health will be increased by 1 health if he is at a distance of 1024 and facing the actor, however the ammo will only be increased if you are at a distance of 1024 regardless regardless of the direction the player is facing.
useractor notenemy MYENEMY
ifpdistl 1024
{
ifp pfacing
addphealth 10
else
addammo PISTOL_WEAPON 1
}
else
addphealth 1
enda
Above is a more complex example of how to handle the {} and if conditions. Inside the condition of the player at a distance of 1024 units, if the player is facing the current actor increase the health by 10, otherwise increase the ammo by 1; if the condition of the player being at a distance of 1024 is false, then increase the health by 1.
And here is an example of a series of if conditions, which is used a lot in GAME.CON:
useractor notenemy MYENEMY
ifpdistl 1024
addphealth 100
else
ifpdistl 2048
addphealth 50
else
ifp pfacing
addphealth 10
else
addphealth 1
enda
The code above tells the game to increase the health by 100 if the player is at a distance of 1024 units, otherwise increase the health by 50 if the player at a distance of 2048 units, otherwise (i.e. the two previous conditions must be false) increase the health by 10 if the player is facing the actor direction, and if none of these conditions are true, increase the health by 1.
Now let's talk about the code running non-stop as I mentioned back then. That what you use actions for.
useractor notenemy MYENEMY
ifpdistl 1024
ifaction 0
{
addphealth 1
action AMYENEMY_STOP
}
enda
By default an actor starts with an action that equals zero, that's why I check if it is zero. Once the distance of 1024 units is meet, increase the player health by 1 point, and make the current actor action AMYENEMY_STOP. As a result, this code will only run a single time in the life.
Now here is a code that would make the code stop once the condition is meet, however it will be reset if the player is 1024 away again. Meaning that the player must walk forth and back to make the code works multiple times.
useractor notenemy MYENEMY
ifpdistl 1024
{
ifaction 0
{
addphealth 1
action AMYENEMY_STOP
}
}
else
ifaction AMYENEMY_STOP
action 0
enda
I hope this helped you to understand the logic behind the CON code.