Author Topic: My questions thread  (Read 9676 times)

This is serious
seriously you could just read it

no if your asking for help, make it easy for the people who are trying to help you to understand what you want, be straight and to the point don't smother your questions inside a full paragraph of bullstuff

no if your asking for help, make it easy for the people who are trying to help you to understand what you want, be straight and to the point don't smother your questions inside a full paragraph of bullstuff
This, 100 times this.
I read it an had no idea what you were asking, I literally had quite the time pulling out your questions. I know you were trying to be funny or w/e but it was more annoying. I degarbled your questions. I don't think you'd understand how annoying it would be if I gave you an equally cryptic, hard-to-read, funny/joke answer.

Question 1:
Quote
One of my first problems is that I have no idea how to name the cat. I want the cat represented by a variable, such as $kitty, you could do something along the lines of $kitty = 'Georges' so the kitty is named Georges. I then think, this solution would only work for one cat, and I want it to work for more!
Question 2:
Quote
Also, I have no idea how to represent $kitty in chat. If someone said /namecat Georges, what would I put in response? messageClient(%client, '', "Your cat is named $kitty"); Would this solution do it? I don't think so, because variables aren't automagically replaced by what they are. That was my second problem.
Question 3:
Quote
My third, probably harder and less needed problem, is how would I save said kitty when the player disconnects? I know this is very complex, so it may not end up happening.


My answer (Question 1): Well there's a few ways you can do this. If you want one cat per player you can make an array and have the index being the player's ID. So you'd get an array like
Code: [Select]
//You store all the cats at the index specified by the players ID
//So when making a new cat, you'd do something like $kitties[%client.bl_id] = newCat();
$kitties[3605] = %myCat; //Coburn's cat would be here
$kitties[9097] = %yourCat; //Cloudy8's cat would be here
$kitties[1337] = %haxorCat; //etc's cat would be here
$kitties[0] = %kittyCat; //Badspot's cat would be here
$kitties[1] = %shaderCat; //Kompressor's cat would be here

You could also just store it right on the client as a member.
Code: [Select]
%client.cat = %myKittyCat;
Did you want support for more than one cat for each player (multiple players can have multiple cats)? Perhaps you should take a look at SimSets (or just use an array) and store a SimSet full of cats (or an array) on the client or in the array delineated by BL_ID.
Code: [Select]
//Using the BLID array thing with SimSets
$kitties[3605] = new SimSet();
$kitties[3605].add(newCat());
//Using the BLID array thing with another array (in effect a multidimensional array)
$kitties[3605,0] = newCat();

My answer (Question 2): There's a thing called string concatenation. You can use @, SPC, TAB, or NL to join strings. You can concatenate anything (because torque is very flexible (more formally known as a not strictly typed language)
Code: [Select]
//Joining strings
%resultStr = "Results go here";
%resultStr = "this" @ "that"; //becomes thisthat
%resultStr = "this" SPC "that"; //becomes this that
%resultStr = "this" TAB "that"; //Hopefully obvious, joins them with a tab
%resultStr = "this" NL "that"; //Joins then with a new line

%that = 100;
%resultStr = "this" SPC %that; //Becomes this 100
%that ="test";
%resultStr = "this" SPC %that; //Becomes this test

My answer (Question 3): If you use the BLID array, there's a special function for doing this called export where you can export a bunch of global variables to a file and it makes it easy to save and load.
If you're using the simset, you'd have to develop a system to save using FileIO which might be a fun learning experience.
Swollow has a bit more down here v v

EDIT: Sorted so other people can help if necessary
« Last Edit: October 19, 2013, 11:21:57 PM by DYLANzzz »

Dylanzzz has also answered the questions so check above aswell ^ ^

1.
Code: [Select]
$kitty = "George";2.
Code: [Select]
messageClient(%client,'',"Your name is " @ $kitty);or
Code: [Select]
messageClient(%client,'',"Your name is" SPC $kitty);3.
The easiest way to do this would be to make it a pref and have it auto export and load
just add $Pref:: before your variable, you would also want to use an array to store the names
so it would be like
Code: [Select]
$Pref::savedNames[666] = "Name";and then
Code: [Select]
echo($Pref::savedNames[666]);would echo Name

you can get the bl_id of a client like so: %client.bl_id
so if you were to use it in a function to save or utilize it you would want something like

Code: [Select]
function serverCmdSaveName(%cl)
{
  $Pref::savedNames[%cl.bl_id] = %cl.name;
}
function serverCmdWhatIsMyname(%cl)
{
  messageClient(%cl,'',$Pref::savedNames[%cl.bl_id]);
}

Edit:
A more difficult method of saving data but more easier to modify:
Code: [Select]
function saveName(%cl)
{
%f = new fileObject();
%f.openForAppend("config/nameList.cs");
%f.writeLine("$savedNames[" @ %cl.bl_id @ "] = " @ %cl.name);
%f.close();
%f.delete();
$savedNames[%cl.bl_id] = %cl.name;
}
function loadAllNames()
{
exec("config/nameList.cs");
}
first we set %f to a fileObject that we create
next we open "nameList.cs" whether it exists or not and add a new line to it containing the clients blid stored in an array of $savedNames and set it to there name
afterwards we close and then delete the fileObject, this conserves on memory and allows you to edit the file again later
then we will apply the name to the array list of the variable so we don't have to load every time

the second function simply executes all the variables stored in the "nameList.cs" that we wrot
« Last Edit: October 19, 2013, 11:18:05 PM by swollow »

Well, your first major issue is that you're trying to make a client-side mod with server commands. While there is an extension out there for client commands that are accessible through using the "/" prefix in chat, it's not exactly proper.

I actually just decided to go ahead and write a mod you can reference. Go ahead and do whatever you like with this, if you decide to release it I don't want any credit. I wrote it in the hope that actually seeing some of these concepts in use may teach you them, and I wrote it to be fairly basic while incorporating somewhat more advanced features.

Client_Cats.zip


So....
package Kitty
{
function servercmdNameKitty
{
   $kitty = 'Georges'
}

function servercmdKitty
{
messageClient(%client,'',"You own a cuddly cat named " @ $kitty);
}
};


For servercmdNameKitty, how would you choose the kitty's name
and wouldn't $kitty = "George";only work for one cat?

Well, your first major issue is that you're trying to make a client-side mod with server commands

1. Server sided

He's making it server sided apparently
I might end up using this considering ive never done clientsided chat code things
« Last Edit: October 19, 2013, 11:17:50 PM by DYLANzzz »

Client_Cats.zip

Not client sided

Warning - while you were typing a new reply has been posted. You may wish to review your post.

gah.

In the future that resource will be helpful, though. I have no idea what the script is 'doing'
« Last Edit: October 19, 2013, 11:15:42 PM by Johnny Blockhead »

So....
package Kitty
{
function servercmdNameKitty
{
   $kitty = 'Georges'
}

function servercmdKitty
{
messageClient(%client,'',"You own a cuddly cat named " @ $kitty);
}
};


For servercmdNameKitty, how would you choose the kitty's name
and wouldn't $kitty = "George";only work for one cat?
first off your functions need paranthesis and paramaters/arguments in this case

Code: [Select]
function serverCmdDoStuff(%clientWhoTypedit)and yes using a single variable would only work for one cat
you could however store multiple cat names on an array list like so
$kitty[0] = "John";
$kitty[1] = "Dog";

or backwads
$kitty["John"] = 0;
$kitty["Dog"] = 1;

depending on how you would want to call the variables

you are also missing a semi colon on the variable declaration, I'm also not sure if ' functions the same as " so i would just use quotations rather than apostrophes.
« Last Edit: October 19, 2013, 11:19:40 PM by swollow »

first off your functions need paranthesis and paramaters/arguments in this case

Code: [Select]
function serverCmdDoStuff(%clientWhoTypedit)and yes using a single variable would only work for one cat
you could however store multiple cat names on an array list like so
$kitty[0] = "John";
$kitty[1] = "Dog";

or backwads
$kitty["John"] = 0;
$kitty["Dog"] = 1;

depending on how you would want to call the variables

you are also missing a semi colon on the variable declaration, I'm also not sure if ' functions the same as " so i would just use quotations rather than apostrophes.


Is there a way for the user to create their own cat name in the array

yes
Code: [Select]
function serverCmdMakeMeACat(%cl,%name)
{
   $kittyName[%cl.bl_id] = %name;
}
i recommend storing the array on the client's bl_id as that doesn't change and is a way of identifying a client


   $kitty = 'Georges'

You could also make a cat script object instead of just storing a name and then you can have all the commands that the cat will use in their own namespace and you can store variables on the cat like %cat.name and sooooo much more. It would be nicer than just a few functions that use the cats name or having tons of arrays for all the different cat parameters for all the players cats.

If you want an example I'd be happy to provide one.

Not client sided
local blockland program

In the future that resource will be helpful, though. I have no idea what the script is 'doing'
What part are you confused by?

What I got

package Kitty
{
//Makes a cat, based on arrays via swollow
function serverCmdAdoptCat(%cl,%name)
{
   $kittyName[%cl.bl_id] = %name;
}

//Tells what your cat's name is and commands.
function servercmdKitty(%client)
{
messageClient(%client,'',"You own a cuddly cat named " @ $kitty);
messageClient(%client,'',"Commands you can do are <insert>");
}
};






For function serverCmdAdoptCat(%cl,%name)
{
   $kittyName[%cl.bl_id] = %name;
}


What would it be?
/adoptcat Georges
??

You could also make a cat script object instead of just storing a name and then you can have all the commands that the cat will use in their own namespace and you can store variables on the cat like %cat.name and sooooo much more. It would be nicer than just a few functions that use the cats name or having tons of arrays for all the different cat parameters for all the players cats.

If you want an example I'd be happy to provide one.

yes pls
this technology is foreign
« Last Edit: October 19, 2013, 11:40:20 PM by Johnny Blockhead »

Code: [Select]
function servercmdKitty(%client)
{
messageClient(%client,'',"You own a cuddly cat named " @ $kittyName[%client.bl_id]);
messageClient(%client,'',"Commands you can do are <insert>");
}
you need to call the name from the array
$kittyName[%client.bl_id]
/adoptCat George would make your cat named george
« Last Edit: October 20, 2013, 12:07:01 AM by swollow »

Script Objects:
So it's like making any other type of object. I'll keep this all cat related.
Code: [Select]
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.
Code: [Select]
//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
Code: [Select]
%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
Code: [Select]
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...
Code: [Select]
%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?
Code: [Select]
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!");
}
« Last Edit: October 20, 2013, 12:09:08 AM by DYLANzzz »