Difference between revisions of "Hotswap"

From Forge Community Wiki
(Guide about hot-swapping both code and resources)
 
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
== What even is this? ==
+
{{Under construction}} <!-- Guide for other IDEs is missing -->
Hot-swapping is a feature letting you to see code/resource changes in-game without restarting it.
+
== What is Hot-swapping? ==
This article is divided into 2 sections: Basic hot-swapping and schema change hot-swapping, as latter one requires more setup.
+
Hot-swapping is a feature letting you to swap-out code of your mod without restarting game, allowing you to see changes quickly and tinker around with it more easily.
  
== Basic hot-swapping ==
+
In this article, we will also be covering how to swap resources so that you can change models and textures, and see it in-game quickly.
Here, you would be able to swap out resource pack/data pack of your mod without restarting the game. You can also swap out code if only method's internal has been changed. You would still need to restart game if you added/removed/renamed method/variable/class.
+
 
 +
== Swapping resources ==
 +
=== Swapping resources on Intellij Idea ===
 +
After you made changes to resource file, hit this "hammer icon", or build button.
 +
[[File:Intellij_idea_menu_for_hot_swap_doc.png]]
 +
After that, hit F3+T if resources you changed are assets. It's a shortcut-key for reloading assets.
 +
If they're data pack, use /datapack command to disable and then re-enable data pack of your mod.
 +
 
 +
== Hot-swapping code ==
 +
You can also swap out code if only method's internal has been changed. You would still need to restart game if you added/removed/renamed method/variable/class. This change is called "schema change", and guide for applying schema change is in below section.
 +
 
 +
Ensure that you are running the game on debug mode in IDE. It is usually indicated by green bug-shaped icon.
 +
[[File:Debugmodeintellij.png]]
 +
=== Hot-swapping code on Intellij Idea ===
 +
It's more simple than swapping out resources. After making changes to the code, just hit the build button. Then It will show you this message on bottom-left corner. If your mod project is big and it takes a long time to do so, hit <code>Build -> Recompile "currently opened file's name"</code>, this will only recompile file that is currently open. This option will not be visible if you don't have compile-able file opened. Files with extensions such as <code>.java</code> or <code>.kt</code> are compile-able.
 +
[[File:Hotswapmessage.png]]
 +
If you changed class's schematics then will show you error messages saying it is unsupported. Head to Applying schema changes section if you need to change schematics.
 +
 
 +
=== Applying schema changes ===
 +
This requires more setup and specific JDK, Jetbrains Runtime (JBR).
 +
But it will greatly accelerate development process and make tinkering easier by allowing you to:
 +
* Create/Remove classes
 +
* Add/Rename/Remove variables/methods
 +
* Change value of final constants
 +
All without restarting the game.
 +
 
 +
The reason why JBR is suggested over other JDKs is that it has [https://dcevm.github.io/ DCEVM](It allows class schema change in debug mode) by default and requires less configuration, such as hot-swap agent and IDE-specific plugins, etc. Although, there isn't exactly "installer" for it, you would have to download and unpack it manually.
 +
 
 +
==== Downloading JBR ====
 +
Download JBR with right java version and platform from here: https://github.com/JetBrains/JetBrainsRuntime/releases
 +
There are many different "flavors" but I recommend you downloading "JBR (vanilla)" one if you don't know what these are.
 +
After downloading has finished unpack it into suitable location
 +
 
 +
==== Adding JDK to IDE ====
 +
===== Adding JDK to Intellij Idea =====
 +
Go to File -> Project Structure -> Project -> SDK -> Add JDK
 +
and then select directory you've unpacked JBR into.
 +
After that, It is recommended to choose JBR as project SDK if you're not familiar with gradle and Intellij's run configs
 +
[[File:Addjdk.png]]
 +
 
 +
==== Changing run configuration ====
 +
Open build.gradle file and then add <code>jvmArg "-XX:+AllowEnhancedClassRedefinition"</code> to desired run configuration. below is example for adding it into <code>runClient</code>
 +
<syntaxhighlight lang="groovy">
 +
runs {
 +
    client {
 +
        workingDirectory project.file('run')
 +
        jvmArg "-XX:+AllowEnhancedClassRedefinition"
 +
        property 'forge.logging.markers', 'REGISTRIES'
 +
        property 'forge.logging.console.level', 'debug'
 +
        property 'forge.enabledGameTestNamespaces', 'examplemod'
 +
        mods {
 +
            examplemod {
 +
                source sourceSets.main
 +
            }
 +
        }
 +
    }
 +
}
 +
</syntaxhighlight>
 +
 
 +
==== Final step ====
 +
===== Final configuration for Intellij Idea =====
 +
Go to <code>File -> Settings -> Build, Execution, Deployment -> Build Tools -> Gradle</code>, and use Intellij Idea for running and building and testing
 +
[[File:Gradleintellij.png]]
 +
 
 +
Then re-generate run configurations by running <code>genIntellijRuns</code>
 +
 
 +
Done! Now launch one of these on debug mode and you can now change classes' schematic as much as you want, and then hit build button. it will swap those just fine.
 +
[[File:Runconfigs.png]]

Latest revision as of 16:14, 6 October 2022

This page is under construction.

This page is incomplete, and needs more work. Feel free to edit and improve this page!

What is Hot-swapping?

Hot-swapping is a feature letting you to swap-out code of your mod without restarting game, allowing you to see changes quickly and tinker around with it more easily.

In this article, we will also be covering how to swap resources so that you can change models and textures, and see it in-game quickly.

Swapping resources

Swapping resources on Intellij Idea

After you made changes to resource file, hit this "hammer icon", or build button.

Error creating thumbnail: Unable to save thumbnail to destination

After that, hit F3+T if resources you changed are assets. It's a shortcut-key for reloading assets. If they're data pack, use /datapack command to disable and then re-enable data pack of your mod.

Hot-swapping code

You can also swap out code if only method's internal has been changed. You would still need to restart game if you added/removed/renamed method/variable/class. This change is called "schema change", and guide for applying schema change is in below section.

Ensure that you are running the game on debug mode in IDE. It is usually indicated by green bug-shaped icon.

Error creating thumbnail: Unable to save thumbnail to destination

Hot-swapping code on Intellij Idea

It's more simple than swapping out resources. After making changes to the code, just hit the build button. Then It will show you this message on bottom-left corner. If your mod project is big and it takes a long time to do so, hit Build -> Recompile "currently opened file's name", this will only recompile file that is currently open. This option will not be visible if you don't have compile-able file opened. Files with extensions such as .java or .kt are compile-able.

Error creating thumbnail: Unable to save thumbnail to destination

If you changed class's schematics then will show you error messages saying it is unsupported. Head to Applying schema changes section if you need to change schematics.

Applying schema changes

This requires more setup and specific JDK, Jetbrains Runtime (JBR). But it will greatly accelerate development process and make tinkering easier by allowing you to:

  • Create/Remove classes
  • Add/Rename/Remove variables/methods
  • Change value of final constants

All without restarting the game.

The reason why JBR is suggested over other JDKs is that it has DCEVM(It allows class schema change in debug mode) by default and requires less configuration, such as hot-swap agent and IDE-specific plugins, etc. Although, there isn't exactly "installer" for it, you would have to download and unpack it manually.

Downloading JBR

Download JBR with right java version and platform from here: https://github.com/JetBrains/JetBrainsRuntime/releases There are many different "flavors" but I recommend you downloading "JBR (vanilla)" one if you don't know what these are. After downloading has finished unpack it into suitable location

Adding JDK to IDE

Adding JDK to Intellij Idea

Go to File -> Project Structure -> Project -> SDK -> Add JDK and then select directory you've unpacked JBR into. After that, It is recommended to choose JBR as project SDK if you're not familiar with gradle and Intellij's run configs

Error creating thumbnail: Unable to save thumbnail to destination

Changing run configuration

Open build.gradle file and then add jvmArg "-XX:+AllowEnhancedClassRedefinition" to desired run configuration. below is example for adding it into runClient

runs {
    client {
        workingDirectory project.file('run')
        jvmArg "-XX:+AllowEnhancedClassRedefinition"
        property 'forge.logging.markers', 'REGISTRIES'
        property 'forge.logging.console.level', 'debug'
        property 'forge.enabledGameTestNamespaces', 'examplemod'
        mods {
            examplemod {
                source sourceSets.main
            }
        }
    }
}

Final step

Final configuration for Intellij Idea

Go to File -> Settings -> Build, Execution, Deployment -> Build Tools -> Gradle, and use Intellij Idea for running and building and testing

Error creating thumbnail: Unable to save thumbnail to destination

Then re-generate run configurations by running genIntellijRuns

Done! Now launch one of these on debug mode and you can now change classes' schematic as much as you want, and then hit build button. it will swap those just fine.

Error creating thumbnail: Unable to save thumbnail to destination