101
« on: October 20, 2013, 12:07:37 AM »
Script Objects:
So it's like making any other type of object. I'll keep this all cat related.
function newCat(%name, %size, %color, %gender)
{
%so = new ScriptObject() //Usually you can put a name for the object (like the actual BL object, not the name the cat will have) in between the parentheses to access it later but it won't work for me?
{
class = "Cat"; //So this is the class name for your script object. This is used in order to define the methods (functions of a class / object) for this script object
name = %name; //We do some initializing with the variables passed to this function. You can create as many of these fields (variables of a class / object) as you want
size = %size;
color = %color;
gender = %gender;
//I usually initialize all fields that I will ever use so I can remember that this object has them
//I usually do "" for regular variables and 0 for objects that will later get created on the script object
children = 0;
};
//Note you can make more parameters outside too
//%so.name = "another name";
//When making objects on a class, youhave to define them outside
%so.children = new SimSet(); //A simset containing all the children the cat has had.
return %so;
}
A lot of stuff is happening here so I'll break it down.
function newCat(%name, ...) - When I'm making tons of objects in torque, I like to isolate it into it's own function to make the object and set it up (since it's an object we dont get to refer to any constructor, so I just do this when handling tons of objects). You don't need this to be in a function, you can make a script object anywhere.
%so = new ScriptObject() { - This will take our new script object and put it in the variable %so
class = "Cat"; - This gives our script object its class that we will use to create methods (see below)
name = %name; size = %si... - These are all the parameters we define on our class when we make it. You can add as many as you want, and you can even add more later, they don't have to all be defined now.
}; - Notice, you have to end it with a ; like a package
%so.children = new SimSet(); - This is an example of a few things. It first shows you how to set parameters on a script object after you've already made it (it's like doing it on any other torque object). This is also how you add a new object to your script object (because you cannot do that inside the initialization brackets like with all the other parameters).
So it makes things like making a new cat, storing the info, and retrieving the info very easy.
//The life of a cat
$cat[3605] = newCat("Yip", 2, "1.0 1.0 0.0 1.0", "male");
echo("Coburn's cat has a cool name. It's name is " @ $cat[3605].name);
You're probably familiar with the . but not exactly what it really is. The . is actually basically a class retrieval operator. It will either run a method (function of a class / object) or get a member (variable of a class / object). Sooo
%obj; //Come junk object
%obj.myVariable //Holds some value myVariable
%obj.myVariable = 2; //Sets the myVariable of the class
%obj.myFunctionCall(2, 3, "Hello"); //Calls some function named myFunctionCall
Now that you're well aquainted with these types of objects, let's make a function that you can call on it using the . operator
function Cat::Purr(%this) //The first variable in a method is ALWAYS the object that would be calling it. Naming it %this is a convention found in other languages, in torque you can call it whatever you want.
{
messageAll('',"\c3" @ %this.name @ "\c5: HOLY MOTHER OF GOD PURRRRRRRR");
}
Break it down on the dance floor, the code dance floor that is...
function Cat::Purr - This defines a function called Purr that can be called from any object of type Cat using the . operator
(%this) - The argument list. It could have any arguments you want, but remember, the first argument is ALWAYS %this, even if you named it %that, or %me, it would still contain the SAME value. The value that it contains is the object that called the function. Ex: If I own a object of type Cat named Jaspers and he started to purr...
%myCat = newCat("Jaspers", 2, "1.0 1.0 0.0 0.0", "male"); //Make new car for me in %myCat variable
%myCat.purr(); //Notice how we never pass anything, and yet %this will still be filled!
//What happens over at the purr function
function Cat::Purr(%this) //%this would contain %myCat, that cat was the object that called purr. This is how we get the name of the cat that called this function.
{
messageAll('',"\c3" @ %this.name @ "\c5: HOLY MOTHER OF GOD PURRRRRRRR"); //%this.name would be Jaspers because he called this function!
}
Oh, to put this more in context, I coded the bottom bit for you to see possibly how this might work with your mod?
function serverCmdAdoptCat(%client, %name)
{
$kitty[%client.bl_id] = newCat(%name, 2, "1.0 1.0 0.0 1.0", "male"); //They name a cat with name %name and we put in some default values.
}
function serverCmdPetCat(%client)
{
$kitty[%client.bl_id].purr(); //We pet our cat, make it purr.
}
function serverCmdSnipCat(%client) //Snip ;_;
{
%cat = $kitty[%client.bl_id];
if(%cat.gender !$= "male") //They have to be male
return;
%cat.gender = "female";
messageAll('', %client.name @ " snipped his cat and it is now female! Ow!");
}