Can I save a clicked object instance for later reference?

Get help using Construct 2

Post » Thu Dec 18, 2014 10:45 pm

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.

Image

Any help is appreciated. I've attached the capx file for the game. Thank you for reading!
You do not have the required permissions to view the files attached to this post.
B
3
Posts: 2
Reputation: 179

Post » Thu Dec 18, 2014 11:06 pm

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.
Image

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.
B
3
Posts: 2
Reputation: 179

Post » Thu Dec 18, 2014 11:13 pm

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', and...er...make them happy. :?

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:

https://www.dropbox.com/s/7a44388ls8j6g ... .capx?dl=0
Don't lose your work. Backup your game with Dropbox.
B
44
S
10
G
10
Posts: 1,106
Reputation: 9,202


Return to How do I....?

Who is online

Users browsing this forum: asdtricky, brunopalermo, ST314 and 39 guests