1 decade ago
by OMATASE
I have 4 characters on the screen that the player controls. When any entity is clicked, a menu pops up. As the player makes selections in this menu, the menu changes to show current selections and show new choices.
Once a player has made their choices for say character #1, they will click on to character #2 and make new choices for that character. When the menu pops up on character #2 it will be a different instance of the menu because I do an ig.game.removeEntity() then an ig.game.spawnEntity().
The problem I'm having is when the player clicks on character #1 again I don't want the menu to have its state reset. I want it to initialize with the same state it had for character #1 after all the choices had been made. A way to save and restore the state for a particular entity would probably work well in this case. Is there a way to do
that?
What's a good alternative for this if not? I'd rather not store data that would allow me to manually reinitialize the menu as it's time consuming and problematic.
If the best way to go about it is to not remove the menu entity ever, but just hide the menu entity, what performance implications would that have? Could I keep the entity around without having its update and draw methods being called?
Thanks!
1 decade ago
by cweed
I am still a novice programmer, but I have some thoughts on your problem which may help - if I'm understanding it correctly and can manage to explain it well enough.
It's hard to say exactly without knowing what your menu does. But lets say as an example that you use it to choose what weapon the character will use, and how many spaces in a direction they will move.
You click on character A and the menu opens. You chose sword as the weapon, and this sets
this.weaponChoice = "sword"
inside the character A entity. Then you close the menu and move on to character B. You finish selecting for B and move back to character A. You then feed
weaponChoice
to the menu you&
039;re spawning through #settings
, and the menu is drawn as though the sword has already been selected.
So in other words the menu entity is created using each character's individual states for each of the choices. Each turn you set the states back to default for all of the characters and so the first time the menu is drawn for each of them, it's drawn in the default neutral position.
so inside character A it would look something like
if (selected()){
ig.game.spawnEntity(EntityMenu, x, y, {weapon: this.weaponChoice, movement: this.moveChosen});
}
and so the menu entity would take the values for
weapon
and
movement
and create a menu in the desired state based on that.
Does that help?
1 decade ago
by OMATASE
I could do it that way but this is the methodology I was referring to when I wrote "I'd rather not store data that would allow me to manually reinitialize the menu as it's time consuming and problematic."
It can be a frustrating way to implement this type of thing because some data may not make sense for storing in the character, or if I give the menu another feature I have to remember and take the extra time to save and restore that data which can become a maintenance problem. It's also common with this type of implementation to have to do double implementation of something so that it will behave differently on reinitialization. Things like bypassing animations, etc. that I'd like to avoid. All of these additional considerations add up to added bugs (sometimes hard to find bugs) and increased maintenance.
It would work, but having been down this road before I know it's less work to be able to save and restore state or just keep the entity around (as long as it's not using resources).
I imagine with javascript there is a way to take a class instance and serialize it to a JSON representation that can be turned back into that class instance later, but I don't know if that's a good way of approaching this since it would probably require use of the frowned-upon eval() method.
If there was an impact supported way of removing an entity from the game's entities array without destroying the instance. Or if I could tell the game to keep an entity around but not call draw or update on that entity that might be good as well. I could add visibility support to my entity class where when visible = false it ignores update and draw calls. I hadn't thought of that. It would probably be slightly less efficient than the other options though.
1 decade ago
by cweed
Ahhh
Ok, apologies, I understand what you're asking now. And it's made me realize that I've probably been handling my own game incorrectly. I would love to figure out how to do this for my own personal selfish reasons now :)
hmm, rather than save the state of the entity and try to serialize the entire Class' into JSON, why not just make a seperate save file
// global object
var playerState = {
weapon: "sword",
armor: "leather",
health: "70",
}
then upon loading each level, load the playerState into the player class.
##
1 decade ago
by OMATASE
This isn't information I need to persist beyond the current scene. It's not data that I need to hold on to and present to the user the next time they start up the game for instance. I just need to hang on to the data temporarily in the existing scene and session probably for 1 - 5 minutes.
I might not know what you mean by "save file" but this doesn't look like it will satisfy the need. It doesn't look like it's really different from what cweed and I were talking about other than it introduces a file into the mix.
Page 1 of 1
« first
« previous
next ›
last »