Aller au contenu

Photo

10 Lines or Less


  • Veuillez vous connecter pour répondre
3 réponses à ce sujet

#1
Tarot Redhand

Tarot Redhand
  • Members
  • 2 674 messages
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


#2
WhiZard

WhiZard
  • Members
  • 1 204 messages

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



#3
The Mad Poet

The Mad Poet
  • Members
  • 425 messages
//////////////////////////////////////////////////////////////////
// TMP - Function for getting random integer between two integers
//////////////////////////////////////////////////////////////////
int RandomBetween(int iMin,int iMax)

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


#4
WhiZard

WhiZard
  • Members
  • 1 204 messages

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;
}