Angelo Kerman Posted August 5, 2016 Share Posted August 5, 2016 If I call UpgradeableFacility.setNormLevel, what else do I need to do to make sure that the space center updates the buildings? When I call that method, the buildings update but the game won't highlight the buildings when I mouseover them. Here's what I do: public virtual void ApplyFacilities(bool saveGame = false) { if (facilitiesNode == null) { Debug.Log("No facilities snapshot, exiting"); return; } ConfigNode[] nodes = facilitiesNode.GetNodes(); float level; for (int index = 0; index < nodes.Length; index++) { if (ScenarioUpgradeableFacilities.protoUpgradeables.ContainsKey(nodes[index].name)) { level = float.Parse(nodes[index].GetValue("lvl")); foreach (Upgradeables.UpgradeableFacility facility in ScenarioUpgradeableFacilities.protoUpgradeables[nodes[index].name].facilityRefs) { facility.setNormLevel(level); } } } //Save the game if (saveGame) GamePersistence.SaveGame("persistent", HighLogic.SaveFolder, SaveMode.BACKUP); } In the above code, I've copied the config node for each building in the facilities scenario, and all I'm doing is reading the values and telling the appropriate facility refs to set their upgrade level. It works, and the building changes its upgrade level, but then subsequent mouse-over events don't get noticed and the building doesn't highlight. Link to comment Share on other sites More sharing options...
JPLRepo Posted August 5, 2016 Share Posted August 5, 2016 what are you trying to do exactly? I'm confused what you are trying to do? You are reading the current level of each Upgradeable (although there is an easier way see below) and then trying to set the level to the same value and then save the game? Not sure I understand why you are doing this? The ScenarioUpgradeableFacilities is automatically persisted to the save file already? BTW: ScenarioUpgradeableFacilities.Instance.GetFacilityLevel(SpaceCenterFacility enum value); will get the current facility level for whichever SpaceCenterFacility you pass in (it's an Enum). Link to comment Share on other sites More sharing options...
JPLRepo Posted August 5, 2016 Share Posted August 5, 2016 (edited) I think you need to be looking at the SpaceCenterBuilding class instead. You need to probably iterate the destructible array that is part of this class for each building and call the Reset method on each, and then setNormLevel. I'm guessing your issue with the Highlighting is tied to the GUI and the fact that you are not triggering this via the UI, I suspect it is therefore not doing something to the UI that you are missing which is causing that issue. So the Tooltips? are not working when you over over the Facilities after you do this? What about the Building Picker? (the icons down the left side). The timing could also important here.. are you doing this in the Spacecenter Scene? After Awake? If before, timing will be an issue. Edited August 5, 2016 by JPLRepo Link to comment Share on other sites More sharing options...
Angelo Kerman Posted August 9, 2016 Author Share Posted August 9, 2016 On 8/4/2016 at 11:37 PM, JPLRepo said: I think you need to be looking at the SpaceCenterBuilding class instead. You need to probably iterate the destructible array that is part of this class for each building and call the Reset method on each, and then setNormLevel. I'm guessing your issue with the Highlighting is tied to the GUI and the fact that you are not triggering this via the UI, I suspect it is therefore not doing something to the UI that you are missing which is causing that issue. So the Tooltips? are not working when you over over the Facilities after you do this? What about the Building Picker? (the icons down the left side). The timing could also important here.. are you doing this in the Spacecenter Scene? After Awake? If before, timing will be an issue. How do you get an instance of the SpaceCenterBuilding? I haven't been able to find it. Link to comment Share on other sites More sharing options...
JPLRepo Posted August 9, 2016 Share Posted August 9, 2016 1 hour ago, Angel-125 said: How do you get an instance of the SpaceCenterBuilding? I haven't been able to find it. How about PsystemSetup.Instance.GetSpaceCenterFacilities(); - will give you an array of PsystemSetup.SpaceCenterFacility or PsystemSetup.Instance.GetSpaceCenterFacility("<nameoffacility>"); will return the single PsystemSetup.SpaceCenter Link to comment Share on other sites More sharing options...
Angelo Kerman Posted August 9, 2016 Author Share Posted August 9, 2016 10 hours ago, JPLRepo said: How about PsystemSetup.Instance.GetSpaceCenterFacilities(); - will give you an array of PsystemSetup.SpaceCenterFacility or PsystemSetup.Instance.GetSpaceCenterFacility("<nameoffacility>"); will return the single PsystemSetup.SpaceCenter Sadly PsystemSetup.SpaceCenterFacility doesn't give me the ability to set the level. So far I haven't found an API that gets me access to the SpaceCenterFacility object. Any ideas? Link to comment Share on other sites More sharing options...
JPLRepo Posted August 9, 2016 Share Posted August 9, 2016 Let's go back a step. I believe what you are doing in your original code is the correct way to set the upgrade level of the facilities. I think the issue here is, in stock it does not set the level in that way during the game (only on scene load) and what is missing is reset of the ToolTip UI objects attached to the facility prefab. During normal gameplay the user initiates the upgrade via the GUI windows. I suspect by calling the setlevel function, the GUI is not being updated correctly to identify the upgraded building prefab. You said originally that highlighting and mouse-over events stop working on the upgraded buildings. What about the building picker icons? The icons down the left side of the space center. Do these still appear? and do they still work? I think what is missing here is by call setNormLevel you are skipping the GameEvents that trigger the UI SpaceCenterBuilding from updating. I think you actually need to call the SetLevel method of the UpgradeableFacility as it triggers the GameEvents. Try changing that bit of your code like this and see what happens: <snip - rest of code the same above> if (ScenarioUpgradeableFacilities.protoUpgradeables.ContainsKey(nodes[index].name)) { level = float.Parse(nodes[index].GetValue("lvl")); foreach (Upgradeables.UpgradeableFacility facility in ScenarioUpgradeableFacilities.protoUpgradeables[nodes[index].name].facilityRefs) { // You have to set your level to an in from the normalized value first, which is what setNormLevel does, but it doesn't fire the GameEvents level = Mathf.Clamp01(level); level = Mathf.FloorToInt(level * (float)(facility.MaxLevel + 1)); level = Mathf.Clamp(level, 0, facility.MaxLevel); //Now call SetLevel, which does fire the GameEvents using the int level value from the normalized value. facility.SetLevel(level); } } <snip - rest of code the same below> Link to comment Share on other sites More sharing options...
Angelo Kerman Posted August 9, 2016 Author Share Posted August 9, 2016 3 hours ago, JPLRepo said: Let's go back a step. I believe what you are doing in your original code is the correct way to set the upgrade level of the facilities. I think the issue here is, in stock it does not set the level in that way during the game (only on scene load) and what is missing is reset of the ToolTip UI objects attached to the facility prefab. During normal gameplay the user initiates the upgrade via the GUI windows. I suspect by calling the setlevel function, the GUI is not being updated correctly to identify the upgraded building prefab. You said originally that highlighting and mouse-over events stop working on the upgraded buildings. What about the building picker icons? The icons down the left side of the space center. Do these still appear? and do they still work? I think what is missing here is by call setNormLevel you are skipping the GameEvents that trigger the UI SpaceCenterBuilding from updating. I think you actually need to call the SetLevel method of the UpgradeableFacility as it triggers the GameEvents. Try changing that bit of your code like this and see what happens: <snip - rest of code the same above> if (ScenarioUpgradeableFacilities.protoUpgradeables.ContainsKey(nodes[index].name)) { level = float.Parse(nodes[index].GetValue("lvl")); foreach (Upgradeables.UpgradeableFacility facility in ScenarioUpgradeableFacilities.protoUpgradeables[nodes[index].name].facilityRefs) { // You have to set your level to an in from the normalized value first, which is what setNormLevel does, but it doesn't fire the GameEvents level = Mathf.Clamp01(level); level = Mathf.FloorToInt(level * (float)(facility.MaxLevel + 1)); level = Mathf.Clamp(level, 0, facility.MaxLevel); //Now call SetLevel, which does fire the GameEvents using the int level value from the normalized value. facility.SetLevel(level); } } <snip - rest of code the same below> I'll give that a try, thanks! That did the trick, thanks again! Link to comment Share on other sites More sharing options...
JPLRepo Posted August 9, 2016 Share Posted August 9, 2016 So it worked? That's Excellent. Sorry for a bit of a run around earlier in the week. I didn't sit down and actually think it through properly the first time. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now