Jump to content

Recommended Posts

Posted

Hello.

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;
System.out.println(i);
}

-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;
System.out.println(i);
i++;
}

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:

modelArcadeGameTop.screens[0].render(0.0625F);

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

Posted

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.

Posted

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?

 

EDIT:

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

Posted

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/

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.

Guest
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.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • My name is Richie Leo, and I’m sharing this message with a heart full of gratitude and hope. Several months ago, I was a victim of a devastating online scam that cost me a staggering $873,463. I was devastated, confused, and had nearly given up on ever recovering my money — until I came across Wizard George Cyber Service. Through their exceptional cyber recovery expertise and deep investigative skills, Wizard George and his team were able to trace, track, and recover the full amount that was stolen from me. Their professionalism, speed, and transparency truly amazed me. If you’re reading this and you’ve been scammed — whether it’s crypto, investment fraud, or any kind of online theft — don’t give up. I strongly recommend reaching out to Wizard George Cyber Service. 📧 Em: wizardgeorgecyberservice(AT) g m a l L. C o M
    • Alright, here is the log file https://mclo.gs/5eCwafV
    • Please read the FAQ (https://forums.minecraftforge.net/topic/125488-rules-and-frequently-asked-questions-faq/) and post log files as described there, using a site such as https://mclo.gs/ and post the link here.  
    • I tried updating the mods in my modpack which caused incompatibilities so i have tried to revert them back to their older versions i was using before. In the logs it doesnt show me any clear incompatibilities except for tfmg & entity texture features, but when i try to remove those it still doesn't work. I have tried removing the forge-client.toml file which was a suggestion i found on  a few other posts. This is the log file i get. [inline log removed] Any help would be appreciated. Thanks in advance
    • I don't use KubeJS, never even heard of it. But after doing what "Ugdhar" suggested earlier in this post with the "config/Mekanism/generator-storage.toml", I tried going into an individual save's serverconfig folder, and just deleting everything except the parcool folder (I have that mod installed.) Then, a bit of loading and temporary freezing later, seems to have worked. Even when quitting to menu and loading back in, or also when quitting to menu, exiting to desktop, and re-launching MC, choose a save and loading it.
  • Topics

×
×
  • Create New...

Important Information

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