public static void CopyVariables(NWObject oSource, NWObject oCopy) { int variableCount = NWNXObject.GetLocalVariableCount(oSource); for (int variableIndex = 0; variableIndex < variableCount - 1; variableIndex++) { NWNXObject.LocalVariable stCurVar = NWNXObject.GetLocalVariable(oSource, variableIndex); switch (stCurVar.Type) { case LocalVariableType.Int: oCopy.SetLocalInt(stCurVar.Key, oSource.GetLocalInt(stCurVar.Key)); break; case LocalVariableType.Float: oCopy.SetLocalFloat(stCurVar.Key, oSource.GetLocalFloat(stCurVar.Key)); break; case LocalVariableType.String: oCopy.SetLocalString(stCurVar.Key, oSource.GetLocalString(stCurVar.Key)); break; case LocalVariableType.Object: oCopy.SetLocalObject(stCurVar.Key, oSource.GetLocalObject(stCurVar.Key)); break; case LocalVariableType.Location: oCopy.SetLocalLocation(stCurVar.Key, oSource.GetLocalLocation(stCurVar.Key)); break; } } }
/// <summary> /// Retrieves all local string variables on an object matching a given prefix. /// Returns a list containing the scripts to run sequentially. /// </summary> /// <param name="target">The object to pull variables from</param> /// <param name="prefix">The prefix to look for</param> /// <returns>A list of scripts to run, ordered from lowest to highest</returns> public static IEnumerable <string> FindByPrefix(NWGameObject target, string prefix) { var variableCount = NWNXObject.GetLocalVariableCount(target); var variableList = new SortedList <int, string>(); for (int x = 0; x < variableCount; x++) { var variable = NWNXObject.GetLocalVariable(target, x); if (variable.Type == LocalVariableType.String && variable.Key.StartsWith(prefix)) { // If the rest of the variable key can be converted to an int, add it to the list. var skipCharacters = prefix.Length; var orderSubstring = variable.Key.Substring(skipCharacters); if (!int.TryParse(orderSubstring, out var order)) { // Couldn't parse an integer out of the key name. Move to the next variable. continue; } // If the variable ID has already been assigned, skip to the next variable. if (variableList.ContainsKey(order)) { Log.Warning($"Variable '{prefix}' for ID {order} already exists. Ignoring second entry."); continue; } // Add the script to the list. var value = GetLocalString(target, variable.Key); variableList.Add(order, value); } } return(variableList.Values.ToList()); }
public bool Run(params object[] args) { int type = _.GetInventoryDisturbType(); if (type != INVENTORY_DISTURB_TYPE_ADDED) { return(true); } NWPlaceable device = Object.OBJECT_SELF; NWPlayer player = _.GetLastDisturbed(); NWItem item = _.GetInventoryDisturbItem(); var componentIP = item.ItemProperties.FirstOrDefault(x => _.GetItemPropertyType(x) == (int)CustomItemPropertyType.ComponentType); // Not a component. Return the item. if (componentIP == null) { ItemService.ReturnItem(player, item); player.FloatingText("You cannot scrap this item."); return(false); } // Item is a component but it was crafted. Cannot scrap crafted items. if (!string.IsNullOrWhiteSpace(item.GetLocalString("CRAFTER_PLAYER_ID"))) { ItemService.ReturnItem(player, item); player.FloatingText("You cannot scrap crafted items."); return(false); } // Remove the item properties foreach (var ip in item.ItemProperties) { var ipType = _.GetItemPropertyType(ip); if (ipType != (int)CustomItemPropertyType.ComponentType) { _.RemoveItemProperty(item, ip); } } // Remove local variables (except the global ID) int varCount = NWNXObject.GetLocalVariableCount(item); for (int index = varCount - 1; index >= 0; index--) { var localVar = NWNXObject.GetLocalVariable(item, index); if (localVar.Key != "GLOBAL_ID") { switch (localVar.Type) { case LocalVariableType.Int: item.DeleteLocalInt(localVar.Key); break; case LocalVariableType.Float: item.DeleteLocalFloat(localVar.Key); break; case LocalVariableType.String: item.DeleteLocalString(localVar.Key); break; case LocalVariableType.Object: item.DeleteLocalObject(localVar.Key); break; case LocalVariableType.Location: item.DeleteLocalLocation(localVar.Key); break; default: throw new ArgumentOutOfRangeException(); } } } if (!item.Name.Contains("(SCRAPPED)")) { item.Name = item.Name + " (SCRAPPED)"; } device.AssignCommand(() => { _.ActionGiveItem(item, player); }); return(true); }