pepperman35 Posted June 12, 2021 Share Posted June 12, 2021 Greetings all, I want to streamline the following script fragment: MSL_MysticLakeSubstationEnable.enable() Utility.Wait(5.0) Debug.Notification("Substation at Mystic Lake has been constructed.") MLS_AbernathyFarmSSEnable.enable() Utility.Wait(5.0) Debug.Notification("Substation at Abernathy Farm has been constructed.") MLS_SunshineTidingsSSEnable.enable() Utility.Wait(5.0) Debug.Notification("Substation at Sunshine Tidings has been constructed.") MLS_RedRocketSSEnable.enable() Utility.Wait(5.0) Debug.Notification("Substation at Red Rocket has been constructed.") MLS_SanctuarySSEnable.enable() Utility.Wait(5.0) Debug.Notification("Substation at Sanctuary has been constructed.") MLS_StarlightDriveInSSEnable.enable() Utility.Wait(5.0) Debug.Notification("Substation at Starlight DriveIn has been constructed.") MLS_TenPinesBluffSSEnable.enable() Utility.Wait(5.0) Debug.Notification("Substation at Ten Pines Bluff has been constructed.") MLS_GrayGardenSSEnable.enable() Utility.Wait(5.0) Debug.Notification("Substation at Gray Garden has been constructed.") To that end, I have created the following script. The script complies and works to a degree. I created two formlist: 1) MLS_SSListQuad1 contains enable markers; and MLS_SSLocListQuad1 is location where the substations are at. MLS_SSLocListQuad1 was populated via dragging location entities into the formlist. The script enables everything properly but reporting out the location is “None” for each. Suggestions on how to incorporate location properly. I suppose I could manually populate an array with the names but I suspect there is a better mousetrap. Scriptname MysticLake:MLS_SubStationProtocolScript extends ObjectReference ; ;-- Properties -------------------------------------- ; FormList Property MLS_SSListQuad1 Auto Mandatory FormList Property MLS_SSLocListQuad1 Auto Mandatory ; ;-- Variables --------------------------------------- ; ; ;-- Functions --------------------------------------- ; ; Construct and enable settlement substations in quadrant 1 ; Function SubstationsinQuad1() Int iIndex = MLS_SSListQuad1.GetSize() ; Indices are offset by 1 relative to size While iIndex iIndex -= 1 ObjectReference Quad1StationRef = MLS_SSListQuad1.GetAt(iIndex) As ObjectReference ; Note that you must typecast the entry from the formlist using 'As'. ObjectReference Quad1StationLocRef = MLS_SSLocListQuad1.GetAt(iIndex) As ObjectReference ; Note that you must typecast the entry from the formlist using 'As'. Quad1StationRef.Enable() Utility.Wait(5.0) Debug.Notification("Substation at " + Quad1StationLocRef + " has been constructed.") EndWhile EndFunction Link to comment Share on other sites More sharing options...
niston Posted June 12, 2021 Share Posted June 12, 2021 Locations are not ObjectReferences, so the cast as ObjectReference fails.Try: Location Quad1StationLocRef = MLS_SSLocListQuad1.GetAt(iIndex) As Location Link to comment Share on other sites More sharing options...
pepperman35 Posted June 13, 2021 Author Share Posted June 13, 2021 cool, thank you sir. I need to get my head wrapped around this casting stuff. Link to comment Share on other sites More sharing options...
ReDragon2013 Posted June 16, 2021 Share Posted June 16, 2021 (edited) Its a good coding practice to check for valid entries. MysticLake:MLS_SubStationProtocolScript Scriptname MysticLake:MLS_SubStationProtocolScript extends ObjectReference ; https://forums.nexusmods.com/index.php?/topic/10136593-formlists-and-scripts/ FormList PROPERTY MLS_SSListQuad1 auto ; objectReferences ; 0 - MSL_MysticLakeSubstationEnable ; 1 - MLS_AbernathyFarmSSEnable ; 2 - MLS_SunshineTidingsSSEnable ; 3 - MLS_RedRocketSSEnable ; 4 - MLS_SanctuarySSEnable ; 5 - MLS_StarlightDriveInSSEnable ; 6 - MLS_TenPinesBluffSSEnable ; 7 - MLS_GrayGardenSSEnable FormList PROPERTY MLS_SSLocListQuad1 auto ; list of locations refering to formlist above ;-- FUNCTIONs -- 2 ;------------------------- FUNCTION myF_Enable(Int i) ; helper for reducing variables stack in caller function ;------------------------- objectReference oRef = MLS_SSListQuad1.GetAt(i) as ObjectReference ; first element formlist.GetAt(0) IF ( oRef ) oRef.Enable() ;;; oRef.EnableNoWait() location loc = MLS_SSLocListQuad1.GetAt(i) as Location IF ( loc ) Debug.Notification("Substation at " +loc+ " has been constructed.") ELSE Debug.Notification("Object enabled, but location is missing!") ENDIF ENDIF ENDFUNCTION ;---------------------------- FUNCTION SubstationsinQuad1() ; Construct and enable settlement substations in quadrant 1 ;---------------------------- int i = MLS_SSListQuad1.GetSize() WHILE (i) ; (i != 0) i = i - 1 myF_Enable(i) Utility.Wait(5.0) IF self.GetBaseObject() ; ok, try to enable next "substation" ELSE i = 0 ; the object (this script is attached to) is invalid! ENDIF ENDWHILE ENDFUNCTION Edited June 16, 2021 by ReDragon2013 Link to comment Share on other sites More sharing options...
pepperman35 Posted June 17, 2021 Author Share Posted June 17, 2021 Yes, I can see the utility in checking for valid entries. Btw, here is the code as it currently stands (still working it out): Scriptname MysticLake:MLS_SubStationProtocolScript extends ObjectReference ; ;-- Properties -------------------------------------- ; GlobalVariable Property pMLS_SSQ1ConstructState Auto Const Mandatory GlobalVariable Property pMLS_SSQ2ConstructState Auto Const Mandatory FormList Property MLS_SSListQuad1 Auto Mandatory FormList Property MLS_SSListQuad2 Auto Mandatory Component Property c_Steel Auto Const Mandatory Component Property c_Copper Auto Const Mandatory Component Property c_Ceramic Auto Const Mandatory Component Property c_Oil Auto Const Mandatory Ammo Property AmmoFusionCore Auto Const Mandatory Message Property pMLS_InsufficientResourcesMsg03 Auto Const Mandatory Keyword Property pWorkshopItemKeyword Auto Const Mandatory Message Property pMLS_CmdCntrTerminalNoWorkshop Auto Const Mandatory ; ; ;-- Variables --------------------------------------- ; string[] Quad1Locs string[] Quad2Locs ObjectReference PlayerRef ; ;-- Events ------------------------------------------ ; Event OnInit() ; ; Initialize player reference ; PlayerRef = Game.GetPlayer() ; ; Initialize the location array for quadrant #1 ; Quad1Locs = new string[9] Quad1Locs[0] = "Abernathy Farm" Quad1Locs[1] = "Covenant" Quad1Locs[2] = "Graygarden" Quad1Locs[3] = "Outpost Zimonja" Quad1Locs[4] = "Red Rocket Truck Stop" Quad1Locs[5] = "Sanctuary" Quad1Locs[6] = "Starlight Drive In" Quad1Locs[7] = "Sunshine Tidings Co-0p" Quad1Locs[8] = "Tenpines Bluff" ; ; Initialize the location array for quadrant #2 ; Quad2Locs = new string[11] Quad2Locs[0] = "Boston Airport" Quad2Locs[1] = "Bunker Hill" Quad2Locs[2] = "Coastal Cottage" Quad2Locs[3] = "Country Crossing" Quad2Locs[4] = "Croup Manor" Quad2Locs[5] = "Finch Farm" Quad2Locs[6] = "Greentop Nursery" Quad2Locs[7] = "Kingsport Lighthouse" Quad2Locs[8] = "Nordhagen Beach" Quad2Locs[9] = "Taffington Boathouse" Quad2Locs[10] = "The Slog" EndEvent ; ;-- Functions --------------------------------------- ; ; Construct and enable settlement substations in quadrant 1 ; Function SubstationsinQuad1() If (pMLS_SSQ1ConstructState.GetValue() == 0 as float) int Flag = 0 ObjectReference ThisWorkshop = Self.GetLinkedRef(pWorkshopItemKeyword) If (ThisWorkshop == None) ;Debug.Trace("MLS_ConstructionProtocolScript.SubstationsinQuad1 ERROR NoWorkshop", 0) pMLS_CmdCntrTerminalNoWorkshop.Show(0, 0, 0, 0, 0, 0, 0, 0, 0) Else ; Test to see if the player and/or workshop has requisite materials int nsl = PlayerRef.GetItemCount(c_Steel) int ncr = PlayerRef.GetItemCount(c_Copper) int ncc = PlayerRef.GetItemCount(c_Ceramic) int nol = PlayerRef.GetItemCount(c_Oil) int ncs = PlayerRef.GetItemCount(AmmoFusionCore) If (nsl >= 196) && (ncr >= 94) && (ncc >= 102) && (nol >= 58) && (ncs >= 7) RemoveFromInventory(PlayerRef, Flag, 196, 94, 102, 58, 7) SubstationConstructor(MLS_SSListQuad1, Quad1Locs) pMLS_SSQ1ConstructState.SetValue(1 as float) Else nsl = ThisWorkshop.GetItemCount(c_Steel) ncr = ThisWorkshop.GetItemCount(c_Copper) ncc = ThisWorkshop.GetItemCount(c_Ceramic) nol = ThisWorkshop.GetItemCount(c_Oil) ncs = ThisWorkshop.GetItemCount(AmmoFusionCore) If (nsl >= 196) && (ncr >= 94) && (ncc >= 102) && (nol >= 58) && (ncs >= 7) Flag = 1 RemoveFromInventory(ThisWorkshop, Flag, 196, 94, 102, 58, 7) SubstationConstructor(MLS_SSListQuad1, Quad1Locs) pMLS_SSQ1ConstructState.SetValue(1 as float) Else pMLS_InsufficientResourcesMsg03.Show(0, 0, 0, 0, 0, 0, 0, 0, 0) EndIf EndIf EndIf EndIf EndFunction Function SubstationsinQuad2() If (pMLS_SSQ2ConstructState.GetValue() == 0 as float) int Flag = 0 ObjectReference ThisWorkshop = Self.GetLinkedRef(pWorkshopItemKeyword) If (ThisWorkshop == None) ;Debug.Trace("MLS_ConstructionProtocolScript.SubstationsinQuad1 ERROR NoWorkshop", 0) pMLS_CmdCntrTerminalNoWorkshop.Show(0, 0, 0, 0, 0, 0, 0, 0, 0) Else ; Test to see if the player and/or workshop has requisite materials int nsl = PlayerRef.GetItemCount(c_Steel) int ncr = PlayerRef.GetItemCount(c_Copper) int ncc = PlayerRef.GetItemCount(c_Ceramic) int nol = PlayerRef.GetItemCount(c_Oil) int ncs = PlayerRef.GetItemCount(AmmoFusionCore) If (nsl >= 196) && (ncr >= 94) && (ncc >= 102) && (nol >= 58) && (ncs >= 7) RemoveFromInventory(PlayerRef, Flag, 196, 94, 102, 58, 7) SubstationConstructor(MLS_SSListQuad2, Quad2Locs) pMLS_SSQ2ConstructState.SetValue(1 as float) Else nsl = ThisWorkshop.GetItemCount(c_Steel) ncr = ThisWorkshop.GetItemCount(c_Copper) ncc = ThisWorkshop.GetItemCount(c_Ceramic) nol = ThisWorkshop.GetItemCount(c_Oil) ncs = ThisWorkshop.GetItemCount(AmmoFusionCore) If (nsl >= 196) && (ncr >= 94) && (ncc >= 102) && (nol >= 58) && (ncs >= 7) Flag = 1 RemoveFromInventory(ThisWorkshop, Flag, 196, 94, 102, 58, 7) SubstationConstructor(MLS_SSListQuad2, Quad2Locs) pMLS_SSQ2ConstructState.SetValue(1 as float) Else pMLS_InsufficientResourcesMsg03.Show(0, 0, 0, 0, 0, 0, 0, 0, 0) EndIf EndIf EndIf EndIf EndFunction Function RemoveFromInventory(ObjectReference oRef, int iFlag, int nsl, int ncr, int ncc, int nol, int ncs) oRef.RemoveItem(c_Steel, nsl, False, None) oRef.RemoveItem(c_Copper, ncr, False, None) oRef.RemoveItem(c_Ceramic, ncc, False, None) oRef.RemoveItem(c_Oil, nol, False, None) oRef.RemoveItem(AmmoFusionCore, ncs, False, None) If (iFlag == 1) Debug.Notification(nsl as string + " steel removed from workshop") Debug.Notification(ncr as string + " copper removed from workshop") Debug.Notification(ncc as string + " ceramic removed from workshop") Debug.Notification(nol as string + " oil removed from workshop") Debug.Notification(ncs as string + " fusion cores removed from workshop") EndIf EndFunction Function SubstationConstructor(FormList oRef2, string[] SSLocs) Int iMax = oRef2.GetSize() Int i = 0 While i <= iMax ObjectReference SubStationRef = oRef2.GetAt(i) As ObjectReference SubStationRef.Enable() Utility.Wait(2.5) Debug.Notification("Substation at " + SSLocs[i] as string + " has been constructed") i = i + 1 EndWhile EndFunction Link to comment Share on other sites More sharing options...
Recommended Posts