Timeline for storing game options in java
Current License: CC BY-SA 3.0
13 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Sep 30, 2015 at 21:36 | comment | added | tandersen | You might not be able to use Options.ScreenWidth, but you can do Options.Instance.ScreenWidth, right? Or, Options.getInstance().ScreenWidth, as seems to be your case. | |
| Sep 30, 2015 at 21:36 | comment | added | Alkarym Moro | Let us continue this discussion in chat. | |
| Sep 30, 2015 at 21:28 | comment | added | Alkarym Moro | I see... i forgot why i made it two files to begin with, as I said, I got quite lost in the process of trying and failing! I think it's because of the static variables to begin with since i couldn't pass the Options class as a whole and "this" couldn't be used in a static method... I think i'll just dump the static variables and try to deal with the rest ;) I just wish i could've called Options.screenWidth ;) | |
| Sep 30, 2015 at 21:17 | comment | added | tandersen | "o = (Options) ObjectInputStream.readObject() " - that's not going to work. o is only a reference, so you're not actually changing the value of your Options instance. You need something like "Options.Instance = (Options) ObjectInputStream.readObject()". I'd probably do it inside the singleton itself. "this.Instance = (Options) ObjectInputStream.readObject()" | |
| Sep 30, 2015 at 21:12 | comment | added | Alkarym Moro | still not working... for some reason, when i load the file it wont load the data. now i do Options o = Options.getInstance() and then o = (Options) ObjectInputStream.readObject() but still, I only get the default values even tho i change them before saving. I'll post the code i got so far and you can tell me where the problem is... | |
| Sep 30, 2015 at 20:51 | comment | added | tandersen | I would go with the singleton in this case. I usually prefer singleton services over static classes. Mostly because static classes are much harder to mock when you are unit testing. | |
| Sep 30, 2015 at 20:46 | comment | added | Alkarym Moro | well i was making them static so that i could access them by typing Options.screenHeight for example. I could make my class a singleton and insure that every instance of it are of the same object. I made them static as well because if I wanted to call Options.setScreenHeight() i could w/o intanciating a new Options object. | |
| Sep 30, 2015 at 20:41 | comment | added | tandersen | I don't think serializer works with static fields. Is there some reason you need them to be static? I would imagine your options file is the only instance in the program. | |
| Sep 30, 2015 at 19:56 | comment | added | Alkarym Moro | ok, i think i got lost on the way... so, i have my class Options which contains all the static values of all my options. I have a classe serializer that loads and save the content of my option class but when i do Options o = new Options and save that "o" it saves only the name of the class... what am i doing wrong? | |
| Sep 30, 2015 at 18:20 | comment | added | tandersen | Great! Glad I could help. Java's built in serialization is much nicer than having to write it yourself, haha. Although, like I implied, it gets a bit messier if you need to serialize pointers. | |
| Sep 30, 2015 at 14:21 | vote | accept | Alkarym Moro | ||
| Sep 30, 2015 at 14:20 | comment | added | Alkarym Moro | OMG!! that's exactly what i needed! now i can add all my options to my options class and simply serialize it to a file and deserialyse it to retrieve the saved options! I knew it was worth it to wait one more day to code that class! I was going to have a map or string and objects and try to figure out the type of each variables once casted as object LOL! what a pain! You just saved me tons of hours of coding and about 3 full bottles of tylenols! | |
| Sep 29, 2015 at 22:57 | history | answered | tandersen | CC BY-SA 3.0 |