Jump to content

[1.7.10] How does the instance annotation work?


Poseidon5001

Recommended Posts

@Instance("ModID")

public static ClassName instance;

How does this work? I understand it makes an Instance from which MinecraftForge can work from, but how does it achieve this?

Also, what's the difference between the aformentioned code and this:

@Instance

public static ClassName instance = new ClassName();

 

Do they both achieve the same thing?

 

Link to comment
Share on other sites

@Instance("ModID")

public static ClassName instance;

How does this instantiate anything though?

@Instance("ModID") so here I pass along the ModID as a string to this annotation...

public static ClassName instance; how does this line set the field to the instance of my mod? is the word instance here connected to the annotation @Instance?

 

@Instance

public static ClassName instance = new ClassName();

Why does this one still work?

@Instance why is there no ModID being passed along here?

public static ClassName instance = new ClassName(); doesn't this make more sense as it is creating a New instance? Is it this one you said won't be the one forge uses?

Link to comment
Share on other sites

FML will construct any @Mod it finds.

It will also apply any @Instance it finds.

If it finds a @Instance with no modid inside a class with a @Mod it will assume that it wants its own instance.

 

Calling new ClassName() yourself creates a extra instance of your class that is never used because that field would be instantly replaced by FML to the instance that FML created.

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

Link to comment
Share on other sites

@Instance("ModID")

public static ClassName instance;

How does this instantiate anything though?

@Instance("ModID") so here I pass along the ModID as a string to this annotation...

public static ClassName instance; how does this line set the field to the instance of my mod? is the word instance here connected to the annotation @Instance?

 

FML scans for all Instance annotations and uses reflection to populate the field with the instance of your mod being used internally. No, the field name is not connected.

 

@Instance

public static ClassName instance = new ClassName();

Why does this one still work?

@Instance why is there no ModID being passed along here?

public static ClassName instance = new ClassName(); doesn't this make more sense as it is creating a New instance? Is it this one you said won't be the one forge uses?

 

You still need to pass in the mod id to the annotation.

 

Yes, the only correct instance of your mod is the one created by Forge, not one you yourself instantiate.

Don't make mods if you don't know Java.

Check out my website: http://shadowfacts.net

Developer of many mods

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.

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



×
×
  • Create New...

Important Information

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