0

I am writing a pathfinding script in Unity, and I have been trying to figure out IEnumerables, and I feel I am not wording my internet searches correct, because it is not at all what I want.

I have 3 scripts, 1 find nodes and it adds to the OpenNodes list which is it's own script with constructor. I have An IEnumerable and it is still saying it can't access, or it needs to be static if just public. So my codes as follows:

public class OpenNodes: IEnumerator, IEnumerable {
public static Vector3 node;
public static int g;

public static IEnumerator GetEnumerator()
{
    return (IEnumerator)this;
}

public OpenNodes(Vector3 newNode, int newGscore)
{
    node = newNode;
    g = newGscore;

}

so what I am trying to do in a different "FinderScript" is along these lines:

foreach(Vector3 node in OpenNodes)

I have been trying my hardest to figure it out, but I have never worked in constructor lists like this. This is new to me, so any help, would be SO greatly appreciated.. Thanks

EDIT: This for each needs to take node in as a vector3 as it is using .x,.y, and .z values for comparison and changes..

John Saunders
  • 160,644
  • 26
  • 247
  • 397
MrRoss
  • 35
  • 10
  • 1
    @JeroenVannevel Just because something is basic or you consider trivial does not mean it's not a good question for Stack Overflow. This question is not **too broad** either, because it can be answered with a short explanation of how those interfaces should be implemented. – mason Mar 13 '15 at 00:28
  • 1
    I'm sorry but how is this off-topic? And that link does not explain anything that I am confused with... Basic it might be, but off-topic it is not.. – MrRoss Mar 13 '15 at 00:28
  • You should use the generic version of IEnumerable, and the implementation is show in [How do I implement IEnumerable](http://stackoverflow.com/questions/11296810/how-do-i-implement-ienumerablet) – mason Mar 13 '15 at 00:32
  • My fault, I didn't realize you were trying to implement the interfaces but thought you were stuck on how properties and collections work. It should be closed per the duplicate above though. – Jeroen Vannevel Mar 13 '15 at 00:38
  • You'd have a better start by extending a particular typed collection, e.g. List. Then you do not need to implement iteration yourself – codemonkeh Mar 13 '15 at 00:39
  • Honestly that duplicate isn't completely helping me.. It is helpful to get me closer, but per my exact situation, it doesn't explain how I am to implement it... – MrRoss Mar 13 '15 at 00:40
  • What is the relationship of newGscore to the node? is it one g score per node, or per all nodes? – codemonkeh Mar 13 '15 at 00:42
  • @codemonkeh Perhaps inheriting from List in this situation [isn't a good idea](http://stackoverflow.com/questions/21692193/why-not-inherit-from-listt). – mason Mar 13 '15 at 00:43
  • newGscore is just a parameter sent in for each node. One score per node.. – MrRoss Mar 13 '15 at 00:43
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Mar 13 '15 at 01:07

2 Answers2

0

As you need to store a score per node you could simply use a Dictionary<Vector3, int> to store the values.

e.g.

var nodes = new Dictionary<Vector3, int>();
nodes.add(node, gScore);

And then either access them directly

nodes[node] = ...

Or loop through them

foreach (var node in nodes.Keys) {
   ...
}

Alternatively, perhaps you can create an object that maps the score and the node together (or simply use a Pair and then just use a list?

var nodes = new List<Vector3>();
var map = new Pair(node, gScore);
nodes.add(map);

Then loop through them as per usual:

foreach (var node in nodes) {
   ...
}

Not sure if i have understood you correctly, and i haven't run this code. HTH.

codemonkeh
  • 2,054
  • 1
  • 20
  • 36
  • I understood that perfectly!! Thanks so much, but what if down the road I am going to pass in more than 2 values? If I remember correct, dictionaries store 2 values?? – MrRoss Mar 13 '15 at 00:52
  • Although you could use a `Tuple` for multiple values, your best option would be to create a custom object to store the properties :) – codemonkeh Mar 13 '15 at 00:54
  • So then add the object to the list, but then it comes into play that I would need to extract those values out of the custom object through a foreach loop. I though it would be more simple to access node directly in a foreach as the way in my question than creating a custom object or such... I am really starting to doubt this is the case, as is seeming to become more and more of a problem.. I may run with a dictionary as of now, and see if it at least solves my issue – MrRoss Mar 13 '15 at 00:58
  • It depends on how you need to access the node. If you're happy to loop through them then a `List` would be your best bet. If would be no harder to access the node, just call the Node property on the item you're looping through. If you need fast access to a particular node, a `Dictionary` is your best bet. – codemonkeh Mar 13 '15 at 01:04
0

The problem I was facing was actually more simple that I realized. Below is the code of my constructor list:

public class OpenNodes  {
public Vector3 node;
public int g;



public OpenNodes(Vector3 newNode, int newGscore)
{
    node = newNode;
    g = newGscore;

}


}

The code for the foreach is as follow:

foreach(OpenNodes list in OpenNode)

to populate the list looks like:

OpenNode.Add(new OpenNodes(nodeLocation, 0 ));

to remove an item looks like:

OpenNode.RemoveAll(OpenNodes => nodeLocation == OpenNodes.node && 0 == OpenNodes.g);

and to access my node it is simply:

list.node.x
list.node
list.node.z

ect.

This is iterating through each item in the list as list, and then you call upon each variable after that. I hope this code will help someone in the future!

MrRoss
  • 35
  • 10