Author Topic: 10 Lines or Less  (Read 768 times)

Legacy_Tarot Redhand

  • Hero Member
  • *****
  • Posts: 4165
  • Karma: +0/-0
10 Lines or Less
« on: June 13, 2015, 07:29:34 pm »


               
Before anyone accuses me of trying to revive an old thread (if such a thread exists) let me say that I am genuinely interested in this. I am not doing this with thoughts of creating a vault submission, just seeking to build up my (and hopefully other peoples) arsenal of scripting weapons. If there is actually an old thread please "Bump" it and I will use that instead.

 

I am wondering if anyone else (other than me) has any small (preferably 10 lines or less) routines that they find genuinely useful and that they'd like to share. You know the sort of thing that while only maybe 3 or 4 lines of code long, either saves loads of typing or helps to make your code more readable. As an example, here are a pair of routines that I wrote which I have found to be useful.



void IncLocalInt(string sLocalInt, object oInMe = OBJECT_SELF) //convenience routine equivalent to LocalInt++
{
    int iThisVar = GetLocalInt(oInMe, sLocalInt);
    iThisVar++;
    SetLocalInt(oInMe, sLocalInt, iThisVar);
}


void DecLocalInt(string sLocalInt, object oInMe = OBJECT_SELF) //convenience routine equivalent to LocalInt--
{
    int iThisVar = GetLocalInt(oInMe, sLocalInt);
    iThisVar--;
    SetLocalInt(oInMe, sLocalInt, iThisVar);
}

Then there are the standard small routines that everybody uses such as





string PaddedStringDigits(int iInValue, int iNumChars) //convenience routine used to pad (with leading zeros) x digit numbers to iNumChars characters long
{
    string sReturnMe = IntToString(iInValue); //in other programming languages you'd probably have to trim leading/trailing spaces


    while(GetStringLength(sReturnMe) < iNumChars)
        sReturnMe = "0" + sReturnMe;
}

and 





void UseExamine() //goes in the OnUsed event - when the player clicks on a usable object the PC goes to the object and the description text is displayed
{
    object oPC = GetLastUsedBy();
    
    if(!(GetIsPC(oPC)))
        return;


    object oMe = OBJECT_SELF; //needed for next line


    AssignCommand(oPC, ActionExamine(oMe)); // if OBJECT_SELF is used here instead of oMe, oPC's description displayed instead
}

Hopefully none of the above appear in the lexicon. I don't want to be accused of plagiarism after all.



 

So over to you (please).

 

TR


               
               

               
            

Legacy_WhiZard

  • Hero Member
  • *****
  • Posts: 2149
  • Karma: +0/-0
10 Lines or Less
« Reply #1 on: June 13, 2015, 07:49:49 pm »


               

I am filing this as a sub-thread of the homebrew scripts



               
               

               
            

Legacy_The Mad Poet

  • Hero Member
  • *****
  • Posts: 715
  • Karma: +0/-0
10 Lines or Less
« Reply #2 on: June 13, 2015, 07:56:29 pm »


               

//////////////////////////////////////////////////////////////////
// TMP - Function for getting random integer between two integers
//////////////////////////////////////////////////////////////////
int RandomBetween(int iMin,int iMax)

{
int iOffSet = iMax-iMin;
return Random(iOffSet + 1)+iMin;
}

               
               

               
            

Legacy_WhiZard

  • Hero Member
  • *****
  • Posts: 2149
  • Karma: +0/-0
10 Lines or Less
« Reply #3 on: June 13, 2015, 08:06:03 pm »


               

From my post on the working with vectors thread


 


//Gets the angle (0.0-180.0 degrees) between two vectors when projected onto the XY plane
//Vectors of magnitude 0.0 are assumed facing East
float AngleBetweenVectors(vector A, vector B)
{
return fabs(fabs(VectorToAngle(-1.0 * A) - VectorToAngle(B)) - 180.0);
}

 


//Gets the angle (0-180 degrees) between two vectors when projected onto the XY plane
//Returns 0.0 if either vector has a magnitude of 0.0.
float AngleBetweenVectors(vector A, vector B)
{
float fMagA = sqrt(A.x * A.x + A.y * A.y);
float fMagB = sqrt(B.x * B.x + B.y * B.y);
if(fMagA * fMagB != 0.0) return acos((A.x * B.x + A.y * B.y)/(fMagA * fMagB));
return 0.0;
}

 


//Gets the true (3D) angle between two vectors
//Returns 0.0 if either vector has a magnitude of 0.0
float AngleBetweenVectors(vector A, vector B)
{
float fMagA = VectorMagnitude(A);
float fMagB = VectorMagnitude(B);
if(fMagA * fMagB != 0.0) return acos((A.x * B.x + A.y * B.y + A.z * B.z)/(fMagA * fMagB));
return 0.0;
}