- Home /
AI Code Errors
I'm super noob when it comes to classes and stuff, so in-depth explanations may be necessary. I have an AI code that I haven't tested yet, I just wrote it. What I am trying to do is get a simple A* pathfinding system worked out. I'm not sure why I'm getting errors but it's probably something simple. I have a class which handles the f score, heuristics, and what not. First of all I'm getting errors saying that the class variables, when being used in another script, are not members of the gameobject class??? Secondly I'm getting errors when I try to assign the vars of a class (in another script) that the vars are unknown identifiers, again I have never been successful with code like this. Lastly I get an errors saying that I need an instance of my navigation script for it to work, I have an idea of what this might mean but would rather just get answers instead of messing up some more if I'm wrong.
So I would like help with fixing these three problems and it would be great if someone could check my AI code to make sure I'm doing everything right, what I'm missing, what needs to go, and what needs to be fixed. Thanks in advance, I posted the scripts below.
Player Code (This is where the navigation script is being called [hopefully I'm using the correct terminology here]):
import System.Collections;
import System.Collections.Generic;
var desiredPosition : Vector3;
private var start : NavCell;
private var destination : NavCell;
private var cells : ArrayList;
function Update(){
var points : NavCell = Navigation.CalcPath(start, destination, cells);
}
function PopulateCells () {
var objects : GameObject[] = GameObject.FindGameObjectsWithTag("NavigableCells");
for(var i : int = 0; i < objects.Length; i++){
var cell = new NavCell();
cell.originalGO = object;
cell.position = originalGO.transform.position;
cell.neighbors = originalGO(Cell).nearCells;
cells.Add (cell);
if(Vector3.Distance(transform.postion, cell.position) < 1.5){
start = cell;
}else
if(Vector3.Distance(transform.position, desiredPosition) < 1.5){
end = cell;
}
}
}
NavCell Class (This is the NavCell class file):
public class NavCell {
var position: Vector3;
var f: int;
var h : int;
var g : int;
var originalGO: GameObject;
var neighbors : GameObject[];
var accessible : boolean;
var pathParent : NavCell;
public function SetPathParent(parent : NavCell, getG : int){
pathParent = parent;
g += getG;
f = g + h;
}
public function GetHueristic (posA : Vector3, posB : Vector3) {
h = Mathf.RoundToInt(Vector3.Distance(posA, posB));
}
}
Navigation Script (where stuff happens):
import System.Collections;
import System.Collections.Generic;
public function CalcPath(from : NavCell, to : NavCell, cells : ArrayList) : NavCell[]
{
for (var n : NavCell in cells) n.SetPathParent(null, 0);
var fp : NavCell = from;
var openList : ArrayList = new ArrayList();
var closeList : ArrayList = new ArrayList();
openList.Add(fp);
while (openList.Count > 0)
{
// find one with lowest F score
fp = null;
for (var cl : NavCell in openList)
{
if (fp == null) fp = cl;
else if (cl.f < fp.f) fp = cl;
}
// drop it from open list and add to close list
closeList.Add(fp);
openList.Remove(fp);
if (fp == to) break; // reached destination, break
// find neighbours of this point
for (var i : int = 0; i < fp.neighbors.Length; i++)
{
// in close set
if (closeList.Contains(fp.neighbors[i])) continue;
if (openList.Contains(fp.neighbors[i]))
{ // open list contains the neighbour
// check if G score to the neighbour will be lower if followed from this point
var destG : int = fp.g;
if (destG < fp.neighbors[i].g)
{
destG = Mathf.RoundToInt(Vector3.Distance(fp.neighbors[i].position, to.position));
fp.neighbors[i].SetPathParent(fp, destG);
}
}
else
{ // add neighbour to open list
var distConst = Mathf.RoundToInt(Vector3.Distance(fp.neighbors[i].position, to.position));
fp.neighbors[i].SetPathParent(fp, fp.g + distConst);
openList.Add(fp.neighbors[i]);
}
}
// start at dest and build path
var path : ArrayList = new ArrayList();
fp = to;
while (fp.pathParent != null)
{
path.Add(fp);
fp = fp.pathParent;
}
if (path.Count > 0)
{ // the path is calculated in reverse, swap it around
// and then return the array of the elements
path.Reverse();
return path.ToArray(typeof(NavCell));
}
return null;
}
}
Answer by Loius · Nov 09, 2012 at 09:29 PM
--First of all I'm getting errors saying that the class variables, when being used in another script, are not members of the gameobject class
a;slkfhasklfhas. That's because they're not. If you have a "GameObject X", then it can access only GameObject members. You need to have a "MyClass X" to access members of MyClass. Without an indication of what line is giving the error (and you get the line number with the error, so no reason not to include that when asking for help...) I can only recommend that you check your return types to be sure you're actually given an object of the right type.
Unknown identifier is similar. It means you're trying to use something which hasn't been declared, like doing:
class Y { function X() { z+= 2; } }
One particular part where I get that error is line 42 of the navigation code.
Your answer
