Author Topic: tagbased problems  (Read 1666 times)

Legacy_GhostOfGod

  • Hero Member
  • *****
  • Posts: 1490
  • Karma: +0/-0
tagbased problems
« Reply #15 on: May 15, 2012, 08:56:06 pm »


               Ok so let's start with the tagbased script first. If you want one script to work with the OnEquip and OnUnequip events (or any of the other events for that matter) then you need to structure it so that you figure out which event is happening first. Then define your variables inside each "if" statement:

#include "x2_inc_switches"
void main()
{
    int iEvent = GetUserDefinedItemEventNumber();

    if (iEvent == X2_ITEM_EVENT_EQUIP)//PC equips item
    {
        object oPC = GetPCItemLastEquippedBy();
        object oItem = GetPCItemLastEquipped();

        //Everything else
    }

    else if (iEvent == X2_ITEM_EVENT_UNEQUIP)//PC unequips item
    {
        object oPC = GetPCItemLastUnequippedBy();
        object oItem = GetPCItemLastUnequipped();

        //Everythihng else
    }

    //You can keep going with this is you want something to happen with other
    //events as well.
    else if (iEvent == X2_ITEM_EVENT_ACQUIRE)//PC picks up, buys, gets item
    {
        object oPC = GetModuleItemAcquiredBy();
        object oItem = GetModuleItemAcquired();

        //Everything else
    }

    else if (iEvent == X2_ITEM_EVENT_UNACQUIRE)//PC sells, drops, loses item
    {
        object oPC = GetModuleItemLostBy();
        object oItem = GetModuleItemLost();

        //Everything else
    }

    //else if (iEvent == etc...
}

You will notice that we put any variables that pertains specifically to one of those events inside the "if". In your script you define oPC at the top as "GetPCItemLastEquippedBy()". But what happens when the player is UNequipping it? He then needs to be defined differently as "GetPCItemLastUnequippedBy()".
Hopefully that makes sense.

So your script might look more like:

#include "x2_inc_switches"
void main()
{
    int nEvent = GetUserDefinedItemEventNumber();

    if (nEvent == X2_ITEM_EVENT_EQUIP)
    {
        object oPC = GetPCItemLastEquippedBy();
        object oItem = GetPCItemLastEquipped();
        int nSpeed = 30;
        if(GetLevelByclass(class_TYPE_MONK, oPC)>=6)// ._.
        {
            nSpeed-= 25;
        }
        AssignCommand(oItem,ApplyEffectToObject(DURATION_TYPE_PERMANENT, SupernaturalEffect(EffectMovementSpeedIncrease(nSpeed)), oPC));
    }

    else if (nEvent == X2_ITEM_EVENT_UNEQUIP)
    {
        object oPC = GetPCItemLastUnequippedBy();
        object oItem = GetPCItemLastUnequipped();
        effect eEffect = GetFirstEffect(oPC);
        while (GetIsEffectValid(eEffect))
        {
            if(GetEffectCreator(eEffect) == oItem)
            {
                if (GetEffectType(eEffect) == EFFECT_TYPE_MOVEMENT_SPEED_INCREASE)
                {
                    RemoveEffect(oPC, eEffect);
                }
            }
        eEffect = GetNextEffect(oPC);
        }
    }
}
               
               

               


                     Modifié par GhostOfGod, 15 mai 2012 - 08:15 .
                     
                  


            

Legacy_WhiZard

  • Hero Member
  • *****
  • Posts: 2149
  • Karma: +0/-0
tagbased problems
« Reply #16 on: May 15, 2012, 10:14:08 pm »


               Here is a list of all tag-based calls (defined in x2_inc_switches)

//0. Activate item, occurs when using any unique power on an item: names of unique powers include activate item, unique power, and manipulate portal stone
const int X2_ITEM_EVENT_ACTIVATE = 0;
//1. Equip item, occurs when the item is equipped
const int X2_ITEM_EVENT_EQUIP = 1;
//2. Unequip item, occurs when the item is unequipped
const int X2_ITEM_EVENT_UNEQUIP = 2;
//3. On-Hit: occurs when the item hits a target (or is hit by a target for armor) in this script OBJECT_SELF is the PC possessing the item
const int X2_ITEM_EVENT_ONHITCAST = 3;
//4. Acquire item, occurs when the item is gained in the inventory
const int X2_ITEM_EVENT_ACQUIRE = 4;
//5. Unacquire item, occurs when the item is removed from the inventory
const int X2_ITEM_EVENT_UNACQUIRE = 5;
//6. Occurs when a spell is cast at the target, OBJECT_SELF is the caster of the spell
const int X2_ITEM_EVENT_SPELLCAST_AT = 6;
               
               

               


                     Modifié par WhiZard, 15 mai 2012 - 09:23 .
                     
                  


            

Legacy_GhostOfGod

  • Hero Member
  • *****
  • Posts: 1490
  • Karma: +0/-0
tagbased problems
« Reply #17 on: May 15, 2012, 10:45:05 pm »


               And as far as re-adding the effects gos, in your OnDeath script you could add a chunk of script kinda like so:

    object oItem;
    int nSlot;

    for (nSlot=0; nSlot<NUM_INVENTORY_SLOTS; nSlot++)
    {
        oItem=GetItemInSlot(nSlot, oPC); //(oPlayer, oDead, oLevelupper, etc..)
        if (GetIsObjectValid(oItem))
        {
            string sTag = GetTag(oItem);
            if (sTag == "Helm_of_Blind")
            {
                AssignCommand(oItem, ApplyEffectToObject(DURATION_TYPE_PERMANENT, SupernaturalEffect(EffectBlindness()), oPC));
            }
            if (sTag == "Helm_of_Deaf")
            {
                AssignCommand(oItem, ApplyEffectToObject(DURATION_TYPE_PERMANENT, SupernaturalEffect(EffectDeaf()), oPC));
            }
            if (sTag == "Pants_of_Poo")
            {
                AssignCommand(oItem, ApplyEffectToObject(DURATION_TYPE_PERMANENT, SupernaturalEffect(EffectAbilityDecrease(ABILITY_CHARISMA, 1)), oPC));
            }
            //etc..
        }
    }


Basically all this does is loop through equipped items and gets the tag & readds effects. If the tag matches something in particular, then it has that equipped item add the effects again. You will need to list all or your items that need to have effects while equipped and add it to a list like above.

As far as I remember the default level up script doesn't remove effects. But then again I haven't actually played the game for about a year so don't quote me on that. But if the default behavior or your script IS removing effects then you will bascially use the same chunk of script as above. If it is a script in particular that is removing the effects OnLevelUp then make sure this chunk of script gos after that part. Proabaly safest just to stick it on the bottom.

Anyhow, good luck. Hope this helps.

P.S. I just highlighted oPC to make sure you put in the correct name of the declaration based on whichever script you are adding this to. oPC, oPlayer, oDead, etc..
               
               

               


                     Modifié par GhostOfGod, 15 mai 2012 - 09:51 .
                     
                  


            

Legacy_Cursed_Eclipse

  • Newbie
  • *
  • Posts: 14
  • Karma: +0/-0
tagbased problems
« Reply #18 on: May 17, 2012, 04:21:37 pm »


               Hey Ghost ,i really appreciate your help,Thx a lot.
I have not tried yet these script but I have included in my module.

I can not understand why the effect goes away when the player lv up,there is no remove effect in my OnLv_UP event.

I thought that another solution could be to force the pc to requip the item when he lv_UP.
What about this solution?
and what do you think is the best?
               
               

               
            

Legacy_GhostOfGod

  • Hero Member
  • *****
  • Posts: 1490
  • Karma: +0/-0
tagbased problems
« Reply #19 on: May 17, 2012, 04:56:15 pm »


               Personally I would try re-adding the effect with the piece of script above your last post(or something similar). You can run into more issues with re-equipping items. Either way you need to loop through all equipped items but when you un-equip/re-equip them you run into the occasional problem of having a full inventory and items being left on the ground, etc.
So just re-adding the effects if a certain item is already equipped is probably the simplest solution.
               
               

               


                     Modifié par GhostOfGod, 17 mai 2012 - 03:57 .
                     
                  


            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
tagbased problems
« Reply #20 on: May 17, 2012, 06:55:46 pm »


               If his problem in level up, Why not just run the TagBased scripts for all equipted items.  

SetUserDefinedItemEventNumber(X2_ITEM_EVENT_EQUIP);
EcxuteScript(OBJECT_SELF, GetTag(oEquip) );   // OBJECT_SELF should be the module for onPCLevelup.

Excuse any errors Im not at the house right now.
               
               

               
            

Legacy_GhostOfGod

  • Hero Member
  • *****
  • Posts: 1490
  • Karma: +0/-0
tagbased problems
« Reply #21 on: May 17, 2012, 09:19:07 pm »


               I already know L8 is the better scripter here so his solution is most likely the best approach.
This way of doing it has also crossed my mind but I don't use it probably from bad information I've picked up along the way or lack of information more likely. So your reply brings up a question for me L8.

Let's say the player has 5 of these items equipped. Then when the player levels up wouldn't 6 scripts then run? Or if you execute a script from a script does it just consider it part of that one script?
The reason I didn't and haven't used your approach L8 is because of my stingy use of resources. I'd rather just run the 1 level up script than run 6 scripts.

I'd like to know your opinion and knowledge on this.

Thanks L8.
               
               

               


                     Modifié par GhostOfGod, 17 mai 2012 - 08:20 .
                     
                  


            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
tagbased problems
« Reply #22 on: May 20, 2012, 04:06:20 pm »


               

GhostOfGod wrote...

I already know L8 is the better scripter here so his solution is most likely the best approach.
This way of doing it has also crossed my mind but I don't use it probably from bad information I've picked up along the way or lack of information more likely. So your reply brings up a question for me L8.

Let's say the player has 5 of these items equipped. Then when the player levels up wouldn't 6 scripts then run? Or if you execute a script from a script does it just consider it part of that one script?
The reason I didn't and haven't used your approach L8 is because of my stingy use of resources. I'd rather just run the 1 level up script than run 6 scripts.

I'd like to know your opinion and knowledge on this.

Thanks L8.


 I am not quite following you logic here.   Yes if there are 5 items with Tag based events equipted,  Then when the Level up evet runs the 5 additonal events scripts will run durning the level up.   The 5 scripts will be ran before the first one finishes, I do not know if you would, I guess you could concider that one script.  I myself would still call it six scripts running. 

  What I do not see is any extra resources, since the TagBased scripts for the events already exsist.  If you really do not like the extra resources, the option is then to disable Tag Based Scripting all together and going back to the system of putting all the code for module events back into the ModuleEvent scripts.   In that case you could still have the OnLevelUp run the Module event script.  It will just take a little more set up .  You would need to create a way for the correct item being equipted to passes to the Module On Equiped event.     

I guess takling a little about the differances between Tag Based Scripting and Just packing all the code into the Module Events is in orfer at this point.   

Advantages To Tagbased scripting as I see them.  
  1)  All code For a Single Item is in a single Script.  This makes it eaiser to code and debug  what  is going on with a single item.  

  2)  There is no massive chained if statments ( if... else if...) to serarch through. Will discuss this more below.
     

DisAdvantages to Tag Based scripting. 
  1)  Massive resource count.  There  Are Two  resource per Item with Module Events.  Well Mabey one I have never been clear on if the source code even counts towards the resource count. never heard any one talk about it and have never looked into it. 

  2)   Harder to Debug what is going on in the module.  A missfireing TBS due to a bad filter can be hard to track down.  


Advantages of NON-Tagbased scripting.   
1)  Lower Resource Count.   The only Resources are the Original Module Events. 

2)  Easier to debug module events.  basicly there is no way for an aquire event to be bugged to run an activate/equiped event by mistake.  short of placing the code in the wrong script. 

disadvantages of NON-Tagbased Scripting. 
1)  Items with multiple Item events have there code scattered across several large scripts. 

2)  The massive conditional statment ( if.. else if...)  making rather large scripts harder to search through. . 

3)  The Massive conditional adding to the Instruction limit ( Too Many Instructions Error).  In Events with a lot of conditionals this can cause you to have to either keep Item events with lots of instructions at the begining of the 'if chain' or split them out into there own scripts anyway. 


I know there are more advantages and dis advantages then I have listed, I am just drawing a blank right now.   

As hented  above will now talk about ' if... else if...' vs the Executescript aspect of the two systems.   I feel that this is what your question was really about anyway. 

The ExecuteScript, as used in TBS,  is an  emulation of how switch case statments should work, minus the ability of leaving out the break to allow excution to fall through to the next case.  In order to explain this I will first explain how  if... else if...   statments work  and how the switch... case:...  statments work in NWScript vs other languages. 


.........
Hmmm.  Out of time right now.   I will try and continue this tonight.  if not it might be next weekend before I can get back to it.  Sorry for the delays in answering.
               
               

               
            

Legacy_GhostOfGod

  • Hero Member
  • *****
  • Posts: 1490
  • Karma: +0/-0
tagbased problems
« Reply #23 on: May 21, 2012, 04:33:43 am »


               

Lightfoot8 wrote...
  Sorry for the delays in answering.

No worries L8. I work full time and got 2 kids. I know how it gos. '<img'>

I was just wondering if there was any disadvantage or advantage in using my idea of readding the effects in the level up script like so (one script; level up):

object oItem;
    int nSlot;

    for (nSlot=0; nSlot<NUM_INVENTORY_SLOTS; nSlot++)
    {
        oItem=GetItemInSlot(nSlot, oPC);//(oPlayer, oDead, oLevelupper, etc..)
        if (GetIsObjectValid(oItem))
        {
            string sTag = GetTag(oItem);
            if (sTag == "Helm_of_Blind")
            {
                AssignCommand(oItem, ApplyEffectToObject(DURATION_TYPE_PERMANENT, SupernaturalEffect(EffectBlindness()), oPC));
            }
            if (sTag == "Helm_of_Deaf")
            {
                AssignCommand(oItem, ApplyEffectToObject(DURATION_TYPE_PERMANENT, SupernaturalEffect(EffectDeaf()), oPC));
            }
            if (sTag == "Pants_of_Poo")
            {
               
AssignCommand(oItem, ApplyEffectToObject(DURATION_TYPE_PERMANENT,
SupernaturalEffect(EffectAbilityDecrease(ABILITY_CHARISMA, 1)), oPC));
            }
            //etc..
        }
    }


As apposed to your idea (which I like) of re-running the tag based scripts on level up (six scripts; level up + 5 tag based in the example I mentioned previously).

I haven't done it the way you suggested because it "seems" like it'd be inefficient. Which is also why I don't use the ExecuteScript function if I can do without it in that particular script. But I don't know how the execute script works exactly. Is there any efficiency loss when using ExecuteScript several times or often in other scripts as apposed to just putting what you need in the one script?

The less scripts that run, the better?

Hopefully this makes more sense. 
               
               

               


                     Modifié par GhostOfGod, 21 mai 2012 - 03:48 .
                     
                  


            

Legacy_Cursed_Eclipse

  • Newbie
  • *
  • Posts: 14
  • Karma: +0/-0
tagbased problems
« Reply #24 on: May 27, 2012, 08:28:15 pm »


               i try both solution but do not work.