playMaker

Author Topic: "Find Closest Object with Name X"?  (Read 989 times)

westingtyler

  • Full Member
  • ***
  • Posts: 125
    • View Profile
"Find Closest Object with Name X"?
« on: December 06, 2016, 09:07:01 PM »
Find Closest Object (to object A) with Name X.

It sucks to have to create a whole new tag just for one command, since I can only find closest "with tag X" currently.

NPCs in my game go to the job board, then they need to find the nearest game object called "free job post" to get assigned a job.

Then they need to find the nearest free bed. the nearest free bathroom. then the nearest free fishing spot on the dock. surely there is a way to do this without creating 1000 new tags?
« Last Edit: December 06, 2016, 09:14:46 PM by westingtyler »

terri

  • Sr. Member
  • ****
  • Posts: 349
    • View Profile
    • terrivellmann.tumblr.com
Re: "Find Closest Object with Name X"?
« Reply #1 on: December 07, 2016, 02:41:07 PM »
You could use arrays and find the closest gameobject from the array.

westingtyler

  • Full Member
  • ***
  • Posts: 125
    • View Profile
Re: "Find Closest Object with Name X"?
« Reply #2 on: December 07, 2016, 10:23:45 PM »
i will look into that, but it adds so many unnecessary steps. it would be so much more streamlined if I could just find the nearest with a name.

I would then need to be able to add free beds dynamically to the array at run time, meaning I'd need to find them all anyway, getting us back to the initial problem.
« Last Edit: December 07, 2016, 10:25:34 PM by westingtyler »

terri

  • Sr. Member
  • ****
  • Posts: 349
    • View Profile
    • terrivellmann.tumblr.com
Re: "Find Closest Object with Name X"?
« Reply #3 on: December 08, 2016, 09:07:48 AM »
ok I modified the original action, I think its what you want

be aware that its probably the least efficient way to do this if I'm not mistaken, you're pretty much going through every gameobject in the scene, possibly every frame

westingtyler

  • Full Member
  • ***
  • Posts: 125
    • View Profile
Re: "Find Closest Object with Name X"?
« Reply #4 on: December 08, 2016, 03:43:50 PM »
Thank you. I will implement this and then work on a better way down the road.

I plan to do this once every half hour of game time for about 20 npcs. and possibly once per npc when the npc is actively looking for a job/home at the beginning of the game. npcs take turns doing this kind of searching. do you foresee a noticeable fps drop?

Regarding the use of arrays as you mentioned before, how would you dynamically add all new beds made to the array to make them available for use? for instance, I build new homes, and homeless npcs then go "oh, a new bed is available" and fill that bed?

thanks for your help so far!
« Last Edit: December 08, 2016, 03:45:21 PM by westingtyler »

terri

  • Sr. Member
  • ****
  • Posts: 349
    • View Profile
    • terrivellmann.tumblr.com
Re: "Find Closest Object with Name X"?
« Reply #5 on: December 08, 2016, 03:53:59 PM »
I don't know about fps drops, might be the case of you stress testing it somehow. Also depends a lot of the build is for mobile/desktop or whatever. Also could be totally fine.

You can either have the beds add themselves to the array (and remove themselves once taken), or wherever you have the code to spawn them, add them after spawning.
You can keep all the arrays on a gameobject that is set as a global variable to make it more practical.

westingtyler

  • Full Member
  • ***
  • Posts: 125
    • View Profile
Re: "Find Closest Object with Name X"?
« Reply #6 on: December 09, 2016, 01:26:31 AM »
thanks for the help.

terri

  • Sr. Member
  • ****
  • Posts: 349
    • View Profile
    • terrivellmann.tumblr.com
Re: "Find Closest Object with Name X"?
« Reply #7 on: December 10, 2016, 02:43:19 AM »
no problem!