HeroEngine Forums

HeroEngine Support => Scripting & Programming => Topic started by: GlorianLanTarini on Feb 17, 17, 07:09:04 AM

Title: (resolved)Unstable external function
Post by: GlorianLanTarini on Feb 17, 17, 07:09:04 AM
I use NormalizeVector3 function to quick calculation of direction, like this:

Code: [Select]
direction as Vector3 = tgtPos - myPos
NormalizeVector3(direction, direction)

But very often I get this error:

SCRIPT ERROR: Exception UsageException in function 'NormalizeVector3' invalid numeric result

How to lower chance of this error, or maybe there is another method for quick calculations of directions?
Title: Re: Unstable external function
Post by: Mr_Conflicts on Feb 17, 17, 10:22:52 AM
I would recommend looking at this (http://hewiki.heroengine.com/wiki/Vector_functions) wiki page to see all the vector related functions provided in Hero Engine. I would use the vector length as the wiki example shows to get distance.

Code: [Select]
point1 as Vector3 = myPos
point2 as Vector3 = tgtPos
dist as float = VectorLength(point1 - point2)

As for the error, I am not sure  as to what the error message means, but maybe its because you are  using the same input value as the output value. (I am not 100% on this but its just a guess, hopefully someone can confirm or deny this.)
Title: Re: Unstable external function
Post by: FI-ScottZ on Feb 17, 17, 10:24:39 AM
A guess, but maybe that happens when your tgtPos and myPos are the same, leading to a vector that is all zeroes and thus can't be normalized.
Title: Re: Unstable external function
Post by: nocake on Feb 17, 17, 02:14:03 PM
A guess, but maybe that happens when your tgtPos and myPos are the same, leading to a vector that is all zeroes and thus can't be normalized.

most likely this. I had this problem as well when I first didnt exclude my character so my ray was hitting the character at the exact spot it started.

One thing i have learned about HSL is you should always check for == 0 and leave yourself a scripterrorandcontinue() then return.

You can always come back to it and now you have a nice little note.
Title: Re: Unstable external function
Post by: GlorianLanTarini on Feb 17, 17, 11:21:16 PM
As for the error, I am not sure  as to what the error message means, but maybe its because you are  using the same input value as the output value. (I am not 100% on this but its just a guess, hopefully someone can confirm or deny this.)

Nope, it can't be a reason, because I take this maneuver from clean engine script, where it work fine.

And it work fine for me, when I use it ones. But, also I use it repeatedly in projectile chasing target mechanic.
A guess, but maybe that happens when your tgtPos and myPos are the same, leading to a vector that is all zeroes and thus can't be normalized.
most likely this. I had this problem as well when I first didnt exclude my character so my ray was hitting the character at the exact spot it started.

One thing i have learned about HSL is you should always check for == 0 and leave yourself a scripterrorandcontinue() then return.

You can always come back to it and now you have a nice little note.

As I say, I get this error (some time massively) when projectile chasing target. And I has check - chasing is done when position is same. But still get errors. I should dig it some more. Maybe change something in projectile's mechanic.

Anyway, thanks for replies.
Title: Re: (resolved)Unstable external function
Post by: GlorianLanTarini on Feb 18, 17, 01:31:02 AM
I change this code

Code: [Select]
if myPos = tgt_pos
  me.ChasingComplete()
.

to this

Code: [Select]
if myPos = tgt_pos
  me.ChasingComplete()
  return
.

and error is stopped.

I have destructor in method ChasingComplete(), so I thought "return" isn't necessary. Sadly I was wrong.
Title: Re: (resolved)Unstable external function
Post by: Thazager on Feb 19, 17, 01:44:21 PM
I have found need of "return" at times also. The best place to check is when the section of code is inside a function and the function continues with more code after. Some times it can be a loop. One place its not really needed would be at the end of the function itself, unless that function was called from another and code follows after.
Title: Re: (resolved)Unstable external function
Post by: nocake on Feb 19, 17, 03:04:53 PM
Return ends a function and returns to the caller.

Having return in ChasingComplete() stops that function and returns it to the caller which in this case is the next line of the if statement.