1

I'm very new to C# so please excuse me if i'm asking stupid question or i'm making my question incorrectly.

Here is my code:

using UnityEngine;
using System;

namespace DuloGames.UI
{
    [Serializable]
    public class UISpellInfo
    {
        public int ID;
        public string Name;
        public Sprite Icon;
        public string Description;
        public float Range;
        public float Cooldown;
        public float CastTime;
        public float PowerCost;
        public float test;

        [BitMask(typeof(UISpellInfo_Flags))]
        public UISpellInfo_Flags Flags;
    }
}

Here is my UIspellDatabase class:

using UnityEngine;

namespace DuloGames.UI
{
    public class UISpellDatabase : ScriptableObject {

        #region singleton
        private static UISpellDatabase m_Instance;
        public static UISpellDatabase Instance
        {
            get
            {
                if (m_Instance == null)
                    m_Instance = Resources.Load("Databases/SpellDatabase") as UISpellDatabase;

                return m_Instance;
            }
        }
        #endregion

        public UISpellInfo[] spells;

        /// <summary>
        /// Get the specified SpellInfo by index.
        /// </summary>
        /// <param name="index">Index.</param>
        public UISpellInfo Get(int index)
        {
            return (spells[index]);
        }

        /// <summary>
        /// Gets the specified SpellInfo by ID.
        /// </summary>
        /// <returns>The SpellInfo or NULL if not found.</returns>
        /// <param name="ID">The spell ID.</param>
        public UISpellInfo GetByID(int ID)
        {
            for (int i = 0; i < this.spells.Length; i++)
            {
                if (this.spells[i].ID == ID)
                    return this.spells[i];
            }

            return null;
        }
    }
}

Here is how i foreach the existing instances of UISpellInfo:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using WorldServer;
using Spells;
using DuloGames.UI;

    public class Character : MonoBehaviour
    {

    private void SeeAllInstances()
        {

        foreach (var item in UISpellDatabase.Instance.spells)
        {
            Debug.Log("ID->" + item.ID + " name: " + item.Name);
        }


    }

I can clearly see all the instances i have for this class UISpellInfo. What i want to achieve is just to add more instances to array UISpellInfo[] spells;.

As far as i understand all the instances of the class UISpellInfo are loaded here UISpellInfo[] spells; then on the foreach i do in SeeAllInstances() i can see all the instances and the data hold for every instance.

My question if i'm even asking correctly is how to add more instances in UISpellInfo[] spells; ?

Can someone help me on this ?

2 Answers 2

4

I think you need to use List<T> for your scenario, as the Array is declared with a size and we have to keep track when adding removing elements to know how many elements are in it and which slots have value or not so that we can add new item at the last available index.

For the situations where we don't know in advance the number of elements that will go in it List<T> is a better option and if you are using that then you just need to call Add() method on your instance to add new item in it.

Your code in that case would be like:

public List<UISpellInfo> spells;

and this will hold all the items of type UISpellInfo and now down somewhere in your code you can do like following to add:

UISpellDatabase.Instance.spells.Add(new UISpellInfo());
Sign up to request clarification or add additional context in comments.

6 Comments

But then this array UISpellInfo[] spells; will stay unmodified ?
@TonyStark you can replace your array to the List<T> and call Add() on it to insert nee object in it with the previous ones as well in it
Thank you very much for helping me. But then can you check this function public UISpellInfo GetByID(int ID) if i only replace this this.spells.Length with this this.spells.Count should all be fine ?
yes @TonyStark Count will give number of item in the collection
With great pleasure.
|
2

To add objects to an array, you would need to reconstruct the array with the new element, and assign it to UISpellInfo[] spells; Here's a method that you can use to accomplish this:

public void AddUISpellInfo(UISpellInfo infoToAdd) {
    Array.Resize(ref this.spellInfoArray, this.spellInfoArray.Length + 1);
    this.spellInfoArray[this.spellInfoArray.Length - 1] = infoToAdd;
}

The method takes whatever item you wish to add as an explicit parameter, as well as a reference to the existing array via the implicit parameter this. You can see more on Array.Resize() here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.