[英]How to exit 2D game simulation (only) without exiting the Java code?
I (new to Java) am working on a decades old Java project built using Golden T Studios Game dev jdk.我(Java 新手)正在使用 Golden T Studios Game dev jdk 构建一个已有数十年历史的 Java 项目。 I have a game in the project which runs a 2D simulation.
我在项目中有一个运行 2D 模拟的游戏。 The code structure in a nutshell is as follows:
简而言之,代码结构如下:
package game;
import java.io.*;
import java.lang.reflect.Field;
import javax.swing.JFrame;
import com.golden.gamedev.GameLoader;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import sometcpimports.*;
public class MainGAME extends JFrame implements Runnable {
public static void main(String[] args) { //call new MainGAME;
}
public MainGAME() {//initiate game parameters;
//start new THREAD;
}
@Override
public void run() { //initiate new game and environment;
game = new GameLoader();
gameenv = new GameEnvironment(params); //This class is in another file "public class GameEnvironment extends Game {}"
//I don't clearly undertsand what the following lines do, so I'm mentioning them as is;
game.setup(gameenv, dimensions);
this.setVisible(false);
gameenv.setVisible(false);
game.start();
game.setVisible(true);
//tbd (code reaches this step)
}
}
My goal is to run the above simulation multiple times (with different inputs each time) and extract information after each run in a new main class as follows.我的目标是多次运行上述模拟(每次使用不同的输入)并在每次运行后在新的主 class 中提取信息,如下所示。
public class gamedriver {
public static void main(String[] args) {
MainGAME.params = some params;
MainGAME.main(); // runs the simulation;
//tbd1 (code doesn't reach this step)
}
}
The issue is that, on running the simulation from a different file (new main class), I am unable to exit 2D simulator after one run.问题是,在从不同的文件(新的主类)运行模拟时,我无法在一次运行后退出 2D 模拟器。 The code doesn't reach
//tbd1
in the new main class which has some output print statements.代码在新的主 class 中没有到达
//tbd1
,其中有一些 output 打印语句。 Essentially I want to simply exit simulator and not the whole JVM.本质上我想简单地退出模拟器而不是整个 JVM。 So far I've tried:
到目前为止,我已经尝试过:
game.stop()
& gameenv.finish()
at //tbd
which does nothing. //tbd
的game.stop()
和gameenv.finish()
什么都不做。System.exit(0)
at //tbd
which exits game after simulation but also exits jvm and doesnt reach the other main class. System.exit(0)
at //tbd
在模拟后退出游戏但也退出 jvm 并且没有到达其他主要 class。finish()
at both //tbd
and GameEnvironment class which behaves exactly like point 2. //tbd
和 GameEnvironment class 处的finish()
,其行为与第 2 点完全相同。 Additionally, I am unable to run the above (MainGAME in gamedriver class) in a for loop.此外,我无法在 for 循环中运行上述(游戏驱动程序类中的 MainGAME)。 This throws
Game.Exception error
.这会引发
Game.Exception error
。 I understand it has something to do with threads but I'm not sure how to run them.我知道它与线程有关,但我不确定如何运行它们。
Thank you for your time.感谢您的时间。 Appreciate your help!
感谢你的帮助!
I found a method to solve the above problem.我找到了解决上述问题的方法。 I'm posting my workaround as a reference for someone who encounters a similar issue.
我将我的解决方法发布为遇到类似问题的人的参考。 Please note that there might be other (better) solutions out there.
请注意,可能还有其他(更好的)解决方案。
System.out.println()
in MainGAME to print all my outputs at the end of simulation.System.out.println()
以在模拟结束时打印我的所有输出。public class gamedriver {
public static void main(String[] args) {
try {
String separator = System.getProperty("file.separator");
String classpath = System.getProperty("java.class.path");
String path = System.getProperty("java.home") + separator + "bin" + separator + "java";
String[] command = new String[]{path, "-cp", classpath, gamedriver.GAME.class.getName(), "output.csv"};
Process process = Runtime.getRuntime().exec(command);
process.waitFor();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
static class GAME {
public static void main(String args[]) {
PrintStream out = null;
try {
out = new PrintStream(args[1]);
} catch (FileNotFoundException p) {
p.printStackTrace();
}
System.setOut(out);// this catches all output from the game to a csv file (output.csv)
MainGAME temp = new MainGame(some params); // initiate the simulation
temp.run()
out.close();
System.exit(0); // force close the simulator
}
}
}
This might be a sloppy way to solve the issue, but it worked for me, especially because none of the other methods provided by GTGE worked.这可能是解决问题的一种草率方法,但它对我有用,特别是因为 GTGE 提供的其他方法都不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.