Author Topic: Question About Events / General TorqueScript  (Read 1174 times)

Ok, is it a good Idea to code simple events as a start?

The reason is that theres a set system for interacting with game content, where as with what most people start off with (/ commands)
doesn't have a definite way to interact with the game.

I understand how register Output/input event works, and I also understand programming because I can program simple programs in C++, and I understand objects/classes (one of the main parts of BL scripting) -edited redundancy out-


I have finally understood something: those who learn scripting by just looking at the script have had experience. I have finally started to get some of the concepts, and I really want to learn.


QUESTION(S):

What exactly is the %this that I see everywhere, er, what does it stand for, and where does it come from?

Obviously its a argument that the function is recieving and modifying, but I don't know where its coming from since it is very vague.



If I am unclear I am not surprised.



Edit: Ok so people just use %this as the variable for the first argument. So it isn't vague, its just literally "this", whatever the function needs to modify.

I know, duh, but I'm learning.


NEXT QUESTION:

Wheres a good source to find all of the blockland functions for things like the player, fxDTSbrick, and other things?

like, I do not know where to find things like %whatevervar.getDataBlock().

edit: Ok a list of functions.


ANOTHER:

What my definition of a datablock is a, ... block of data?

So a datablock is only like a structure or something like that in torque, what is an exact definition?          
« Last Edit: October 07, 2009, 08:53:21 PM by Zenthos »

Ok, is it a good Idea to code simple events as a start?
It won't hurt, however it's by far not the easiest way to start.
Most people start by modifying weapons. (And spamming dataBlocks.)

The reason is that theres a set system for interacting with game content, where as with what most people start off with (/ commands)
doesn't have a definite way to interact with the game.
... If I understand what you have said correctly, it is completely false.
serverCmds (/commands) are just functions like anything else.
You can do whatever you want inside these functions.
The only special thing about functions whose names begin with serverCmd is that they may be called on the server by a client via commandToServer() (a client sided function.)
Also, similar to methods I describe below, serverCmd functions get an argument passed to them whenever they are called by a client, this is the client object who invoked the function call.

For example:
Code: [Select]
function serverCmdKillMyPlayer(%client) {
   if(isObject(%client.player))
      %client.player.kill();
}
The above would kill the player (if it exists) of the client who invoked the function call.

However you can also do much more complex things that are totally unrelated with the client.
For example dumping the title of the blockland homepage to the chat whenever somebody types /iLoveThisGame:
Code: [Select]
function blocklandFetcher::onConnected(%this) {
   %this.send("GET / HTTP/1.1" NL "Host: blockland.us\n");
   %this.send("");
}

function blocklandFetcher::onLine(%this, %line) {
   if(strPos(strToLower(%line), "<title>") != -1) { //I don't believe case sensitivity is a potential issue anyway, strtoLower is probably not necessary.
      talk(strReplace(strReplace(%line, "<TITLE>", ""), "</TITLE>", "")); //Talk() sends chat to players, and normally I would do a subString
      //mess to get the title as the line containing the title may contain other stuff as well as the title.
      //However for the purposes of this example, it doesn't really matter because blockland.us has the title on it's own line (for the time being).
      %this.disconnect();
      %this.delete();
   }
}

function serverCmdILoveThisGame() {
   %obj = new tcpObject(blocklandFetcher);
   %obj.connect("blockland.us:80");
}
More on %this later.

Note: If I were writing this for a real script, I would have added more error handling callbacks.

I understand how register Output/input event works, and I also understand programming because I can program simple programs in C++, and I understand objects/classes (one of the main parts of scripting) because I can do it in C++.
...

I have finally understood something: those who learn scripting by just looking at the script have had experience. I have finally started to get some of the concepts, and I really want to learn.
Reading other people's scripts is how I started. Eventually you will want to read some stuff on GG (Garage Games) because just because blocklanders do stuff that work, it doesn't mean there isn't a better way. (Unless you're space guy in which case what you're doing is such a big hack that the way you're doing it at the moment is the ONLY way it can be done at all!)

What exactly is the %this that I see everywhere, er, what does it stand for, and where does it come from?

Obviously its a argument that the function is recieving and modifying, but I don't know where its coming from since it is very vague.
You're right to a certain extent.
%this is just what people generally use as the first argument of a method of an object.
This argument is always the object on which you are calling a given method.
For example:
Code: [Select]
function tcpObject::onConnected(%this, %line) {
   %this.send("Hello World!");
}
would be a method of tcpObjects.

In this case, the onConnected callback is called whenever a given tcpobject successfully establishes a connection, but that is beside the point.

%this, is set to the object that was just connected.
I then use %this to use that connected object, and send "Hello World!" to that connection via the object %this.

The argument does not necessarily need to be %this.
It could be %bob.

Code: [Select]
new scriptObject(uselessObject) {
   class = "whenIDoStuffIGetDeleted";
};

function whenIDoStuffIGetDeleted::doStuff(%bob) {
   echo("deleting" SPC %bob.getID());
   %bob.delete();
}

uselessObject.doStuff();
In this example, we create a scriptObject named uselessObject, this scriptObject is a member of the whenIDoStuffIGetDeleted class. Then, we create a method of the whenIDoStuffIGetDeleted class, this method is named doStuff. In this case, it is important that we have %bob to detect which object this method is being called on, otherwise we might delete the wrong object.

Note: I didn't test any of my examples.

Mostly I was quickly typing due to my discovery of how torque isn't a deep hole with a crapload of things to know, its similar. (Again, my realization)


Yes, I have edited weapons.

Thanks for that,

I got a little fuzzy when you were describing:
Code: [Select]
function tcpObject::onConnected(%this, %line) {
   %this.send("Hello World!");
}

You made it sound like that function was automatically called when the object connects, but I don't see how that works.

I thought
Code: [Select]
function Objname::memberfunction(%this)type of function was to create a member function for objects,
but again I may be right just not understanding what you said, or missing your point altogether. (or both)

edit: ok I figured it out so a Objname::func is used for making a new function for the object, or making more things happen when that function is called...

so yes that would happen when the object connected.

If im not using the right words for things (object, member function) tell me. Edit: Ok not member function, method?


Ok is there a place to get a list of functions for BL / general torquescript? Maybe even some global variables?

Just figured out, datablocks are like packages of packages...

Jesus christ this morning I barely knew how to edit weps, know I see the game as a lot simpler.
« Last Edit: October 07, 2009, 10:52:51 PM by Zenthos »

Ok Bump and first successful script:
Code: [Select]
//Event_EchoSquare



registerOutputEvent(fxDtsBrick, "EchoMath", "list Square 0 Sqrt 1" TAB "int 0 10000", 0);




function fxDtsBrick::EchoSquare(%brick, %choice, %number)
{
if(%choice)
{
%number = mSqrt(%number);
}
else
{
%number = %number * %number;
}

echo(%number);
}



See, you can do simple things with events.

I know all of the keywords, I have used this to my advantage: http://www-rohan.sdsu.edu/~stewart/GPGT/Appendix%20A%20-%20Quick%20References.pdf

I am now excited.