Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autogen Component Class #70

Open
6 tasks
RainbowIris323 opened this issue Nov 29, 2023 · 0 comments
Open
6 tasks

Autogen Component Class #70

RainbowIris323 opened this issue Nov 29, 2023 · 0 comments

Comments

@RainbowIris323
Copy link

Describe the mod example you'd like to see
A lengthy description of all functions methods etc for making autogen UI's for world object components using AutogenClass and Autogen/SyncToView attributes and tooltips with updates based on property value changes
General things to cover:

  • How to make an autogen ui
  • How to display it in different ways (if possible)
  • how to get the client to update on value changes
  • how to connect RPC methods to value changes
  • how to disable changing of the values
  • Tooltips as well
2023-11-28.21-32-40.mp4

Additional context
Currently making any kind of UI for components is a pain and the fact that I have spent countless hours trying to figure this out and have no results is kind of annoying so documentation would help.
For example i have code that just wont work shown here:

using Eco.Core.Controller;
using Eco.Core.Items;
using Eco.Gameplay.Items;
using Eco.RM.Items;
using Eco.Shared.Localization;
using Eco.Shared.Serialization;
using Eco.Shared.Utils;
using Eco.Gameplay.Objects;
using Eco.Gameplay.Components.Storage;
using System.ComponentModel;

namespace Eco.RM.Components
{
    [Serialized]
    [AutogenClass]
    [RequireComponent(typeof(PublicStorageComponent))]
    [LocDisplayName("Battery Storage")]
    [LocDescription("Battery storage for a object.")]
    [HasIcon("BatteryStorageComponentIcon")]
    [CreateComponentTabLoc("Battery Storage")]
    [Ecopedia("[RM] Stored Power", "Batteries", true, true, "Battery Storage")]
    public partial class BatteryStorageComponent : WorldObjectComponent
    {
        [Serialized, SyncToView, Autogen, ReadOnly(true)]
        public float MaxCharge { get; set; }

        [Serialized, SyncToView, Autogen, ReadOnly(true)]
        public float MaxDischargeRate { get; set; }

        [Serialized, SyncToView, Autogen, ReadOnly(true)]
        public float MaxChargeRate { get; set; }

        [Serialized, SyncToView, Autogen, ReadOnly(true)]
        public float CurrentCharge { get; set; }

        [Serialized, SyncToView, Autogen, ReadOnly(true)]
        public float EnergyTransferRate { get; set; }

        public Inventory Inventory => Parent.GetOrCreateComponent<PublicStorageComponent>().Inventory;
        float energyChangeLastTick;
        
        public void UpdateStats()
        {
            MaxCharge = Inventory.NonEmptyStacks.Sum((itemStack) => ((BatteryItem)itemStack.Item).MaxCharge);
            MaxDischargeRate = Inventory.NonEmptyStacks.Sum((itemStack) => ((BatteryItem)itemStack.Item).MaxDischargeRate);
            MaxChargeRate = Inventory.NonEmptyStacks.Sum((itemStack) => ((BatteryItem)itemStack.Item).MaxChargeRate);
            UpdateCharge();
        }
        public void UpdateCharge()
        {
            CurrentCharge = Inventory.NonEmptyStacks.Sum((itemStack) => ((BatteryItem)itemStack.Item).CurrentCharge);
        }

        public void Initialize(int slots)
        {
            Parent.GetOrCreateComponent<PublicStorageComponent>().Initialize(slots);
            Inventory.AddInvRestriction(new TagRestriction(new Tag[] {TagManager.GetOrMake("Battery")}));
            Inventory.SetOwner(Parent);
            UpdateStats();
        }
        public float Charge(float watts)
        {
            var og_watts = watts;
            Inventory.NonEmptyStacks.ForEach((itemStack) => {
                watts -= ((BatteryItem)itemStack.Item).Charge(watts);
            });
            energyChangeLastTick += og_watts - watts;
            return og_watts - watts;
        }
        public float Discharge(float watts)
        {
            var og_watts = watts;
            Inventory.NonEmptyStacks.ForEach((itemStack) => {
                watts -= ((BatteryItem)itemStack.Item).Discharge(watts);
            });
            energyChangeLastTick -= og_watts - watts;
            return og_watts - watts;
        }
        public override void LateTick()
        {
            if (energyChangeLastTick > 0) UpdateCharge();
            if (energyChangeLastTick != EnergyTransferRate) EnergyTransferRate = energyChangeLastTick;
            energyChangeLastTick = 0;
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant