Can I save a clicked object instance for later reference?

0 favourites
  • 3 posts
From the Asset Store
Template for a generic save / load system, fully documented in comments and video
  • Hi everyone,

    Here's what I'm trying to achieve:

    1. Player clicks on a customer.

    2. Player's avatar walks (pathfinds) to that customer.

    3. Once the avatar has arrived at the customer's location, that customer's happiness level increases (this is an instance variable).

    The problem I'm having is that between steps 1 and 3, I "lose" the ability to know what customer instance was clicked on in step 1, so I'm then unable to modify the correct instance in step 3. It works if I modify the instance's variable in step 1 (since I still know what's been clicked on), but I need it to happen in step 3.

    Here's a screenshot of my logic with additional details. The way this logic is currently set up, when I refer to to "Sprite Customer > Add 1 to HappinessLevel" in the final step, it increases the happiness score of ALL customers, not just the one that was originally clicked.I read into storing the instance's UID to refer to it later, but got stumped (and it seems like bad practice?). I also looked into nesting my actions and conditions so I wouldn't lose the customer information, but then I was limited in the pathfinding behaviors I was able to use, so that didn't work either.

    Any help is appreciated. I've attached the capx file for the game. Thank you for reading!

  • Happy to report that I figured this out thanks to the post titled: How do I know which instance has which IID? (I can't post links so I guess just search the forum for it.)

    For anyone who has the same problem/question:

    -- I added a new instance variable to my SpriteCustomer called "SelectedCustomer", changed it to a Boolean Variable and left the default at false.

    -- In Step 1 as described in my above post, I added an action to toggle the "SelectedCustomer" variable. This sets the variable to "true".

    -- Once the player avatar arrives at the customer position, I added a second condition to look for the SpriteCustomer instance that had "SelectedCustomer" set to true. The exact condition I clicked was "Is boolean instance variable set" and then chose the "SelectedCustomer" variable from the dropdown.

    -- The actions to add 1 to happiness level worked perfectly.

    -- Then I added one last action to toggle the "SelectedCustomer" variable again. This sets the variable back to "false" so it's ready for me to click the next customer.

    Here's a picture. All the new stuff I added is underlined in blue.

    The logic is still a bit fragile - for example if I click a bunch of customers like crazy the game doesn't quite know what to do. But for demo purposes it's great.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Edit: this should be more stable and efficient than the solution you have now.

    Elementry, my dear gmullinix!

    When clicking on a customer, store the customers UID in an instance var of the player.

    On arrival, pick the customer again by using 'pick by UID', them happy. <img src="{SMILIES_PATH}/icon_e_confused.gif" alt=":?" title="Confused">

    Oh, and I added two constraints when clicking on a customer: the event won't run if a path is being calculated, or if the player is moving along a path.

    capx: ... .capx?dl=0

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)