Help with CoC-style game in JavaFX

Stolhem

New Member
Feb 6, 2016
4
0
Long time CoC fan here. Ever since playing the game I've become interested in making my own CoC-style text adventure game with a higher degree of player customization. I've started working on my own project in JavaFX, and the UI is nearly done. I've been using the CoC source as a sort of guideline, the obvious difference being it's written in Flash instead of Java. I've noticed several classes in the CoC source code invoke methods in Player.as without initializing player.as in said class. I can't get this to work in Java without initializing the player class every time I want to invoke a method from it, which ends up creating a new Player object every time. I only want Player to be initialized once, but I'd like to be able to invoke methods in the Player class without creating a new Player object every single time.


Here are all the files in my project folder. To clarify further, I want to invoke Player.java methods from either of the Controller.java classes without initializing Player each time. 


tl;dr 


Lots of class files in CoC invoke player methods without initializing the player class. I can't figure out how to do this in javaFX without initializing player class every time, which gets ridiculously messy. If anyone has a solution to this it'd be greatly appreciated!
 

Etis

Well-Known Member
Creator
Aug 26, 2015
2,500
258
Em... You should initialize player once and then store reference to it for future use. And about invoking methods without object - you should make them static. OFC, they should be standalone to do this, otherwise use said reference.
 

Stolhem

New Member
Feb 6, 2016
4
0
Em... You should initialize player once and then store reference to it for future use. And about invoking methods without object - you should make them static. OFC, they should be standalone to do this, otherwise use said reference.

Thanks for your quick response. I read somewhere that it was poor programming ethics to avoid multiple initializations by making everything static, but I don't think they were trying to make a text porn game
 

Etis

Well-Known Member
Creator
Aug 26, 2015
2,500
258
Sort of. This is actually complicated thing. Basically, static functions are for things which are not affilated with any particular object. But I can't say anything specific without knowing which functions you want to have as static. Maybe you are just doing it wrong? CoC have a lot of major architectural flaws. If you want example, you should use TiTS source.
 

Stolhem

New Member
Feb 6, 2016
4
0
here's a rough example of what I'm trying to accomplish:


Let's say Player.java has a method called public void setGoldAmt() to set the player's current gold amount. In Controller1.java, I want to have the line: player.setGoldAmt(10) to give the player 10 gold. Then, in Controller2.java, let's say I want to change the gold amount to 30 by invoking the same method player.setGoldAmt(30). This will only work if the method in Player.java is static. So it seems like I should make every function in Player.java that I want to have access to elsewhere a static function?
 

Etis

Well-Known Member
Creator
Aug 26, 2015
2,500
258
here's a rough example of what I'm trying to accomplish:


Let's say Player.java has a method called public void setGoldAmt() to set the player's current gold amount. In Controller1.java, I want to have the line: player.setGoldAmt(10) to give the player 10 gold. Then, in Controller2.java, let's say I want to change the gold amount to 30 by invoking the same method player.setGoldAmt(30). This will only work if the method in Player.java is static. So it seems like I should make every function in Player.java that I want to have access to elsewhere a static function?

You are definitely doing it wrong :)


You should create field for player object somewhere and call it, like Chars.pc().setGoldAmt(); - this is how TiTS does it.
You can also use singleton pattern - this is recommended way to use unique objects in java, would be like Player.getInstance().setGoldAmt();


Basically, both are the same - you store objects after creation and reuse it. I'd recommend TiTS way, with HashMap of characters covered under named functions. It would make iteration over all game characters much easier, and you'll need it for save/load and time aware things.
 
Last edited by a moderator:

Stolhem

New Member
Feb 6, 2016
4
0
You are definitely doing it wrong :)


You should create field for player object somewhere and call it, like Chars.pc().setGoldAmt(); - this is how TiTS does it.
You can also use singleton pattern - this is recommended way to use unique objects in java, would be like Player.getInstance().setGoldAmt();


Basically, both are the same - you store objects after creation and reuse it. I'd recommend TiTS way, with HashMap of characters covered under named functions.

Followed your advice to use singleton pattern and it's working great now. Should be fine for my purposes but if I run into issues later I may switch to the TiTS way. Thanks for your help :)