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

    • Ok, I updated to the latest version and now the models are visible, the problem now is that the glowing eyes are not rendered nor any texture I render there when using shaders, even using the default Minecraft eyes RenderType, I use entityTranslucent and entityCutout, but it still won't render. Something I noticed when using shaders is that a texture, instead of appearing at the world position, would appear somewhere on the screen, following a curved path, it was strange, I haven't been able to reproduce it again. I thought it could be that since I render the texture in the AFTER ENTITIES stage which is posted after the batches used for entity rendering are finished, maybe that was the reason why the render types were not being drawn correctly, so I tried injecting code before finishing the batches but it still didn't work, plus the model was invisible when using shaders, there was a bug where if I look at the model from above it is visible but if I look at it from below it is invisible. So in summary, models are now visible but glowing eyes and textures are not rendered, that hasn't changed.
    • https://pastebin.com/99FA0zvK Attempting to run genIntellijRuns, this task fails with pasted error. IntelliJ IDEA 2024.3 (Community Edition) eclipse_adoptium-21-x86_64-os_x.2/jdk-21.0.6+7 Steps followed: Step 1: Open your command-line and browse to the folder where you extracted the zip file. Step 2: You're left with a choice. If you prefer to use Eclipse: 1. Run the following command: `./gradlew genEclipseRuns` 2. Open Eclipse, Import > Existing Gradle Project > Select Folder    or run `gradlew eclipse` to generate the project. If you prefer to use IntelliJ: 1. Open IDEA, and import project. 2. Select your build.gradle file and have it import. 3. Run the following command: `./gradlew genIntellijRuns` 4. Refresh the Gradle Project in IDEA if required.
    • By the way, I don't think I made this as clear as possible. When the server would not start, I copied the world from the server and am now trying to load it in from Single Player. When I said I removed the mods, I meant I did so from the client, as the server was out of the picture, as I was loading the world locally from single player. The log I provided before was from the server, but same thing happens on Single player. I did disable all of those mods in the screenshot, and the world would not load past 14%. Adding them back i'm loading up to 80% and then nothing. This is the full startup log of trying to join it from singleplayer: https://mclo.gs/Or8U7N6 And here is the debug.log from BEFORE when I was trying on the server. https://mclo.gs/2uTQucK
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

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