Modifying class member variables in other mods. #2350
-
I'm wondering on how I can modify a member variable within another mod's class file. For instance, I'm trying to modify the window width of the Origin mod's display screen. I've narrowed down where this occurs to io.github.apace100.origins.screen.OriginDisplayScreen at line 31. While I know that mixin can inject and modify variables within methods, I'm wondering how I can accomplish this with variables declared out of the scope of a method but within a class. Any help on this subject is greatly appreciated, thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Nevermind, I cloned the mod and tried out changing the windowWidth to double its current amount (from 176 to 352) and it broke a lot of things. I think I'm fine without a proper answer to this. |
Beta Was this translation helpful? Give feedback.
-
In case you want to know the real answer for future reference, This is really just a quick way of writing a normal mixin with a shadowed mutable field and then adding your own interface to access the field. However there is a gotcha. There is no guarantee the code using that final field actually accesses it at runtime. |
Beta Was this translation helpful? Give feedback.
In case you want to know the real answer for future reference,
you should use an accessor
https://fabricmc.net/wiki/tutorial:mixin_accessors
which you will need to annotate as
@Mutable
to workaround the "final".This is really just a quick way of writing a normal mixin with a shadowed mutable field and then adding your own interface to access the field.
However there is a gotcha. There is no guarantee the code using that final field actually accesses it at runtime.
The compiler is allowed to "inline" the values of static final fields into referencing methods at compile time.
So changing the value of the static final field will do nothing if that happens.