SandMouseAnarchy Posted June 19, 2019 Share Posted June 19, 2019 Im trying to set an aid item as a favorite item by script, i need the aid item to replace a weapon that is already favorited to slot x. Here is what i have so far... Scriptname SM_Silencer extends ObjectReference Keyword Property ma_SM_Silencer Auto Keyword Property HasSilencer Auto Formlist Property SM_Silencer_WeaponList Auto Weapon Property Silencer Auto Bool CurrentlyAttachingMod = False Int B = 0 Int C = 0 Potion Property SuppressorAID Auto Event OnInit() RegisterForMenuOpenCloseEvent("PipBoyMenu") EndEvent FUNCTION EquipSuppressor(Actor akActor) CurrentlyAttachingMod = True Weapon MyEquippedWeapon = akActor.getequippedweapon(0) If SM_Silencer_WeaponList.HasForm(MyEquippedWeapon) ObjectMod[] TempMods = (Self as objectreference).GetAllMods() If TempMods.Length > 0 Int i = 0 While i < TempMods.Length akActor.AttachModToInventoryItem(MyEquippedWeapon , TempMods[i]) Debug.notification("Suppressor attatched") i += 1 EndWhile EndIf EndIf CurrentlyAttachingMod = False EndFUNCTION FUNCTION FavoriteSuppressor(Int SuppressorIndex) Debug.notification("Suppressor is in slot" + SuppressorIndex) Game.GetPlayer().AddItem(SuppressorAID) Utility.Wait(0.25) Form[] favorites = new Form[12] favorites[SuppressorIndex] = SuppressorAID FavoritesManager.SetFavorites(favorites) Debug.Notification("Suppressor aid should be in favorites") EndFUNCTION Event OnMenuOpenCloseEvent(string asMenuName, bool abOpening) if (abOpening) if (asMenuName== "PipBoyMenu") RegisterForMenuOpenCloseEvent("PipBoyMenu") EndIf if (asMenuName== "FavoritesMenu") RegisterForMenuOpenCloseEvent("FavoritesMenu") Debug.notification("favorates menu opened") C = 1 EndIf EndIf if !(abOpening) RegisterForMenuOpenCloseEvent("PipBoyMenu") Form[] favorites = FavoritesManager.GetFavorites() If (favorites) int index = 0 While (index < favorites.Length) If favorites[index] == Silencer Int SuppressorIndex = Index Debug.MessageBox("Suppressor index is " + SuppressorIndex) FavoriteSuppressor(SuppressorIndex) EndIf index += 1 EndWhile Else Debug.Notification("There are no favorite items.") EndIf EndIf EndEvent Unfortunitely when testing in game the aid item wont overide the weapon, and im not really sure why(?) im trying to use these wiki pages for reference as i cant find anything else on google at the minute: https://www.creationkit.com/fallout4/index.php?title=SetFavorites_-_FavoritesManager https://www.creationkit.com/fallout4/index.php?title=GetFavorites_-_FavoritesManager Can an item overide another item in the same slot when the favorite is set by sccript?if it can - do i need to specify a number instead of "(SuppressorIndex)"? Link to comment Share on other sites More sharing options...
Wolfmark Posted June 19, 2019 Share Posted June 19, 2019 Try to call SetFavorites first and then use AddItem to add the item to player's inventory.Also you should first call GetFavorites, update the array to add your item and then call SetFavorites. But when I tried this I got some strange error, so my solution was: 1. Call GetFavorites to get the list with current favorites.2. Create a new array and copy the current favorites. Update this array to add your item and then call SetFavorites.3. Add the item to player's inventory. Link to comment Share on other sites More sharing options...
SandMouseAnarchy Posted June 21, 2019 Author Share Posted June 21, 2019 Hi, thanks WolfMark - im not entirely sure what you mean by copying the array and updating the array, would you mind explaining a little or showing an example of what you mean? Trying to follow your advice, iv re-written the function - this is what i have now -> FUNCTION FavoriteSuppressor() Utility.Wait(1) Form[] favorites = FavoritesManager.GetFavorites() If (favorites) int index = 0 While (index < favorites.Length) If favorites[index] == Silencer Int SuppressorIndex = Index Debug.MessageBox("Suppressor index is " + SuppressorIndex) Game.GetPlayer().AddItem(SuppressorAID) favorites.Remove(Index) favorites[Index] = SuppressorAID FavoritesManager.SetFavorites(favorites) EndIf index += 1 EndWhile Else Debug.Trace("There are no favorite items.") EndIf EndFUNCTION after testing this new piece of the script out - i still cant seem to set favorites or overide items already in favorite slots :( Link to comment Share on other sites More sharing options...
Wolfmark Posted June 21, 2019 Share Posted June 21, 2019 (edited) You must call first SetFavorites and then call AddItem. The code should look like this: Form[] favorites = FavoritesManager.GetFavorites() favorites[SuppressorIndex] = SuppressorAID FavoritesManager.SetFavorites(favorites) Game.GetPlayer().AddItem(SuppressorAID) But when I tried this for a mod I wrote few months ago it failed with "Mismatched types assigning to variable named FAVORITES" error when trying to change the favorites array returned by GetFavorites(), so I had to duplicate the array. See this: https://imgur.com/gC8Bhlh. So the code should look something like this: Form[] favorites = FavoritesManager.GetFavorites() Form[] newFavorites = new Form[favorites.length] int i = 0 while (i < favorites.length) newFavorites[i] = favorites[i] i += 1 endwhile newFavorites[SupressorIndex] = SuppressorAID FavoritesManager.SetFavorites(newFavorites) Game.GetPlayer().AddItem(SuppressorAID) or even better (or maybe not): Form[] favorites = FavoritesManager.GetFavorites() Form[] newFavorites = new Form[favorites.length] int i = 0 while (i < favorites.length) if (i == SuppressorIndex) newFavorites[i] = SuppressorAID else newFavorites[i] = favorites[i] endif i += 1 endwhile FavoritesManager.SetFavorites(newFavorites) Game.GetPlayer().AddItem(SuppressorAID) Edited June 21, 2019 by Wolfmark Link to comment Share on other sites More sharing options...
Recommended Posts