Home » Articles » Coding

Anatomy of a uscript Source file
//This page is formatted so that it will compile when pasted directly in to a UC file. To understand compiling, go here:
//http://deusex.ucoz.net/publ/code/starting/1-1-0-1
//===========================================
//Commenting: Lines starting with "//" or paragraphs surrounded by /* and */ are Commented, meaning ignored by compilers.
//Can be used for making your own notes to remember what things do
class MyObject extends AmmoCrate;
/*MyObject is the class name for this object, used for referencing this object in other functions, such as summoning, or accessing its functions and variables from another object. Make it whatever you want, but make sure to follow the rules of:
Keep it short, to save time typing it later
Make sure its unique, the game will get confused if you have two objects called the same thing.
No special characters, letters and numbers only.
The extends Actor means it takes its functions, variables and base coding from that object. Example, Actor is the bare basic object, no inherited stuff, but if you extend DeusExWeapon, it becomes a base for a new weapon, or Mutator makes it function as a mutator. For this example, DeusExDecoration is used, making it act as a decoration, such as ammo crates or lamps. This is important depending on what you WANT it to do, but more specifically, AmmoCrate, because we want the specific properties of the crate as well.
Making the line read "class MyObject extends DeusExDecoration config(MyMod);" turns the Config flag on, and will create an .ini file that contains all the config variables set below, allowing altering at any time, and the word inside the brackets decides the name of the config file, usually best practice is keeping it the same as the mod file, but you can make it anything else, even if it already exists. For example, entering Mutators will make it add on to the Mutators.ini file that most hosts already have, which can be helpful to keep the system tidy.*/

var() string Str;
var int i;
var DeusExPlayer DXP;

//Variables are pieces of information you want the file to remember later, they can be modified and stored but will reset between each instance, meaning every time a new version of this object is created ingame, it'll use the defaults or nothing. The purposes of variables vary, in this case, we are storing a String, meaning a word or words, int meaning Integer, numbers, and a DeusExPlayer, meaning a human player class.
//There's various options for creating variables. Making var in to var() makes the value modifiable in the SDK, when placed in maps through its properties, while making it var config means the value becomes global between all instances of this object and can be modified through an .ini file, only if the config flag is set at the top of the file though.

//a line starting with function is.. well, a function, basically a container for the actual code. In this case, we're only modifying a base function from the master class of DeusExDecoration. Frob is called when a player examines the object. If we were creating a new function, we'd need to Call it somewhere to actually make it happen, which we will do here. The parts inside the brackets are Atributes, slightly more complicated to explain, but in this case, Actor Frobber is set by the player class, telling this object that the Frobber is themself, they're basically temporary variables used for specific things, like telling another class to store a variable to use in one specific function or to send one temporary variable to another function.
function Frob(Actor Frobber, Inventory frobWith)
{
//We wont use it here, but you can create temporary variables inside any function using the same format as var x x, but instead as local x x, these are variables ONLY used inside this function, and will be reset and forgotten every time the function is called.
if(DeusExPlayer(Frobber) != None) //A Failsafe, this line basically says If the frobber variable is not blank, and there IS a frobber, continue
{
DoAThing(str); //A Function call, this is a function we're going to create, and as you can see its calling str as its atribute, this is the variable we set at the top of the file.
i++ //this basically says, add one to i, meaning every time this function is called, every time someone frobs, the variable gets one bigger. Altering integers has many options, they can be formatted in more complex formulas such as 1+6*2, meaning 1 plus six then times by two, but 1++ is the same as 1+1. And if you have a variable integer, they can be used instead of raw numbers, like here, we're using i instead.
DXP = DeusExPlayer(Frobber); //Storing the player class in a variable so we can access it later.
}
}
//Notice functions and "if statements" like this are surrounded by the brackets, this is important to tell the compiler that whats happening here is contained here, everything inside the the brackets only is accessed if the function above is passed, so in this example, the if statement is only checked if Frob is called, and the DoAThing is only called if the IF statement checks

function DoAThing(String thing) //Our custom function, defining our new variable, THING, which is set to whatever the classes Str variable is, because in the Frob function, when this function is called, its telling this variable to be that variable.
{
//BroadcastMessage is a default function stored in Actor, so anything we want to make will have access to this, it basically prints a message to everyone.
BroadcastMessage(DXP.PlayerReplicationInfo.PlayerName$" has frobbed the object! It has been frobbed "$i$" times!");
DXP.ClientMessage(thing);

//Now, whats happening here.... In Broadcast, the variable from the Player we stored, specifically its PlayerName is being accessed, which will print out that players name in to the broadcast in the game, and further in, the i variable is also being accessed. As we know, this variable increased each time its frobbed.
//And then, ClientMessage, this is a function inside the player class, so we're calling DXP's function there and ClientMessage then tells that player whatever the variable is in a private client message.
}

//DefaultProperties, the block that sets the variables, basically. Any variable you define here becomes the default permenantly in the game. So here is where you set what you want the Str variable to tell the game, and also you can define other properties such as Health, its Mesh, HitPoints, ItemName etc. Most are self explanatory.
//Formatting-wise, each property has its own layout or format, for numbers, it'd be plain i=2 etc.
//For Strings, they need to be in quote marks, like below.
//For more advanced things and if you ever don't know, check other classes for examples, any decoration or item will have Mesh or Texture examples.
defaultproperties
{
ItemName="Frob Counting Crate"
Str="Keep frobbing me, get that number higher!"
}
Category: Coding | Added by: Kaiz0r (23 September 2016)
Views: 49 | Rating: 0.0/0
Total comments: 0
avatar