//Handle network command function - Must be registered with the UIManager in order to be called properly //Registering it with the UIManager is best done in the FortressCraftMod class' Register method //The static function calls are designed to work equivalently on the server and client so that the two remain in sync public static NetworkInterfaceResponse HandleNetworkCommand(Player player, NetworkInterfaceCommand nic) { MyModMachine machine = nic.target as MyModMachine; string key = nic.command; if (key != null) { if (key == InterfaceMyFunctionItem) { MyModMachineWindow.MyFunctionItem(player, machine, nic.itemContext); } else if (key == InterfaceMyFunctionString) { int data; //Parse the string data (safely) and send to the appropriate function if (int.TryParse(nic.payload ?? "0", out data)) { MyModMachineWindow.MyFunctionString(player, machine, data); } } } return(new NetworkInterfaceResponse() { entity = machine, inventory = player.mInventory }); }
//You can also get a call when the player's cursor hovers over an icon/button so that you can display info about it //Example code below for displaying item info in the hotbar public override void ButtonEnter(string name, SegmentEntity targetEntity) { MyModMachine machine = targetEntity as MyModMachine; if (name == "itemicon") { ItemBase itemForSlot = ItemManager.SpawnItem(100); int count = itemForSlot.GetAmount(); if (itemForSlot == null) { return; } if (HotBarManager.mbInited) { HotBarManager.SetCurrentBlockLabel(ItemManager.GetItemName(itemForSlot)); } else { if (!SurvivalHotBarManager.mbInited) { return; } string name1 = !WorldScript.mLocalPlayer.mResearch.IsKnown(itemForSlot) ? "Unknown Material" : ItemManager.GetItemName(itemForSlot); if (count > 1) { SurvivalHotBarManager.SetCurrentBlockLabel(string.Format("{0} {1}", count, name1)); } else { SurvivalHotBarManager.SetCurrentBlockLabel(name1); } } } }
public static bool MyFunctionItem(Player player, MyModMachine machine, ItemBase item) { //Simplistic item transfer function - player data is available too! if (WorldScript.mbIsServer) { player.mInventory.AddItem(item); } machine.MarkDirtyDelayed(); dirty = true; if (!WorldScript.mbIsServer) { NetworkManager.instance.SendInterfaceCommand(InterfaceName, InterfaceMyFunctionItem, null, item, machine, 0.0f); } return(true); }
/////////////////////////////////////////////////////////////// //Network command functions /////////////////////////////////////////////////////////////// public static bool MyFunctionString(Player player, MyModMachine machine, int data) { //handle data machine data update here machine.data = data; machine.MarkDirtyDelayed(); //This can be used to force a redraw of the window in the event that the data update requires the window to change for other players networkredraw = true; //Send the command to the server if (!WorldScript.mbIsServer) { NetworkManager.instance.SendInterfaceCommand(InterfaceName, InterfaceMyFunctionString, data.ToString(), null, machine, 0.0f); } return(true); }
/////////////////////////////////////////////////////// //UI Functions /////////////////////////////////////////////////////// //This is called when the GenericMachinePanelScript tries to show your machine UI window if you decide to implement a UI window public override void SpawnWindow(SegmentEntity targetEntity) { MyModMachine machine = targetEntity as MyModMachine; if (machine == null) { return; } //Definition of window contents this.manager.SetTitle("My Machine Window Title"); this.manager.AddIcon("itemicon", "empty", Color.white, 0, 0); this.manager.AddBigLabel("labelidentifier", "Label text", Color.white, 0, 60); this.manager.AddButton("buttonidentifier", "Button Text", 0, 120); this.manager.AddPowerBar("powerbaridentifier", 0, 180); //Mark it dirty if you have contents that need updating immediately dirty = true; networkredraw = false; }
//Called every frame to update the window contents public override void UpdateMachine(SegmentEntity targetEntity) { MyModMachine machine = targetEntity as MyModMachine; //If the machine reference is lost we need to exit the window and remove the UI rules that lock the screen position allow cursor movement if (machine == null) { GenericMachinePanelScript.instance.Hide(); UIManager.RemoveUIRules("Machine"); return; } //Redraw if network update requires a change in the window contents if (networkredraw) { this.manager.RedrawWindow(); } //Some example update functions below this.manager.UpdatePowerBar("powerbaridentifier", 0, 100f); //This function hooks up the scroll wheel to the panel scroll bar GenericMachinePanelScript.instance.Scroll_Bar.GetComponent <UIScrollBar>().scrollValue -= Input.GetAxis("Mouse ScrollWheel"); //Only update content below if dirty - save time looking up icon information if they don't need to change if (!dirty) { return; } this.manager.UpdateIcon("itemicon", ItemManager.GetItemIcon(100), Color.white); this.manager.UpdateLabel("labelidentifier", "New Label Text", Color.white); //Set dirty to false once updated dirty = false; }