I have written a few methods to spawn equipment that you find in certain places around the map in my game. It may not be the best way to do it but it works - sometimes. Sometimes I just get a null pointer exception on a spatial and I cant figure out why. Here is how I assign a model (or a box, in place of a model) to an array of spatials:
[java]gear[values[0]] = assetManager.loadModel("Models/Gear/Crossbow/Crossbow.j3o");
gear[values[1]] = assetManager.loadModel("Models/Gear/Crossbow/Crossbow.j3o");
gear[values[2]] = assetManager.loadModel("Models/Gear/Knife/Knife.j3o");
gear[values[3]] = assetManager.loadModel("Models/Gear/Knife/Knife.j3o");
gear[values[4]] = makeCube();
gear[values[5]] = makeCube();
gear[values[6]] = makeCube();
gear[values[7]] = makeCube();
gear[values[8]] = makeCube();
gear[values[9]] = makeCube();[/java]
And here is how I utilize this spatial array:
[java]private void Spawn(Vector3f[] points){
Spatial[] gear = makeGear(points);
for(int i=0;i<=9;i++){
gear.setLocalTranslation(points);
rootNode.attachChild(gear);
}
}[/java]
And i get a nullpointer sometimes on
gear.setLocalTranslation(points);
I know you might need more information but anyways…
gear[values] → where values is unknown integer for me(from unknown - unknown) where i is integer(from 0 - 9)
and
gear → where i is integer(from 0 - 9)
i see difference here …
I know you might need more information but anyways…
exactly. need more information
yes and a stack trace, what is “i” when the NPE occurs?
Right, values is
int[] values = new int[10]
and is filled with random values from one - 10, so the number 1 will only occur once, the number 2 will only occur once.
e.g 2,7,9,1,6,3,5,8,4,0
This is to allow the gear to be spawned in a different ‘loot point’ every time. I can show you how I make values[] in this way but you might not like it.
[java]private Spatial[] makeGear(Vector3f[] points){
Spatial[] gear = new Spatial[10];
int[] values = new int[10];
values[0] = rand(10);
values[1] = rand(10);
while(values[1] == values[0]){
values[1] = rand(10);
}
values[2] = rand(10);
while(values[2] == values[0] || values[2] == values[1]){
values[2] = rand(10);
}
values[3] = rand(10);
while(values[3] == values[0] || values[3] == values[1] || values[3] == values[2]){
values[3] = rand(10);
}
values[4] = rand(10);
while(values[4] == values[0] || values[4] == values[1] || values[4] == values[2] || values[4] == values[3]){
values[4] = rand(10);
}
values[5] = rand(10);
while(values[5] == values[0] || values[5] == values[1] || values[5] == values[2] || values[5] == values[3] || values[5] == values[4]){
values[5] = rand(10);
}
values[6] = rand(10);
while(values[6] == values[0] || values[6] == values[1] || values[6] == values[2] || values[6] == values[3] || values[6] == values[4] || values[6] == values[5]){
values[6] = rand(10);
}
values[7] = rand(10);
while(values[7] == values[0] || values[7] == values[1] || values[7] == values[2] || values[7] == values[3] || values[7] == values[4] || values[5] == values[6] || values[7] == values[6]){
values[7] = rand(10);
}
values[8] = rand(10);
while(values[8] == values[0] || values[8] == values[1] || values[8] == values[2] || values[8] == values[3] || values[8] == values[4] || values[8] == values[5] || values[8] == values[6] || values[8] == values[7]){
values[8] = rand(10);
}
values[9] = rand(10);
while(values[9] == values[0] || values[9] == values[1] || values[9] == values[2] || values[9] == values[3] || values[9] == values[4] || values[9] == values[5] || values[9] == values[6] || values[9] == values[7] || values[9] == values[8]){
values[9] = rand(10);
}
gear[values[0]] = assetManager.loadModel(“Models/Loot/Crossbow/Crossbow.j3o”);
gear[values[1]] = assetManager.loadModel(“Models/Loot/Crossbow/Crossbow.j3o”);
gear[values[2]] = assetManager.loadModel(“Models/Loot/Knife/Knife.j3o”);
gear[values[3]] = assetManager.loadModel(“Models/Loot/Knife/Knife.j3o”);
gear[values[4]] = makeCube();
gear[values[5]] = makeCube();
gear[values[6]] = makeCube();
gear[values[7]] = makeCube();
gear[values[8]] = makeCube();
gear[values[9]] = makeCube();
return gear;
}[/java]
Rand method just calls Random.nextInt, but I can put min and max caps on it
omg, do you want to write in Asembler
[java]Collections.shuffle(list, new Random(seed));[/java]
if im not wrong, it should do what you need.
just try to write in “higher” level. that was java created to
WTF I did all that for nothing??
I found the problem anyways,
while(values[7] == values[0] || values[7] == values[1] || values[7] == values[2] || values[7] == values[3] || values[7] == values[4] || values[5] == values[6] || values[7] == values[6]){
values[7] = rand(10);
It was that line, thanks though ill try this
also never base a random algorithm on using while, the performance will not be consistent and you could theoretically be there forever, especially ones near the bottom.
Edit: and you will also make mistakes like that and your procedure does not scale well, try it with 1000 items, and you’ll see what i mean
there are many examples like here:
Collections: shuffle(List < ? > list,Random rnd) : Collections « java.util « Java by API
Collections: swap(List<?> list, int i, int j) : Collections « java.util « Java by API
Collections: reverseOrder() : Collections « java.util « Java by API
or even Arrays can do something(sorting here, but randomize maybe too, idk):
http://www.leepoint.net/notes-java/data/arrays/70sorting.html
the only you need is to use Java Collections.
@wezrule said:
also never base a random algorithm on using while, the performance will not be consistent and you could theoretically be there forever, especially ones near the bottom.
Edit: and you will also make mistakes like that ;) and your procedure does not scale well, try it with 1000 items, and you'll see what i mean
Hell no 10 of them took me long enough.
I'll have a look thanks oxplay
But I can’t use gear[myList[0]] = assetmanager.blahblahblah… can I?
you should make Collection, for example ArrayList, and put your Spatials there.
after that you can make random swap / shuffle on it.
but at all you dont need use Collections, you can use Array and use:
[java]Arrays.sort(employee, new AgeComparator());[/java]
look example:
http://www.javadeveloper.co.in/java-example/java-comparator-example.html
where you can decide on elements positions (so you can even here random their positions)
this comparator can be used on Arrays and Collections like ArrayList / etc
Yay, made my own ArrayList up and named it listy <3 you have made me very happy, thank you!
Comparator: not sure I’m ready for that yet… I can do without it anyway
@javagame said:
WTF I did all that for nothing??
Yep. If you find yourself doing that sort of code then you always need to step back and think about whether there is a smarter way to do it - or whether someone else has already solved the problem. Especially considering that things like shuffling a list/array are a very common requirement.