Author Topic: Will this code work? (events)  (Read 1251 times)

I wrote up this currency code and was wondering if this would work before I put it in and probably earn a syntax error.
I thought I'd check with you guys beforehand
Code: [Select]
registerOutputEvent(GameConnection, "addRupees","int 1 10000 25", 1);

function GameConnection::addRupees(%client, %amount)
{
%oldRup = %client.rupees;
%client.rupees = (%oldRup + %amount);
commandToClient(%client, 'bottomprint', "You now have" SPC %client.rupees SPC "rupees", 1);
}

registerOutputEvent(GameConnection, "subtractRupees","int 1 10000 25", 1);

function GameConnection::subtractRupees(%client, %amount)
{
{
if(%client.rupees >= %amount)
{
%oldRup = %client.rupees;
%client.rupees = (%oldRup - %amount);
commandToClient(%client, 'bottomprint', "You now have" SPC %client.rupees SPC "rupees", 1);
}
else
{
commandToClient(%client, 'bottomprint', "You do not have enough rupees to do this", 1);
}
}
}

registerOutputEvent(GameConnection, "setRupees","int 1 10000 25", 1);

function GameConnection::setRupees(%client, %amount)
{
%client.rupees = %amount;
commandToClient(%client, 'bottomprint', "You now have" SPC %client.rupees SPC "rupees", 1);
}

put it in, get a syntax error, learn from the error and correct it

I don't think you need a 1 after the registerOutputEvent since it is registered as GameConnection.

Everything seems fine to me.

In subtractRupees, you have an unnecessary set of curly braces.

In subtractRupees, you have an unnecessary set of curly braces.
I've always been confused by the curly braces -_-

I've always been confused by the curly braces -_-

What otta said means:

If there is only 1 statement in the if statement, it should be like this:
if(%stuff)
   //Stuff


Other than that, it should be:

if(%stuff)
{
   //stuff
   //even more stuff
   //so much stuff
}

The basic idea of curly braces is that it groups up all the script between them into one block. Generally (and always in torquescript), they are preceded by a statement such as if, while, for, etc. which indicates what's going on within those braces.

For convenience, they aren't always necessary for some things (i'm pretty sure these are the for and while loops as well as anything relating to an if statement) if the content is only one line long.

(i'm pretty sure these are the for and while loops as well as anything relating to an if statement) if the content is only one line long.
Correction: one statement, not one "line".

This is a series of shorthand loops and some of the loops have more than one "line" in their bodies:

Code: [Select]
for(%a = 0; %a < 10; %a++)
    echo(%a);

Code: [Select]
for(%a = 0; %a < 10; %a++)
    for(%b = 0; %b < 10; %b++)
        echo(%a SPC %b);

Code: [Select]
for(%a = 0; %a < 10; %a++)
    for(%b = 0; %b < 10; %b++)
        for(%c = 0; %c < 10; %c++)
            echo(%a SPC %b SPC %c);
(The outermost loop's body contains a loop which contains another loop)

Consider an entire loop and its body as "one statement".

In subtractRupees, you have an unnecessary set of curly braces.
Some languages allow a standalone block of code. Torque doesn't, right?

Code: [Select]
for(%a = 0; %a < 10; %a++)
    for(%b = 0; %b < 10; %b++)
        for(%c = 0; %c < 10; %c++)
            echo(%a SPC %b SPC %c);
i never tried that because i just assumed it wouldn't work lol

is that another weird torque thing or is it a standard rule

Some languages allow a standalone block of code. Torque doesn't, right?
i'm pretty sure it doesn't

i never tried that because i just assumed it wouldn't work lol

is that another weird torque thing or is it a standard rule
It works because, in a loop/if statement without brackets, the first and only the first statement is the entire body. Each loop and its body make up one statement, which is why you can have three nested loops without any brackets. Imagine the entire %b loop as the entire body of the %a loop:
Code: [Select]
for(%a = 0; %a < 10; %a++)
    for(%b = 0; %b < 10; %b++)
    {
        for(%c = 0; %c < 10; %c++)
            echo(%a SPC %b SPC %c);
    }

I don't think you need a 1 after the registerOutputEvent since it is registered as GameConnection.

Everything seems fine to me.
That argument is no longer append to client. It is now admin only. Append client was moved further down. So now it's registerOutputEvent(%class, %name, %parameterList, %adminOnly, %appendClient).

Yeah.. for future reference, while we're happy to help fix your code, you're supposed to try to fix it yourself first.

This is Coding Help, not Coding Monkeys.

That argument is no longer append to client. It is now admin only. Append client was moved further down. So now it's registerOutputEvent(%class, %name, %parameterList, %adminOnly, %appendClient).
Wait wait what?

So we have to go back and change every single event script? Or does it do fancy magic and detect if %appendClient isnt defined, then use %adminOnly as %appendClient or something?

If this is true, then whoo! Finally a decent (hopefully) admin only output event system!

Admin only argument is only for input events.

Admin only argument is only for input events.
Whoops. Forgot.