Alright I've been writing C/C++ code for a few years but recently got into helping make an indie game with a few guys. We are scripting in C# but i'm just a bit lost on how to do some stuff without pointers.

Its a generic class that holds the non-work portion of a buff. So if I want to buff a character I make a Buff<Character> and assign an Action<Character> to the apply and remove parts. Should work fine in that regard, but thing is I want the buff to keep a
reference to its parent so I can get it back when its time to remove it and apply the "RemoveBuff" logic. Only thing I can think of is have the Buff create an event that the character class that holds it recognizes with an index and removes it, but that seems like a large workaround for what one single pointer could do.

[System.Serializable]
public class Buff<T>
{
    T Parent;
    //Protected Members
    public string Name;
    public string SubName;
    public string ToolTip;
    protected float InternalTimer;
    public bool Forever;
    public bool Active;
    //Public Variables 
    public Texture2D Icon;
    public Action<T> ApplyBuff;
    public Action<T> RemoveBuff;
    public float Duration;
    //Get Time remaining - Read Only
    public float TimeLeft
    {
        get { return InternalTimer; }

    }
    public void Update()
    {
            InternalTimer -= Time.deltaTime;
            if (InternalTimer <= 0)
            {
                InternalTimer = 0;
                RemoveBuff(Parent);
            }
    }
}

Recommended Answers

All 2 Replies

So send a reference into that class. This way will be available here and there. As long as you dont do a NEW referenve it "old" one always has the "old2 data holding inside.

Example:

class Class1
{
     private List<string> list = new List<string>(); //this is now our varaible to use (reference)

     public Class1()
     {
         AddSome();
         ChangeIt();
     }
     void AddSome()
     {
         list.Add("a");
         list.Add("b");
     }
     void ChangeIt()
     {
         Class2 c2 = new Class2(list); //passing list as referece to otrher class
         c2.ChangeList(); //do changes there (ot adding, or...)
         //when code comes back from class2 it will be already changed!!
     }       
}

class Class2
{
    List<string> list;
    public Class2(List<string< _list)
    {
        this.list = _list;
    }
    public void ChangeList()
    {
         list[0] = "aa";
         list[1] = "bb";
    }
}

Thanks, lol, eventually found this out. Just odd to be able to pass by pointer without purposefully doing it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.