Issue cloning main PC

I am having a problem cloning the player created character. The problem is that the clone retains the player character’s tag. If I SetTag(oPC, “mainPC”) immediately after character creation. Then create a clone and SetTag(oClone, “clonePC”). And then place a debug string on oPC = GetObjectByTag(“mainPC”), the debug string appears above the clone. Which is thoroughly weird.

The clone code is as follows:

//oCrit is "mainPC"

//iLoc is waypoint location for spawn

//sTag is "clonePC"

void cloneCreature(object oCrit, location lLoc, string sTag){

object oNPC = CopyObject(oCrit, lLoc);

SetTag(oNPC, sTag);

//see x2_im_setupclone.nss




Does anyone know what I am doing wrong?

I don’t know what causes this problem, but

object oNPC = CopyObject(oCrit, lLoc);

SetTag(oNPC, sTag);

can be replaced with

oNPC = CopyObject(oCrit, lLoc, OBJECT_INVALID, sTag);
* Duplicates the object specified by oSource.
* ONLY creatures and items can be specified.
* If an owner is specified and the object is an item, it will be put into their inventory
* If the object is a creature, they will be created at the location.
* If a new tag is specified, it will be assigned to the new object.
* @param
* @return
object CopyObject(object oSource, location locLocation, object oOwner = OBJECT_INVALID, string sNewTag = "");

Also pay attention to this note (I know this is from NWN1):

There are problems with the new tag as well. Calling GetTag() with the copied object as a parameter works, but calling GetObjectByTag() won’t return the copy, or at least it won’t all the time. Calling GetTag(GetObjectByTag("")) will also return the tag of the most recently copied object.


Thankyou for the reply Aqvilinus. That does seem to fix the problem. If I use oNPC = CopyObject(oCrit, lLoc, OBJECT_INVALID, sTag) then debug with debug(oPC, GetTag(GetObjectByTag(“mainPC”))) I now get “mainPC” appearing above the main pc rather than the clone.

The initial reason for using GetObjectByTag was to secure access to the main pc. But I am not sure if this is a good idea anymore. I was using GetEnteringObject run from the OnClientEnter property of the first area in the module to grab the player and then assign them a unique tag. From then forward I could easily and without need for checking grab the player using GetObjectByTag, or so I thought. Is there a better more foolproof alternative, indistry standard I guess? GetFirstPC(TRUE), GetPrimaryPlayer.

Try this:

void main()
	location lLoc = GetLocation(GetWaypointByTag("put_waypoint_tag_here"));
	object oClone = CopyObject(GetFirstPC(), lLoc, OBJECT_INVALID, "clone");
	ChangeToStandardFaction(oClone, STANDARD_FACTION_COMMONER);

This will create a copy of the MAIN PC at the specified waypoint. It will then assign it a tag of “clone”.
If you want it to clone the currently controlled character, then change GetFirstPC() to GetFirstPC(FALSE).

1 Like