Author Topic: How do variables work?  (Read 1761 times)

How would you go about setting a variable
I have this variable:

Code: [Select]
%client.pieEnabled
How do you set it to 2?
I tried using this

Quote
function ServerCmdEnablePie(%client)
{
if(%client.pieEnabled = 2);
   {
   messageclient(%client,"","<color:FFFF00>You are already allowed to eat pie.");
        }
        else
        {
            messageall("",""<color:FFFF00> @ %client.name @ " is now allowed to eat pie!");
         messageclient(%client,"","<color:FFFF00>You are now allowed to eat pie.");
         %client.pieEnabled = 2;
           }
   }
};
Would that work for setting it to 2?

Here is the full code if you are interested
Code: [Select]
Package pie
{
function ServerCmdEnablePie(%client)
{
if(%client.pieEnabled = 2);
{
messageclient(%client,"","<color:FFFF00>You are already allowed to eat pie.");
        }
        else
        {
            messageall("",""<color:FFFF00> @ %client.name @ " is now allowed to eat pie!");
messageclient(%client,"","<color:FFFF00>You are now allowed to eat pie.");
%client.pieEnabled = 2;
         }
}
};
activatePackage(pie);


Package pies
{
function ServerCmdEatpie(%client)
{
if(%client.pieEnabled = 2);
{
messageall("",""<color:FFFF00> @ %client.name @ " ate some pie!");
messageclient(%client,"","<color:FFFF00>You ate some pie.");
        }
        else
        {
messageclient(%client,"","<color:FFFF00>You are not awesome enough to eat pie.");
}
}
};
activatePackage(pies);

Also, do I have the correct args such as
Code: [Select]
%client or do I need
Code: [Select]
%this too.

The assignment operator in Torque Script is '=', you assigned a value to your variable correctly, but you're not comparing the value correctly, which is why your code isn't being run past your if statement, you're using an assignment operator in your if statement, so basically, you're saying "if 2 do logic", and since control statements like if take a boolean expression, you will need to use a comparison operator.

For example:

Code: [Select]
%testValue = 2;

if(%testValue == 2)
{
        echo("The value was 2!");
}

Your code contains numerous syntax errors as well, if someone else doesn't point them out I'll help you out tomorrow.

I also suggest that you read the documentation on Torque Script.
« Last Edit: June 22, 2013, 02:49:24 PM by Kadon »

Recommendation, not mandatory: Use nested indentation, and use 4 spaces per indentation rather than 1 tab per indentation.

When you've rewritten the code, your bracket mistakes will be more clear.

Also, commandToClient, messageClient, and messageAllExcept want a tagged string supplied in the second argument. A tagged string is enclosed with the ' character, rather than the " character.

A cool thing is that a tagged string returns a numerical ID. To look up a tagged string's value based on its ID, simply use getTaggedString( id );

Code: (Incorrect Usage) [Select]
   if(%cake == 5)
{
messageClient(%client,"","You have 5.");
        }

Code: (Correct Usage) [Select]
if( %cake == 5 )
{
    messageClient( %client , '' , "You have 5." );
}

use 4 spaces per indentation rather than 1 tab per indentation.
What? No
That's just a pain in the ass to count out if you have multiple levels of indents

use 4 spaces per indentation rather than 1 tab per indentation.
Why the hell would you do that?

Use nested indentation, and use 4 spaces per indentation rather than 1 tab per indentation.
dafuq?

Why the hell would you do that?

dafuq?

What? No
That's just a pain in the ass to count out if you have multiple levels of indents

No need to be like that, he was just making a suggestion, and while I personally don't agree with what he suggested, that's his personal preference so you shouldn't attack him because you disagree.

I think it's perfectly reasonable to call someone out for suggesting something dumb, but more than two people is unneccesary

he was just making a suggestion
He was making a recommendation, not a suggestion.
There's a difference.

you shouldn't attack him because you disagree.
No one attacked him personally, but said that his preference was odd.



Anyways most good text editors allow you to change how many spaces a tab indents, so if you set it to 4 there's literally no difference between 4 spaces and one tab other than one takes four times as many key presses

literally no difference between 4 spaces and one tab other than one takes four times as many key presses
Save 4x time on indenting, AND SAVING IS GOOD. SAVE TREES!

I think it revised it enough to get it working
I know my formatting is horrible, but did I get the code and the brackets right?

Code: [Select]
package EnablePies
{
function ServerCmdEnablePie(%client)
if(%client.pieEnabled == 2)
{
messageClient( %client , '' , "You are already allowed to have pie." );
}
else
{
messageClient( %client , '' , "<color:FFFF00>You are now allowed to eat pie." );
%client.pieEnabled = 2
}

};
ActivatePackage(EnablePies);

package EatPie
{
function ServerCmdEatPie(%client)
if(%client.pieEnabled == 2)
{
messageClient( %client , '' , "You are already allowed to have pie." );
}
else
{
messageClient( %client , '' , "<color:FFFF00>You are not allowed to eat pie." );
       }
};
ActivatePackage(EatPie);

Code: [Select]
package EnablePies
{
function ServerCmdEnablePie(%client)
{
if(%client.pieEnabled == 2)
{
messageClient(%client,'',"You are already allowed to have pie.");
}

else
{
messageClient(%client,'',"<color:FFFF00>You are now allowed to eat pie.");
%client.pieEnabled = 2;
}
}
};
ActivatePackage(EnablePies);

package EatPie
{
function ServerCmdEatPie(%client)
{
if(%client.pieEnabled == 2)
{
messageClient(%client,'',"You are already allowed to have pie.");
}

else
{
messageClient(%client,'',"<color:FFFF00>You are not allowed to eat pie.");
}
}
};
ActivatePackage(EatPie);

Your functions need brackets as well, like Honorable added.

Ok. I got it working. How would you display a variable in a chat message?
Like this?
Code: [Select]
messageClient(%client,'',"You ate a pie. You have eaten %client.piesEaten pies.");
Also, how would you add 1 onto a varible?
Like this?
Code: [Select]
%client.piesEaten + 1;
Or like this? (is this for checking what it is when you add 1?)
Code: [Select]
%client.piesEaten += 1;

Ok. I got it working. How would you display a variable in a chat message?
Like this?
Code: [Select]
messageClient(%client,'',"You ate a pie. You have eaten %client.piesEaten pies.");
Also, how would you add 1 onto a varible?
Like this?
Code: [Select]
%client.piesEaten + 1;
Or like this? (is this for checking what it is when you add 1?)
Code: [Select]
%client.piesEaten += 1;
To add a variable you can do %client.piesEaten++; (this one is only to increment it by one), %client.piesEaten += 1; or %client.piesEaten = %client.piesEaten + 1;

To subtract a variable you can do %client.piesEaten--; (this one is only to decrement it by one), %client.piesEaten -= 1; or %client.piesEaten = %client.piesEaten - 1;

To display a variable in a message do this: messageClient(%client,'',"You ate a pie. You have eaten" SPC %client.piesEaten SPC "pies.");
SPC is a connector that adds space in the part of the message were it connects. Other connectors are @, which connects it with no space or anything, and NL, which connects it and adds a new line at the spot were it connects.


In the below examples, %client.piesEaten is 2.
So, messageClient(%client,'',"You ate a pie. You have eaten" SPC %client.piesEaten SPC "pies."); would look like: You ate a pie. You have eaten 2 pies.

messageClient(%client,'',"You ate a pie. You have eaten" @ %client.piesEaten @"pies."); would look like: You ate a pie. You have eaten2pies.

And messageClient(%client,'',"You ate a pie. You have eaten" NL %client.piesEaten NL "pies."); would look like: You have eaten a pie. You have eaten
2
pies.


You need a connector at both ends of the variable, but the connectors can be different from each other.
« Last Edit: June 24, 2013, 05:21:59 PM by jes00 »