Author Topic: [SQ3.0.2] Bunch of useful snippets  (Read 2419 times)

Shadow.

  • Tester
  • Full Member
  • ****
  • Posts: 144
  • Karma: +16/-35
    • View Profile
[SQ3.0.2] Bunch of useful snippets
« on: March 09, 2013, 12:55:02 pm »
Return full time in array

Code: [Select]
function GetTimeTable()
{
timestamptable <- array(5,0);
local time = GetFullTime().tostring();
timestamptable[0] = time.slice(0,3);
timestamptable[1] = time.slice(4,7);
timestamptable[2] = time.slice(8,10);
timestamptable[3] = time.slice(11,19);
timestamptable[4] = time.slice(20,24);
return timestamptable;
}

How to use it: GetTimeTable()[id], id can be 0,1,2,3,4 but not higher or lower. 0 returns day in 3 letters, 1 returns month in 3 letters, 2 returns day number in 1 integer, 3 returns the time in 8 characters, 4 returns year in 1 integer.

Example:
Code: [Select]
function onConsoleInput(cmd,text)
{
       print("Executing command: " + cmd + " with params: " + text + " at " + GetTimeTable()[0] + "/" + GetTimeTable()[1] + "/" + GetTimeTable()[3] );
}



Sending script error messages w/o forcing an error 2 ways.

Code: [Select]
function PrintErrorMessage(errortitle,optionalparams)
{
          if(optionalparams)
                throw("\"" + errortitle + "\" , possible cause: " + optionalparams);
           else
                throw("\"" + errortitle + "\"");
}

How to use it: PrintErrorMessage("hello","apple");
Images:




Method 2

Code: [Select]
function PrintErrorMessage2(errortitle,optionalparams)
{
          if(optionalparams)
                error("Error at \"" + errortitle + "\" , possible cause: " + optionalparams);
           else
                error("Error at \"" + errortitle + "\"");
}

How to use it: PrintErrorMessage("hello","apple");
Images:



To be continued.
If you have useful snippets, post them over here.

PS: SQ3.0.2 is the Squirrel version used by LU.
« Last Edit: March 09, 2013, 12:56:33 pm by Shadow. »

IdkanYavuk X

  • Jr. Member
  • **
  • Posts: 91
  • Karma: +37/-36
  • Developer for The Newport Experience
    • View Profile
Re: [SQ3.0.2] Bunch of useful snippets
« Reply #1 on: March 15, 2013, 05:50:45 pm »
Return full time in array

Code: [Select]
function GetTimeTable()
{
timestamptable <- array(5,0);
local time = GetFullTime().tostring();
timestamptable[0] = time.slice(0,3);
timestamptable[1] = time.slice(4,7);
timestamptable[2] = time.slice(8,10);
timestamptable[3] = time.slice(11,19);
timestamptable[4] = time.slice(20,24);
return timestamptable;
}

How to use it: GetTimeTable()[id], id can be 0,1,2,3,4 but not higher or lower. 0 returns day in 3 letters, 1 returns month in 3 letters, 2 returns day number in 1 integer, 3 returns the time in 8 characters, 4 returns year in 1 integer.

I use GetFullTime() always, but not in that way, I use this
Code: [Select]
local t = GetFullTime(), clock = GetTok( t, " ", 4 ), hour = GetTok( clock, ":", 1 ), minute = GetTok( clock, ":", 2 );
what is your opinion about my method?

Shadow.

  • Tester
  • Full Member
  • ****
  • Posts: 144
  • Karma: +16/-35
    • View Profile
Re: [SQ3.0.2] Bunch of useful snippets
« Reply #2 on: March 15, 2013, 06:26:55 pm »
well yeah, either way is good, I was bored back then and I wrote those snippets ;d

Thijn

  • Tester
  • Sr. Member
  • ****
  • Posts: 531
  • Karma: +27/-16
    • View Profile
Re: [SQ3.0.2] Bunch of useful snippets
« Reply #3 on: March 16, 2013, 03:25:34 pm »
There is a default squirrel function called date() which will return a table with all data you need.
No need to parse a string..
Code: [Select]
local time = date(GetTime());

timestamptable <- array(5,0);
timestamptable[0] = time.day;
timestamptable[1] = time.month;
timestamptable[2] = time.day;
timestamptable[3] = format("%02d:%02d:%02d", time.hour, time.min, time.sec);
timestamptable[4] = time.year;
Credits to Gudio for the code snippet

 

© Liberty Unleashed Team.