[java] scope help please
I started to throw a pathfinding/attackmove demo together yesterday but I couldn''t get access to variables in other classes. I want public Square[][] terrain = new Square[10][10]; to be availible everywhere. If I put it into a particular class only the methods in that class work. I made everything public but that doesn''t seem to fix it. Note there are probably some bad decisions in how the thing is set up, but I just want it all to compile first.
//first file
public class TrivialApplication
{
World w = new World();
public static void main(String args[])
{
System.out.println( "Hello World!" );
}
}
//second file
public class Square //will not compile
{
public int x;
public int y;
public int cost;
//public Square[][] terrain = new Square[20][20];
public Square(int x_, int y_, int cost_)
{
x=x_;
y=y_;
cost=cost_;
}
public Square() /*without this constructor Path would not compile*/
{
x=-1;
y=-1;
cost=99;
}
/* here is where I need to access terrain. I tried a few differnt ways, such as the w.terrain version, a TrivialApplication.w.terrain, and a few other things, none work unless I just put terrain itself in this class.
public Square[] adjacent()
{
Square[] adj = new Square[6];
adj[0] = w.terrain[x-1][y-1];
adj[1] = terrain[x][y-1];
adj[2] = terrain[x-1][y];
adj[3] = terrain[x+1][y];
adj[4] = terrain[x][y+1];
adj[5] = terrain[x+1][y+1];
return adj;
}
}
//third file
public class World
{
public Square[][] terrain = new Square[10][10];
public World()
{
}
}
//fourth file
public class Path
{
int totalCost;
public Square thisSquare;
public Path nextPath;
public Path(Square s, Path p)
{
thisSquare = s;
nextPath = p;
}
}
Okay you aren''t making your Square[][] static, that means that everytime you make a new object of the class that has Square[][], you get a new Square[][] tied to that object. You need to reference the 2D array as object.Square[][]. If you made it static, it would bind to the class, allowing you to reference it as ClassName.Square[][], or Square[][] if it''s public.
I hope that this was your problem and that this allows it to compile and run.
I hope that this was your problem and that this allows it to compile and run.
Thanks a lot. Making it static fixed it. I tried World.terrain and it worked. What has me confused is that w.terrain didn''t work earlier when it was nonstatic.
If w.terrain didn''t work and terrain was public, w probably wasn''t. You get used to it after a while, and you can spot these things as you''re writing them.
I was wondering what the general design for creating the classes for a world are.
My first (failed) attempt had a base class with terrain and unit info in it. Then came a terrainmanager and unit manager. Finally i had the applet class.
did i space things out too much? I had the same prob with the static terrain level too. My last resort solution was to dump everything into a single file.
PEACE
GAZZ
My first (failed) attempt had a base class with terrain and unit info in it. Then came a terrainmanager and unit manager. Finally i had the applet class.
did i space things out too much? I had the same prob with the static terrain level too. My last resort solution was to dump everything into a single file.
PEACE
GAZZ
Also, how much performance is lost using a 2d array? When i was playing with C i could see a small, but detectable performance drop using a 2d array over a plain array. Does that same thing happen in Java??
Me and my dumb questions.
PEACE
GAZZ
Me and my dumb questions.
PEACE
GAZZ
Jim_Ross:
thanks, that looks like the problem.
eqs:
I''m putting an x and y coordinate in each unit, try that. That way your units always know where they are. You might also want to keep some unit info in the terrain (so you can find out which units are in an area) too.
thanks, that looks like the problem.
eqs:
I''m putting an x and y coordinate in each unit, try that. That way your units always know where they are. You might also want to keep some unit info in the terrain (so you can find out which units are in an area) too.
quote: Original post by eqs
Also, how much performance is lost using a 2d array? When i was playing with C i could see a small, but detectable performance drop using a 2d array over a plain array. Does that same thing happen in Java??
You lose performance in C using a dynamic 2D array because there is a double derefernce going on. In a static 2D array there''s a few more arithmetic ops and a derefernce.
In Java, how much performance is lost depends on the JVM. I believe Sunsoft''s JVM performs a triple dereference to access a member of an array. And then your 2D array is just an array of arrays so you need to triple dereference the second array that actually holds your data in order to get at the information you need. The MS JVM only perfroms a double derefernce per array. Either way you''ve got lots of memory dereferncing going on. Which is kind of slow.
On the other hand you can use a 1D array and use arithmetic operations to treat it as a 2D array, so you only lose the triple/double dereference and an add/multiply pair. So it will be slightly faster. But would it be worth the loss of code clarity?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement