Poseidon5001 Posted July 20, 2016 Posted July 20, 2016 @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? Quote
shadowfacts Posted July 20, 2016 Posted July 20, 2016 The Instance annotation sets the field to the specific instance of your mod that Forge is using internally via reflection. You shouldn't instantiate your mod class yourself, the one you've instantiated won't be the one Forge uses. Quote Don't make mods if you don't know Java. Check out my website: http://shadowfacts.net Developer of many mods
Poseidon5001 Posted July 20, 2016 Author Posted July 20, 2016 @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? Quote
LexManos Posted July 20, 2016 Posted July 20, 2016 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. Quote I do Forge for free, however the servers to run it arn't free, so anything is appreciated. Consider supporting the team on Patreon
shadowfacts Posted July 20, 2016 Posted July 20, 2016 @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. Quote Don't make mods if you don't know Java. Check out my website: http://shadowfacts.net Developer of many mods
Recommended Posts
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.