Question 'Bout Custom Functions

Hey, how are you guys doing today. All is well I hope. Listen, I’m new to creating functions, so - I created a function that spawns three cobras’, and it worked as advertised. But now I am left wondering if there’s something I’m missing. wondering if I’m approaching this the proper way?
You guys show me how it’s done…

Code so far:

#include "nw_i0_2q4luskan"

void SpawnThree(string sCreature, location lLoc)
{
    location lLoc = GetLocation(GetFirstPC());

    CreateObject(OBJECT_TYPE_CREATURE, sCreature, lLoc);
    DelayCommand(0.5, CreateObjectVoid(OBJECT_TYPE_CREATURE, sCreature, lLoc));
    DelayCommand(1.0, CreateObjectVoid(OBJECT_TYPE_CREATURE, sCreature, lLoc));
}

void main()
{
    object oPC = GetLastUsedBy();
    if (!GetIsPC(oPC)) return;

    string sCreature = "goldcobra002";
    location lLoc = GetLocation(GetFirstPC());

    if ( GetLocalInt(oPC, "DO_ONCE__" + GetTag(OBJECT_SELF)) )
         return;
     SetLocalInt(oPC, "DO_ONCE__" + GetTag(OBJECT_SELF), TRUE);

    ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectVisualEffect
    ( VFX_IMP_DUST_EXPLOSION ), oPC);

    SpawnThree(sCreature, lLoc);
}
Alrighty then.  

The script seems to be mostly fine, If I were you I would rewrite it like this:

#include "nw_i0_2q4luskan"

void main()
{
    object oPC = GetLastUsedBy();
    if (!GetIsPC(oPC)) return;

    string sTAG = GetTag(OBJECT_SELF);
    if (GetLocalInt(oPC, "DO_ONCE__" + sTAG) == TRUE) return;
    SetLocalInt(oPC, "DO_ONCE__" + sTAG, TRUE);

    ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectVisualEffect (VFX_IMP_DUST_EXPLOSION), oPC);

    string sCreature = "goldcobra002";
    location lLoc = GetLocation(oPC);
    CreateObject(OBJECT_TYPE_CREATURE, sCreature, lLoc);
    DelayCommand(0.5, CreateObjectVoid(OBJECT_TYPE_CREATURE, sCreature, lLoc));
    DelayCommand(1.0, CreateObjectVoid(OBJECT_TYPE_CREATURE, sCreature, lLoc));
}

I changed a few things here and there, mostly I defined sTAG to not calculate it twice, I removed the double calculation of lLoc and removed the subroutine as I wouldn’t deem it necessary.

But overall, it should work mostly the same, the biggest difference is the output of lLOC, in my case it’s where the the PC has used the object, in your script it’s the location of the first PC, which might or might not be the same character…

Thanks for the input Clans!

This could be changed to

    string sVar = "DO_ONCE__" + GetTag(OBJECT_SELF);
    if (GetLocalInt(oPC, sVar)) return;
    SetLocalInt(oPC, sVar, TRUE);

No need to create the variable name twice. Also the “== TRUE” is unnecessary.