-2

SOLVED -- It appears that I was attempting to reference a struct (vector2) which isn't passed by reference. Therefore the code was variables / list were not reflecting upon one another.

Thanks everyone.

This is the code I'm using. I want to be able to change the variables that the List is referencing and have the list reflect the updated variables without searching through the list and updating it every time. This will make it much easier to update my buffs and what not.

Is this possible?

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Buff : MonoBehaviour{


    // (Buff Amount, Timer)
    Vector2 thrust = new Vector2(1,0);
    Vector2 mass = new Vector2(1,0);
    Vector2 shield = new Vector2(1,0);
    Vector2 armor = new Vector2(1,0);
    Vector2 structure = new Vector2(1,0);
    Vector2 regen = new Vector2(1,0);
    Vector2 explosionRadius = new Vector2(1,0);
    Vector2 damage = new Vector2(1,0);
    Vector2 disable = new Vector2(0,0);

    List<Vector2> buffs = new List<Vector2>();

    // Use this for initialization
    void Start () {
        buffs.Add(thrust);
        buffs.Add(mass);
        buffs.Add(shield);
        buffs.Add(armor);
        buffs.Add(structure);
        buffs.Add(regen);
        buffs.Add(explosionRadius);
        buffs.Add(damage);
        buffs.Add(disable);
    }

    // Update is called once per frame
    void Update () {

        // Makes Regen variable change.
        regen[0] = 5;

        // Can I make it affect what is in the List of Vector 2's?
        foreach (Vector2 buff in buffs)
        {
            if (buff.magnitude > 1)
                UpdateBuff(buff);
        }
    }

    // Therefore this will be called. Then the variable outside can also be updated?
    void UpdateBuff(Vector2 inBuff)
    {
        Debug.Log("Updating Buff: " + inBuff.ToString());
        if ((inBuff[1] -= Time.deltaTime) <= 0)
        {
            inBuff[0] = 1;
            inBuff[1] = 0;
        }
    }
Ken Rea
  • 67
  • 8
  • 2
    Did you try this code? – Steve Mar 01 '14 at 09:07
  • Well it doesn't really work, the list version of the variable does not update unfortunately. Therefore the updatebuff never gets called. – Ken Rea Mar 01 '14 at 09:11
  • 2
    @ZombeeKenRea Oh, duh. `Vector2` is a *structure type*. You can create your own *reference type*, and then it will work the current code as desired. Alternatively, consider making a Vector2 as a *property* of a custom "buff" type. – user2864740 Mar 01 '14 at 09:14
  • AHHH Structs aren't referenced. – Ken Rea Mar 01 '14 at 09:15
  • Not sure what you mean by make it a property. Elaborate if you have time :) – Ken Rea Mar 01 '14 at 09:16
  • 1
    `class ShipBuff { Vector2 Force { get; set; } .. }`, if there are any particular methods on Vector2 that are handy to keep about or if it fits the data well. But ShipBuff (a class) is what is passed around and modified - the Force/Vector2 property should then be *reassigned* (instead of just mutating the Vector2 object). – user2864740 Mar 01 '14 at 09:16
  • I only felt magnitude was useful here, which is just adding the two together. So I'll probably just make my own custom buff class. Thanks a bunch. – Ken Rea Mar 01 '14 at 09:18
  • You should make that an answer user2864740 so I can make the best answer. – Ken Rea Mar 01 '14 at 09:30
  • 2
    Instead of the "SOLVED" part in the title you should post your solution as an answer and mark it as accepted. Then edit your title, please. – Ondrej Janacek Mar 01 '14 at 09:35

2 Answers2

0

"Foreach" variables are readonly, which means you can't pass them by reference.

dnxit
  • 7,118
  • 2
  • 30
  • 34
  • check this as well http://stackoverflow.com/questions/1538301/c-sharp-does-foreach-iterate-by-reference – dnxit Mar 01 '14 at 09:18
  • It worked as intended once I wasn't using Vector2 as it was a struct. In C# structs aren't passed by reference like objects/classes. I created my own class and it works just fine. – Ken Rea Mar 01 '14 at 09:28
  • @Dirk Foreach is designed to visit each item in a collection exactly once, and does not use an explicit "loop index"; if you want more control over the loop and have a loop index, use for. EDIT: You can change the items in the collection being iterated on inside a foreach loop. For example: foreach(Chair ch in mychairs) { ch.PaintColour = Colour.Green; //this alters the chair object *in* the collection. } You cannot, however, add or remove items to/from the collection. – dnxit Mar 01 '14 at 10:07
0

Fixed using custom class.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Buff : MonoBehaviour{


    // (Buff Amount, Timer)
    public BuffInfo thrust = new BuffInfo(1,0);
    public BuffInfo mass = new BuffInfo(1,0);
    public BuffInfo shield = new BuffInfo(1,0);
    public BuffInfo armor = new BuffInfo(1,0);
    public BuffInfo structure = new BuffInfo(1,0);
    public BuffInfo regen = new BuffInfo(1,0);
    public BuffInfo explosionRadius = new BuffInfo(1,0);
    public BuffInfo damage = new BuffInfo(1,0);
    public BuffInfo disable = new BuffInfo(0,0);

    public List<BuffInfo> buffs = new List<BuffInfo>();

    float deltaUpdates = 0;
    // Use this for initialization
    void Start () {
        buffs.Add(thrust);
        buffs.Add(mass);
        buffs.Add(shield);
        buffs.Add(armor);
        buffs.Add(structure);
        buffs.Add(regen);
        buffs.Add(explosionRadius);
        buffs.Add(damage);
        buffs.Add(disable);
        regen.power = 5;
    }

    // Update is called once per frame
    void Update () {

        // Makes Regen variable change.


        if ((deltaUpdates += Time.deltaTime) < 1)
            return;

        // Can I make it affect what is in the List of Vector 2's?
        foreach (BuffInfo buff in buffs)
        {
            if (buff.magnitude() > 1)
                UpdateBuff(buff);
        }

        deltaUpdates = 0;
    }

    // Therefore this will be called. Then the variable outside can also be updated?
    void UpdateBuff(BuffInfo inBuff)
    {
        Debug.Log("Updating Buff: " + inBuff.ToString());
        if ((inBuff.power -= deltaUpdates) <= 0)
        {
            inBuff.power = 1;
            inBuff.time = 0;
        }
    }
}
Ken Rea
  • 67
  • 8