Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

I have noticed in a lot of the mods i have looked at they seem to use this.field/method a lot e.g.

public void markDirty ()
    {
        buildTool(toolName);
        if (this.worldObj != null)
        {
            this.blockMetadata = this.worldObj.getBlockMetadata(this.xCoord, this.yCoord, this.zCoord);
            this.worldObj.markTileEntityChunkModified(this.xCoord, this.yCoord, this.zCoord, this);
        }
    }

(Example is from tconstruct)

I understand how it works but is it actually necessary? I would like to know because i dont use it almost at all in my mod except in constructors.

I am the author of Draconic Evolution

good practice in case a parameter name conflicts with an attribute (in which case it'd only set the variable in the scope of the function itself). if the variable names are different then it's not technically necessary but it still helps with remembering which scope you're dealing with (class or function)

Hi

 

Largely a style choice, I don't think there's any consensus.  Personally I think it adds unnecessary clutter, and scope hiding is asking for trouble, so I only add it if my intentions are unusual and I want to make them obvious.  Others find it more clear to use this all the time.  Still others give their member variables a prefix eg m_blockMetadata.  If you're consistent I reckon it doesn't matter.

 

-TGG

 

 

Yeah I don't like it either style-wise. 

 

There are three cases I know of where you DO need the keyword this:

 

1. To be explicit that you're referring to an object field in the case where there is some other local or super variable with same name.  Like if you had an int field in your object called counter and then you had a for loop with its own index also called counter and you needed to access the object's counter field from within that loop.

 

2. To refer to the current object as a parameter to a method you're calling.  For example, if you're coding within a custom entity and adding ai tasks you would have:

tasks.addTask(0, new EntityAISwimming(this));

 

3. If you're calling a constructor of your object from within it.

    public ItemGoldenEgg() 
    {
	this("Golden Goose", 0xF5E16F, 0xF5F56F);
}
    
    public ItemGoldenEgg(String parEntityToSpawnName, int parPrimaryColor, int parSecondaryColor)
    {
        this.maxStackSize = 16; // same as regular egg
        this.setCreativeTab(CreativeTabs.tabMisc);
        colorBase = parPrimaryColor;
        colorSpots = parSecondaryColor;
    }

 

The general use of "this" all over the place is, as TheGreyGhost notes, arguable.  Some people think it makes it more readable, some like me and TGG think it makes it less readable (due to clutter).  But shouldn't create any issue with your code either way.

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

I don't like it either, but sometimes if I have trouble remembering the name of a variable or method "this." brings up the options and helps me out.  I go back and delete the 'this' at that point.  However, its not really hurting anything.  I just think it looks ugly.

Long time Bukkit & Forge Programmer

Happy to try and help

In reality, 99%of the time when you see this in mods, its copy/pasta from vanilla code and people not knowing better.

But people are right there are cases where its NEEDED (Local variables shading class variables)

It's largely a style choice, however most 'modders' learn from decompiled code {horrible btw} which uses excessive 'this' to guarantee there are no shading issues.

I do Forge for free, however the servers to run it arn't free, so anything is appreciated.
Consider supporting the team on Patreon

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.