0

So I'm trying to load actor models, and eventually this class is going to get pretty big. So I really don't want to have to keep track of writing them all to my entity array(line 40), is there a more efficient way of storing my actor models. using indexing?

My code looks like this..

12.    public static Entity[] callActors(Loader loader) {
13.
14.     //Earth Entity Actor        
15.     model = OBJLoader.loadObjModel("moon", loader);
16.     texture = new ModelTexture(loader.loadTexture("MoonMap1024x1024"));
17.     staticModel = new TexturedModel(model,texture);
18.     texture = staticModel.getTexture();
19.     texture.setShineDamper(10);
20.     texture.setReflectivity(0);
21.     //Assigned an Entity Actor
22.     Entity entity1 = new Entity(staticModel, new Vector3f(-1.40f,1,-6.0f),0,0,0,1);     
23.     
...
39.
40.      Entity[] entityArray = {entity1,entity2,entity3};
41.     
42.     return entityArray;
43. }

Is there a way to use indexing like this at line 22 Entity[] entityArray[1] = new Entity(staticModel, new Vector3f(-1.40f,1,-6.0f),0,0,0,1); instead of writing all of the actors to the entity array all at once (line 40)?

1
  • Entity[] entityArray = {entity1,entity2,entity3}; is effectively the same as Entity[] entityArray = new Entity[3]; entityArray[0] = new Entity(staticModel, new ...etc); entityArray[1] = new ...etc, if that is what you are asking. Commented Oct 25, 2017 at 4:59

3 Answers 3

1

You can declare array of type Entity with specific size and add elements in it using indexing. You can also maintain index variable.

 Entity[] arr = new Entity[10];
 arr [0]= new Entity(staticModel, new Vector3f(-1.40f,1,-6.0f),0,0,0,1);

OR

Entity[] arr = new Entity[10];
int i=0;
 arr [i]= new Entity(staticModel, new Vector3f(-1.40f,1,-6.0f),0,0,0,1);
 i++;
 arr [i]= new Entity(staticModel, new Vector3f(-1.40f,1,-6.0f),0,0,0,1);
 i++;
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much! Also, do I have to declare this knowing the final size of my Array?
@TheSpiceHoarder, If you use array, you need to know the final size. On the other hand, if the size varies, you may try ArrayList instead.
@TheSpiceHoarder, to use array we have to declare it's size. If size is unpredictable the we can use ArrayList instead.
0

Not sure I got you right, but if you want easy way to init the array you can do it like this:

Entity[] arr = new Entity[] {entity1, entity2, enttiy3};

If you want to set specific indexes you can use hashMap (also get operations will be faster), do it like this:

HashMap<Integer, Entitiy> Entity = new HashMap<Integer, Entitiy>();

Useful links:

HashMap in Java

Arrays in java

Comments

0

Is there a way to use indexing like this at line 22
Entity[] entityArray[1] = new Entity(staticModel, new Vector3f(-1.40f,1,-6.0f),0,0,0,1); instead of writing all of the actors to the entity array all at once (line 40)?

You need not use the array symbol ([ ]) while trying to access one Entity. Since entityArray[1] means one Entity (stored at the position 1 in array entityArray), you can simply write:

Entity[] entityArray = new Entity[ARRAY_SIZE];
entityArray[1] = new Entity(staticModel, new Vector3f(-1.40f,1,-6.0f),0,0,0,1);

If you want, however, to construct the Data Structure after you have initialized your Entities, you can use the same code you've written.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.