Getting the type of an actor using actor-on-screen (Answered by noxtudios)

Fool

  • Posts: 88
So I'm writing an actor that targets the nearest enemy each tick. But the problem is no enemies are actually being targeted.
I appreciate any help anyone can offer solving this. I've tried several different solutions and no luck. Thanks.

Heres what I have so far.

Edit: So what I determined is 'Last Distance' is ALWAYS equal to 64, according to a print statement. For no apparent reason.
Maybe a rounding issue? I'll try rounding the distance attribute before doing   "last distance = distance", and see if its that.




« Last Edit: June 15, 2016, 03:55:57 am by Fool »

noxtudios

  • Posts: 293
Just a quick observation..

You probably only want to set the Last Distance if the target is actually the closest. Thus comparing all next targets to that last closest actor Distance. Then it will skip all other actors that are further away.
Put "Set Last Distance to Distance" in the If Statement (Distance < Last Distance)
Then it only sets the last distance if the last distance was the closest

Also Set Last Distance starting value to 1000000 or other large Number, as its always 0 if you change the above..
Then if there are any actors on screen, it will find the first one and set that as the Last Distance, then compare all other on screen actors to that Last Distance

Edit. Just checking again, your way of doing it should be fine i think , I did the same thing in my old game, and thats the way I did it above..
I'll go and double check my code and compare to yours now..

Just checked my old game.

Distance = 1000000
For Each Actor Of GROUP
If (Actor Of Group - Actor(DistanceCalc)) < Distance
     Set ActorAttribute to ActorOfGroup
     Set Distance to (Actor Of Group - Actor(DistanceCalc))
End Loop


If ActorAttribute Has Value
     Closest Actor Found in Scene
    Do Stuff Here with Actor

« Last Edit: June 15, 2016, 01:20:48 am by noxtudios »

Fool

  • Posts: 88
Hey thanks nox!

The problem it turned out was I was trying to do actor-wise comparisons inbetween for-each loops. So things were becoming confusing. I ended up turning my distance code into a really compact custom block GetDistance, which was called by another custom block, GetClosestActor. No attributes needed.  After that the tick event just was implemented follow some pseudo code
I wrote while trying to figure out how to put it all together.

 for each actor_on_screen
    if actor_on_screen = target type && actor_on_screen != current_target:
        if current_target = null:
            current_target = actor_on_screen
        otherwise:
            current_target = get_closest_actor(self, current_target, actor_on_screen)

For anyone reading this, all this really says is if the actor on the screen is the right target type, but doesn't equal the current target, it means either current target isn't set or it is, and we should do a distance calculation.

In other words we arrived at roughly the same conclusion. Laughed out loud when I saw your reply this morning. Thanks Nox.


« Last Edit: June 16, 2016, 11:46:22 am by Fool »