Author Topic: Help (first time coding)?  (Read 1743 times)

I've never really coded before. I would like to know how to format a code + if there's a way to create a game clock, and if so how.

For formatting just look at other add-ons. You'll see the similarities between them. If you do something wrong you'll get a syntax error. An in game clock is possible but most likely out of your range if you're just starting.

Here's a basic template
Code: [Select]
function nameOfFunction(%inputVariables)
{
      do stuff here;
}


Every variable needs a "%" before it. To declare a function start with function before it, then the ( ) with the input variables. Every line has to end with a semicolon. The best way to learn all of this is just look at other already made things.


function tickTok()
{
   cancel($ClockTikTok); //Make sure we don't have duplicates running..
   $Clock::Min++;
   $ClockTikTok = schedule(1000,0,tickTok);
}

Do tickTok(); in your console, then it should start the ticking.
Then you can do echo($Clock::Min); in your console and it should tell the mins from the start.

Every variable needs a "%" before it. Every line has to end with a semicolon.
Uhh no?

Every variable needs a "%" before it.
Wrong, local variables which can only be used in the current scope, have a % before and global variables, which can be used in all scopes, start with an $

For example:

If I run this
Code: [Select]
function dosomething()
{
    %hello = "hi";
}
dosomething();
and type
Code: [Select]
echo(%hello);into the console, nothing will show up because you can only use the variable in the function where it's defined.
if I ran this though
Code: [Select]
function dosomething()
{
    $hello = "hi";
}
dosomething();
and I typed
Code: [Select]
echo($hello);into the console, would would see "hello" be written because global variable can be used by everything.


It's recommended to use local variables when you can but when you want to define something that would be used by many scripts ex: an environment setting or screen resolution, use global variables.

Sorry for spoonfeeding but this is an important thing for OP to understand

if you need to know more stuff about the syntax of Torque script look at this: http://torquescript.webs.com/objectsmethods.htm
« Last Edit: March 15, 2014, 10:47:51 AM by Aide33 »

Calm down guys, he was trying to simplify it and accidentally oversimplified. 99% of variables start with a %, otherwise you're doing something wrong. Most lines end with a semicolon.

Am I doing this right?
I don't know if I am.

Code: [Select]
function dosomething()
{
$time = "The time is..."
$"The time is..." = showclock
}

I want it to show how long the server is up and running.

Well, the syntax is mostly correct. You need a semicolon after $time = "The time is..."

You should also be using local variables (%) instead of global variables ($).

I'm a little confused on what the fourth line is trying to do. First off, variable names can only be 1 word and can't include quotes. The line before it is the correct way to set a variable (minus the semicolon). I don't know what you think showclock does, but I can tell you that it doesn't do anything at all.

So I'll write up a short tutorial on how you'll have it show how long the server has been running for.

First, you have to make your function:
function showRuntime()

After this line you'll need to open a curly bracket.

Then you're going to want to store the runtime in a variable:
%runtime = getSimTime();

This line calls the function getSimTime which tells you how long the program has been running, then stores that value in %runtime.

However, getSimTime gives you the time in milliseconds. You're going to want to convert this to seconds so that you can format it (hours:minutes:seconds). So we want to divide it by 1000 (there's a thousand milliseconds in a second) and round down to get rid of those pesky milliseconds.
%runtime = mFloor(%runtime / 1000);
m as a prefix on a function stands for "math," and "floor" means round down (to the floor). There is also mCeil which rounds up (to the ceiling).

Now we want to format this so that instead of knowing the server has been running for 1253 seconds, we can know it's been running for 20:53.
%runtime = getTimeString(%runtime);

Then you're going to want to display it to the server.
announce("Blockland has been running for " @ %runtime);
Announce is the simplest function that displays text in chat to the entire server. The most confusing part about this should be @, which links two pieces of text together. In this case it forms the line "Blockland has been running for X".

So all together your script should look like this:
Code: [Select]
function showRuntime()
{
    %runtime = getSimTime();
    %runtime = mFloor(%runtime / 1000);
    %runtime = getTimeString(%runtime);
    announce("Blockland has been running for " @ %runtime);
}

This can actually be simplified into announce("Blockland has been running for " @ getTimeString(mFloor(getSimTime()/1000))); but that's a lot harder to understand, and lines like that are a primary reason that people have trouble learning from reading other's scripts.
« Last Edit: March 15, 2014, 02:13:06 PM by $trinick »

Every variable needs a "%" before it.
global variables begin with a $
variables on objects are not prefixed with a percent sign
Every line has to end with a semicolon.
declarations must end in a semi colon not lines
conditions and cases for example do not need to end in a semi colon

Are you people serious? I gave him a super simple explanation of what to look for. I didn't tell him it's the case for everything, I even told him to go look at already made add-ons. Y'all motherforgeters are pedantic as stuff..

Are you people serious? I gave him a super simple explanation of what to look for. I didn't tell him it's the case for everything, I even told him to go look at already made add-ons. Y'all motherforgeters are pedantic as stuff..
So you are saying that

Code: [Select]
function nameOfFunction(%inputVariables)
{
      do stuff here;
}
This has no errors at all? Are you saying you know more than us?! What?

I don't know why it matters of who helps who first, the more help he gets, the better. Your little code didn't really help.

Are you people serious? I gave him a super simple explanation of what to look for. I didn't tell him it's the case for everything, I even told him to go look at already made add-ons. Y'all motherforgeters are pedantic as stuff..
Coding forums are the most pedantic places on the internet. This should be pretty obvious since coding is all about following the rules :)


Are you people serious? I gave him a super simple explanation of what to look for. I didn't tell him it's the case for everything, I even told him to go look at already made add-ons. Y'all motherforgeters are pedantic as stuff..
you said 'every' multiple times if that doesn't imply case for everything than I don't know what does.

you said 'every' multiple times if that doesn't imply case for everything than I don't know what does.
Calm down guys, he was trying to simplify it and accidentally oversimplified. 99% of variables start with a %, otherwise you're doing something wrong. Most lines end with a semicolon.