Skip to content

Autogen Component Class #70

@RainbowIris323

Description

@RainbowIris323

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;
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions