java - realtime update an objects variables from hashmap of those variables -


this questions noob rated, have no solution , googleling led me updating values in hash, , know.

i making rpg, , question affect has stats (like equipment class has addedstr, addediq, addedagi , on).

to simplify update process, have added these variables in hashmap , update hashmap following way:

hashmap<string,integer> statshash = new hashmap<>();  void setstatshash() {   statshash.put("addedcon", addedcon);   statshash.put("addedstr", addedstr);   statshash.put("addedagi", addedagi);   ... }  void changestatshash(string instat, int inchange) {   statshash.put("addedcon", statshash.get("addedcon") + 1);   (this example, not point out instat , inchange dummy variables) } 

and works well, can update stats in `hashmapv. problem following:

myequipment.changestatshash("addedcon", 1); myequipment.printstatshash(); system.out.println(myequipment.addedcon); 

when this, printstatshash indicate addedcon = whatever before +1. myequipment.addedcon has not changed.

so question , only:

how update "mother" variable hashmap everytime variable in hashmap changes?

i hoping hashmap pointer original object , change together, not seem way.

thanks!

how update "mother" variable hashmap everytime variable in hashmap changes?

the map contains different object "mother" variable (i presume here mean instance variable), changes 1 not reflected in other (it unclear if both objects, or if instance variable primitive).

to update value either location (the map or instance variable), make both refer same object. example, wrap value it's own class:

public class intwrapper{     private int value;      public void setvalue(int val){ value = val;}     public int getvalue(){ return value;}      public void incrementvalue(int inc){ setvalue(value + inc);} } ...  hashmap<string,intwrapper> statshash = new hashmap<>(); addedcon = new intwrapper(); statshash.put("addedcon", addedcon);  ...  void changestatshash(string instat, int inchange) {     statshash.put("addedcon", statshash.get("addedcon").incrementvalue(1)); } 

as along both instance variable addedcon , within map same object. being said, i'm not sure grasp how placing value in map 'simplifies process'.


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -