Author Topic: How do you work with OnUserDefined?  (Read 372 times)

Legacy_YeOlde

  • Jr. Member
  • **
  • Posts: 66
  • Karma: +0/-0
How do you work with OnUserDefined?
« on: February 14, 2012, 10:53:07 am »


               Hey!

I am a little confused (yes, again!) about OnUserDefined on creatures. As I understand, the OnHeartbeat makes a call to OnUserDefined and launch any SignalEvent in that script. Is this correct?

Do I need a seperate OnHeartbeat script for every difference I want to make with the OnUserDefined script? I am just a bit uncertain how it is supposed to work.

However...

I would like it to work with variables in a way like this...

In the OnHeartbeat, it checks for variables on the creature (self) with the "IF" statement, and continues to the OnUserDefined.

[Example]

If integer "emote" = 1, then run EventUserDefined (001)
If integer "emote" = 2, then run EventUserDefined (002)
If integer "random walk" = 1, then run EventUserDefined (003)
else = do nothing


Is this possible, and is it a smooth way to work with the OnUserDefined script? Or do you have a better and/or easier solution?

Thanks! '<img'>

(And my apologies for my way of expressing myself, not native English)
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
How do you work with OnUserDefined?
« Reply #1 on: February 14, 2012, 02:28:58 pm »


                You are a little off on how the UserDefinedEvent was set up to work.  The main Idea behind it is to allow you to place all of your custom code for all of the objects events into a single script that you would attach to the UserDefined event.  It is not just the HB event that calls this event.  It is all of the Events.  The UserDefinedEventNumber is there so the script knows what event called the script when it is ran.  With this system there is no need to change the standard HB event ot any of the other standard Event scripts for that matter. All of your custom code goes into the UserDefinedEvent script only.  

So to do what you are asking, you wolld place all of your code into the UserDefinedEvent script.   When the HB script is ran it will signal a UserDefinedEvent for the HB Event.   The UserDefinedEvent script can then handle all of your if statments to decide what to do. 

 I am currently not at the house so I can not give much more information now.   You may want to look at the UserDefined templets to give you a nudge in the right direction if you need more information.   You should be able to get the templet from the Script Editor in the tool set on the Templets Tab.   ( Over there where the functions are listed).
               
               

               
            

Legacy_Shadooow

  • Hero Member
  • *****
  • Posts: 7698
  • Karma: +0/-0
How do you work with OnUserDefined?
« Reply #2 on: February 14, 2012, 03:31:23 pm »


               The point of the userdefined is only in allow custom content.

There is no point to do "something" via userdefined when you can do this in the script where you run the userdefined in first place. Point of the userdefined is when you have a script that occupy certain object events and you want to allow builder to add additional features into this script without modifying it.
               
               

               
            

Legacy_Shadooow

  • Hero Member
  • *****
  • Posts: 7698
  • Karma: +0/-0
How do you work with OnUserDefined?
« Reply #3 on: February 14, 2012, 03:41:26 pm »


               Look at this script. I wrote this script to handle placeable doors and its given into OnUsed event:

(note not possible to compile without my custom include with EVENT_ constants)

void LockAtClose(object oDoor);

void main()
{
object oPC = GetLastUsedBy();
object oDoor = OBJECT_SELF;
SignalEvent(oDoor,EventUserDefined(EVENT_PLACEABLE_DOOR_ON_USED));
 if(!GetIsTrapped(oDoor))
 {
  if(GetLocked(oDoor))
  {
  string sKey = GetTrapKeyTag(oDoor);
  object oKey = GetItemPossessedBy(oPC,sKey);
   if(sKey != "" && GetIsObjectValid(oKey))
   {
   SendMessageToPC(oPC,GetStringByStrRef(7945));
   SetLocked(oDoor,FALSE);
   }
   else
   {
   DelayCommand(0.1,PlaySound("as_dr_locked2"));
   FloatingTextStringOnCreature("*"+GetStringByStrRef(8307)+"*",oPC);
   SendMessageToPC(oPC,GetStringByStrRef(8296));
   SignalEvent(oDoor,EventUserDefined(EVENT_PLACEABLE_DOOR_ON_FAILEDTOOPEN));
   return;
   }
  }
  if(!GetLocalInt(oDoor,"IS_OPEN"))
  {
  AssignCommand(oPC,ActionPlayAnimation(ANIMATION_LOOPING_GET_MID));
  DelayCommand(1.0,ActionPlayAnimation(ANIMATION_PLACEABLE_OPEN));
  SetLocalInt(oDoor,"IS_OPEN",TRUE);
  SignalEvent(oDoor,EventUserDefined(EVENT_PLACEABLE_DOOR_ON_OPEN));
   if(GetLocalInt(oDoor,"DOORCLOSE") > -1)
   {
   int Delay = GetLocalInt(oDoor,"DOORCLOSE_DELAY");
   float fDelay = IntToFloat(Delay);
    if(!Delay)
    {
    fDelay = 7.0;
    }
   DelayCommand(fDelay,ActionPlayAnimation(ANIMATION_PLACEABLE_CLOSE));
   DelayCommand(fDelay,LockAtClose(oDoor));
   DelayCommand(fDelay,SignalEvent(oDoor,EventUserDefined(EVENT_PLACEABLE_DOOR_ON_CLOSE)));
   DelayCommand(fDelay,DeleteLocalInt(oDoor,"IS_OPEN"));
   }
  int iDoorLock = GetLocalInt(oDoor,"DOORLOCK");
   if(iDoorLock > 0 && !GetLocked(oDoor))
   {
   int isDay = GetIsDay();
    if(iDoorLock == 6 || !isDay && iDoorLock == 4 || isDay && iDoorLock == 5)
    {
    SetLocked(oDoor,TRUE);
    }
   }
  }
  else
  {
  object oTarget = GetObjectByTag(GetLocalString(oDoor,"DOORTARGET"));
   if(!GetIsObjectValid(oTarget))
   {
   return;
   }
  object oOppositeDoor;
  string sTag = GetLocalString(oDoor,"DOOR");
   if(sTag == "")
   {
   oOppositeDoor = GetNearestObject(OBJECT_TYPE_DOOR,oOppositeDoor);
   }
   else
   {
   oOppositeDoor = GetObjectByTag(sTag);
   }
   if(GetLocalInt(oOppositeDoor,"DOOROPEN") != -1)
   {
    if(!GetIsOpen(oOppositeDoor))
    {
    ActionOpenDoor(oOppositeDoor);
    }
   }
  AssignCommand(oPC,ClearAllActions());
  AssignCommand(oPC,JumpToObject(oTarget));
   if(GetArea(oTarget) == GetArea(oDoor))
   {
    object oHenchman = GetAssociate(ASSOCIATE_TYPE_FAMILIAR,oPC);
    if(GetIsObjectValid(oHenchman))
    {
    AssignCommand(oHenchman,JumpToObject(oTarget));
    }
   oHenchman = GetAssociate(ASSOCIATE_TYPE_ANIMALCOMPANION,oPC);
    if(GetIsObjectValid(oHenchman))
    {
    AssignCommand(oHenchman,JumpToObject(oTarget));
    }
   oHenchman = GetAssociate(ASSOCIATE_TYPE_SUMMONED,oPC);
    if(GetIsObjectValid(oHenchman))
    {
    AssignCommand(oHenchman,JumpToObject(oTarget));
    }
   oHenchman = GetAssociate(ASSOCIATE_TYPE_DOMINATED,oPC);
   int counter = 1;
    while(GetIsObjectValid(oHenchman))
    {
    AssignCommand(oHenchman,JumpToObject(oTarget));
    oHenchman = GetAssociate(ASSOCIATE_TYPE_DOMINATED,oPC,++counter);
    }
   counter = 1;
   oHenchman = GetAssociate(ASSOCIATE_TYPE_HENCHMAN,oPC);
    while(GetIsObjectValid(oHenchman))
    {
    AssignCommand(oHenchman,JumpToObject(oTarget));
    oHenchman = GetAssociate(ASSOCIATE_TYPE_HENCHMAN,oPC,++counter);
    }
   }
  SignalEvent(oDoor,EventUserDefined(EVENT_PLACEABLE_DOOR_ON_AREATRANSITIONCLICK));
  }
 }
 else
 {
 SignalEvent(oDoor,EventUserDefined(EVENT_PLACEABLE_DOOR_ON_FAILEDTOOPEN));
 }

void LockAtClose(object oDoor)
{
int iDoorLock = GetLocalInt(oDoor,"DOORLOCK");
 if(iDoorLock > 0 && !GetLocked(oDoor))
 {
 int isDay = GetIsDay();
  if(iDoorLock == 3 || !isDay && iDoorLock == 1 || isDay && iDoorLock == 2)
  {
  SetLocked(oDoor,TRUE);
  }
 }
}


Point is that once I need to add custom feature into these doors like "do something at OnFailedToOpen, I can use UserDefined to do this and I dont have to adjust system script.
               
               

               
            

Legacy_YeOlde

  • Jr. Member
  • **
  • Posts: 66
  • Karma: +0/-0
How do you work with OnUserDefined?
« Reply #4 on: February 15, 2012, 08:21:05 am »


               Thank you very much, this is now crystal clear to me! '<img'>
               
               

               
            

Legacy_FunkySwerve

  • Hero Member
  • *****
  • Posts: 2325
  • Karma: +0/-0
How do you work with OnUserDefined?
« Reply #5 on: February 15, 2012, 04:27:52 pm »


               FWIW, we've never bothered with user-defined events on HG. Instead, we make our script events general, and run special cases off of variables set on the object/s in question.

Funky