Changing Code at runtime

Hello, during my morning coffee warmup internetsurfing i found this video:



http://www.youtube.com/watch?v=2nfDEFFN54E



Notch is able to change the code at runtime and instantly use the changed code. From what i have seen this looks like plain java, even i don't need it now, it would be a nice thing to have when programming/debugging the more detailed features later.

The question is how does he do it, is it plain hotswapping with some kind of listener which detects what files have changed?

The SDK does this with Custom Controls :slight_smile: Actually I reloaded the scene when a Control changed for a while, giving the same effect I could just as well only reload the classes that have changed. Its classloader magic basically.

@normen said:
The SDK does this with Custom Controls :) Actually I reloaded the scene when a Control changed for a while, giving the same effect I could just as well only reload the classes that have changed. Its classloader magic basically.


Since you are probably faster then google in this case: when in the sources should i look for that functionallity?
@zzuegg said:
Since you are probably faster then google in this case: when in the sources should i look for that functionallity?

http://www.csg.ci.i.u-tokyo.ac.jp/~chiba/javassist/
or
http://groovy.codehaus.org/

Groovy Example i use:
Example code that gets compiled: http://pastebin.com/9vBcYvmQ
When i compile:
@zzuegg said:
Since you are probably faster then google in this case: when in the sources should i look for that functionallity?

Its just classloaders, the specific implementation in the SDK probably won't help you much. You can set the classloader(s) that the assetManager uses to load files, thats how the SDK reloads it. In your case you want to have a folder with a classloader and replace that classloader with a new one and load classes from that after you have compiled the classes, basically like any IDE does code checking. Or how plugins in java are typically handled.

Edit: Btw this is another point of doing the SDK, the infrastructure for stuff like this is already there. If somebody tries to do his own Swing code editor with automatic compiling now I go mad, really ;)
@normen said:
Its just classloaders, the specific implementation in the SDK probably won't help you much. You can set the classloader(s) that the assetManager uses to load files, thats how the SDK reloads it. In your case you want to have a folder with a classloader and replace that classloader with a new one and load classes from that after you have compiled the classes, basically like any IDE does code checking. Or how plugins in java are typically handled.

Edit: Btw this is another point of doing the SDK, the infrastructure for stuff like this is already there. If somebody tries to do his own Swing code editor with automatic compiling now I go mad, really ;)


Its actually not about editing here, i am talking about specific game functions, like controllers, actions ecc.

Hm, in this case, since i am using already reflections to load all my game components:

[java]
Reflections reflections;
reflections = new Reflections("com.respublic.mods.officialmain");
Set<Class<? extends RPAbstractMod>> tMods = reflections.getSubTypesOf(RPAbstractMod.class);
for (Class<? extends RPMod> tClass : tMods) {
mainMod.add(tClass.newInstance());
}
[/java]

i might have already all i need, i going to investigate this..
@zzuegg said:
Its actually not about editing here, i am talking about specific game functions, like controllers, actions ecc.

Hm, in this case, since i am using already reflections to load all my game components:

[java]
Reflections reflections;
reflections = new Reflections(&quot;com.respublic.mods.officialmain&quot;);
Set&lt;Class&lt;? extends RPAbstractMod&gt;&gt; tMods = reflections.getSubTypesOf(RPAbstractMod.class);
for (Class&lt;? extends RPMod&gt; tClass : tMods) {
mainMod.add(tClass.newInstance());
}
[/java]

i might have already all i need, i going to investigate this..

Well that is editing, just like its meant to be in the SDK: You code a control in one window and see the results in another. If you plan to add this to your game as kind of a "coding game", its way too dangerous in terms of hacking your game I guess. Also you need the compiler, which means a JDK.
@normen said:
Well that is editing, just like its meant to be in the SDK: You code a control in one window and see the results in another. If you plan to add this to your game as kind of a "coding game", its way too dangerous in terms of hacking your game I guess. Also you need the compiler, which means a JDK.


How do you simulate user input in the sdk?
Of course this would be easily possible for passive controls, but i have no idea how to make it with user activated functions (In the SDK)
@zzuegg said:
How do you simulate user input in the sdk?

:? Why simulate? The user enters code and adds Controls via the SDK ^^

@zzuegg said:
Of course this would be easily possible for passive controls, but i have no idea how to make it with user activated functions (In the SDK)

You'd just reload them from the new classloader.

Actually all the above stuff is not needed for my development.

Simply start your project in debug mode, add changes you want, save the file and press the green button ‘Apply code changes’



At least for me, thats enough

2 Likes

ooo thats good to know :slight_smile: i also wondered about this a while ago

Another way is to use products like jRebel from zeroturnaround, it does some JVM-magic to replace code in live systems that aren’t designed for reloading classes. It has other drawbacks but handles this pretty well.

1 Like