Sign in to follow this  
joedobo

Using weak reference and the action methods so a data structure can persist for life of action.

Recommended Posts

Using weak reference and the action methods so a data structure can persist for life of action.


I need to have some information persist for all calls to an action method for a given action attempt. Just to clarify, when a toon does something that starts a timer the action method is called multiple times during that time. I'm creating a data structure at the beginning of an action and I need that data to remain available throughout the timer. 
My problem is if the data structure is created inside the action method it gets deleted(garbaged collected) as soon as that method is done, it's reference is gone. One solution I used was to add a field to the Action class in WU with Javassist. The field's type would be a data structure class object. I'd use reflection get and set methods along with the action object instance that is a arg for the action method. This way my data structure should stick around until the current action ends.
I want to try something different mainly because I'd like to not use the reflective get and set methods for something that can be called a lot.

 

 

here is some code on my idea. I removed parts from it that aren't relevant and add some additional for clarity. ******Will this work?******

class CreateEssenceAction implements ModAction, BehaviourProvider, ActionPerformer {
  private WeakHashMap<WeakReference<Action>, EssenceActionData> actionListener;

  public CreateEssenceAction() {
      actionListener = new WeakHashMap<>();
  ...


  @Override
  public boolean action(Action action, Creature performer, Item gemActive, Item amphoraTarget, short num, float counter) {
      if (performer instanceof Player && amphoraTarget != null && activeItemIsCatalyst(gemActive) &&
              amphoraTarget.getTemplateId() == ItemList.amphoraLargePottery) {
          try {
              final float ACTION_START_TIME = 1.0f;
              EssenceActionData essenceActionData;
              if (counter == ACTION_START_TIME) {
                  essenceActionData = new EssenceActionData(gemActive, amphoraTarget);
                  WeakReference<Action> weakActionReference = new WeakReference<>(action);
                  actionListener.put(weakActionReference, essenceActionData);
  ...

 

Edited by joedobo

Share this post


Link to post
Share on other sites

Yes, this should work. You can use a WeakHashMap<Action, EssenceActionData>. The WeakHashMap already uses WeakReferences internally.

The weak hash map will keep the extra data for the action as long as the Action object exists.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this