Aller au contenu

Photo

How do you merge scripts?


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

#1
Ugly_Duck

Ugly_Duck
  • Members
  • 76 messages
I'm trying to merge PnP Coins with MPWC Spawnbeta6.  The scripts are supposed to go into the onclient enter section (Nevermind the "ClientExit" thing, it's just backwards).  Here are the scripts:

//::///////////////////////////////////////////////
//:: Coin Conversion System
//:: CRP_AREA_START_CLIENTENTER
//:: Version 2.0 for NWN2
//:: August 2007
//:: by Accerak
//:://////////////////////////////////////////////
#include "crp_inc_coinage"
void main()
{
 object oPC = GetEnteringObject();
 if (!GetIsPC(oPC)) return;
 if (GetItemPossessedBy(oPC, "coinpouch")!= OBJECT_INVALID)
    return;
 CreateItemOnObject("coinpouch", oPC);
 
 if (GetItemPossessedBy(oPC, "pouchvalue")!= OBJECT_INVALID)
    return;
 CreateItemOnObject("pouchvalue", oPC);
 
 if (GetItemPossessedBy(oPC, "coinvalue")!= OBJECT_INVALID)
    return;
 CreateItemOnObject("coinvalue", oPC);
 
 int nNWNGold = GetGold(oPC);
 AssignCommand(oPC, TakeGoldFromCreature(nNWNGold, oPC, TRUE));
 CreateItemOnObject("crp_coin_4", oPC, nNWNGold);
 
}

... and this is the second one:

//
// Module - OnClientExit
//
// Catches any 'counted' PCs that reenterthe game.  Th OnClientExit routine has
// decounted them, however they seem to be geting restored from some internal
// checkpoint when the player reconnect.  That means that all we do in here is
// to clear the counted indicator, which is preventing the player from being
// counted when they reenter their area.
//
#include "spawn_inc"
void main() {
    object oPC = GetEnteringObject();
 PrintString("spawn_modoncliententer: " + GetName(oPC));
    // Only react to entering counted things
 object area = GetLocalObject(oPC, "spawn_counted_in_area");
 
    if (GetIsObjectValid(area)) {
  PrintString("spawn_modoncliententer: " + GetName(oPC) + " sneaking into " + GetName(area));
 
  // Clear the area record, incase the player object gets recycled...
  
  SetLocalObject(oPC, "spawn_counted_in_area", OBJECT_INVALID); 
     // Do we have to count them as well?
   
  return;
    }
 return;
}

#2
Shallina

Shallina
  • Members
  • 1 012 messages
if you start to do custom stuff you need to learn a bit of scripting yourself.

Knightmare made a tutorial, scripting for noobs. Check it, it has the basic, and that should be more than enought for what you want to do.

There is 2 way to do what you want. A single void main(){ } that has the 2 body of your 2 script.

Or an other script that would call those script with the function ExecuteScipt.

#3
_Knightmare_

_Knightmare_
  • Members
  • 643 messages
Yup, check my tutorial (linked in sig below). It has a short section on merging scripts together.

#4
painofdungeoneternal

painofdungeoneternal
  • Members
  • 1 799 messages
Add something like this to the current events, inside of the main() { } that is there now

DelayCommand(0.05, ExecuteScript("yourscript", oPC));

and then change the words "yourscript" to match your "yourscript.ncs" file.

In the exiting script i'd imagine you'd need to do
AssignCommand( ExecuteScript("yourscript", oPC) );

#5
Ugly_Duck

Ugly_Duck
  • Members
  • 76 messages
Thanks for thehelp, i got it going well now.

#6
The Fred

The Fred
  • Members
  • 2 516 messages
People often ask for help on merging scripts, but it's really only something which can be applied on a case-by-case basis. Most of the time, if the scripts are doing different things, you can just stick one bit after the other.

Remember that each script has only one void main(). Put it and its following open squiggly bracket (technical term), {, then the first script, then the second script (removing the void main() and outmost squiggly brackets from both) and then close your squiggly brackets.

E.g:
void main()
{
    //Code 1
}
PLUS
void main()
{
    //Code 2
}
EQUALS
void main()
{
    //Code 1
    //Code 2
}

The only real thing you will have to do with many merges is watch out for duplicate variables. For example, both your scripts are OnClientEnters and so both start with
object oEnt = GetEnteringObject();
You only need this once, because all it's doing is finding out which object is entering (believe it or not). So, remove the second instance, or the compiler will get upset. Also make sure you have no other variables of the same name - you may have to rename variables in the second set.

Note that if your first script has
object oEnt = GetEnteringObject();
and your second script has
object oEnteringPC = GetEnteringObject();
for example, you don't need to do anything (though the code is doing the same thing twice, so it's inefficient).

As a general tip, a lot of the time you can just do this sort of thing:
void main()
{
    ExecuteScript("Script1", OBJECT_SELF);
    ExecuteScript("Script2", OBJECT_SELF);
}
then keep each original script as it is.

EDIT: I see this is essentially what pain just suggested.

Modifié par The Fred, 21 août 2011 - 05:47 .


#7
Ugly_Duck

Ugly_Duck
  • Members
  • 76 messages
Thanks for the tip - it helped me out a lot!