Jump to content

[1.7.10][SOLVED]For and for each in model initialization


Recommended Posts


I already posted this issue in a previous thread, but nobody answered, and then I switched to 1.7.10 to see if it changes anything - it didn't. I can't find any difference in how those two loops work, so potentially both should run fine, but it appears like for each doesn't. Here's what I'm doing (this is in a ModelBase extending class):

-First I create a ModelRenderer array:

ModelRenderer[] screens = {null, null, null, null, null, null, null};

-Then, in the constructor, I fill the array with models. There are two, theoretically identical ways to do this. First one is a for loop:

int textOffsetY = 66;
for (int i = 0; i < 7; i++) {
screens[i] = new ModelRenderer(this, 0, textOffsetY);
screens[i].addBox(0F, 0F, 0F, 28, 20, 0);
screens[i].setRotationPoint(-14F, 20F, -4F);
screens[i].setTextureSize(64, 256);
screens[i].mirror = true;
setRotation(screens[i], 0F, 0F, 0F);
textOffsetY += 21;

-The second way is a for each loop:

int textOffsetY = 66;
int i = 0;
for (ModelRenderer screen : screens) {
screen = new ModelRenderer(this, 0, textOffsetY);
screen.addBox(0F, 0F, 0F, 28, 20, 0);
screen.setRotationPoint(-14F, 20F, -4F);
screen.setTextureSize(64, 256);
screen.mirror = true;
setRotation(screen, 0F, 0F, 0F);
textOffsetY += 21;

They both work, because I've checked the console and both output the i variable, from 0 to 6.

-After this I try to render one of the array elements in a TileEntitySpecialRenderer:


Here's when it gets really wierd. If I've intialized the models with a for loop, everything works fine. However, if I've used the for each loop, I get this: https://gist.github.com/anonymous/4f85c79b11749d821e72

I am either having a complete brainfart here and don't know how Java works, or there's something seriously wrong with Forge/FML/Minecraft/OpenGL. Any help would be appreciated

Link to comment
Share on other sites

In your For Each loop, you are never setting the newly created instance into the array -- so yes, it will be null.  While you _are_ iterating over the null instances that exist, changing the reference of 'screen' while in the loop does not change the assignment for that reference in the array.  Hopefully someone else can explain this in a more understandable manner.


add something like:


screens [ i ] = screen;


to the bottom of your for-each loop ( BEFORE you increment i ), and you should be good.



Not MC related, basic Java / reference stuff.

Link to comment
Share on other sites

Ok, so when I use a for each loop, is the screen variable in it not a reference to an object in the screens array?



Nevermind, I finally got this. It would work if I used an array of Objects from start, and not an array of null pointers.

Link to comment
Share on other sites

I'm not sure but I suspect it is because when you use an array you are pointing to an explicit memory location which doesnt care that it was previously null.  But I think with for-each the screens that are passed are null pointers which cause trouble to the iteration.


I've run into this before with Java.  For example if you do this:

Block myBlock - null;
ItemSeed mySeed = new ItemSeed(myBlock);
myBlock = new BlockPotato;


The ItemSeed will never get updated with the potato because it was originally told to point to null.


Not sure if that is your problem, but it seems similar.  If you init to null it isn't clear that there is any pointer to use in certain situations later.  I think the explicit array index call in your for loop ensures you're pointing to allocated memory, but I think the screen iterator in the for-each just keeps pointing to null (i.e. not allocated space).


Or maybe I'm totally wrong...

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

Important Information

By using this site, you agree to our Terms of Use.