Team Spartan Cookies & Milk Forums
  • Issue Tracker
  • Donations
  • Search
  • Member List
  • Help

Register

Team Spartan Cookies & Milk Forums Garry's Mod Starfall v
1 2 3 4 5 … 18 Next »
Questions and Documentation Requests
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Linear Mode
Questions and Documentation Requests
08-05-2015, 08:06 PM,
#2
Person8880 Offline
gpj.ecafllorT
*******
Posts: 2,407
Threads: 45
Joined: Sep 2013
RE: Questions and Documentation Requests
I'm going to start by pre-emptively explaining input validation in Starfall.

There's 3 stages to input validation:
  1. Object validation check.
  2. Function argument type check.
  3. Function argument value check.

1. Object validation check
Objects that exist in the game world, e.g entities, are first checked to make sure that the entity object you're running a function on still exists in the game world. If they do not, then an error of the form "Attempted to use an invalid Entity" is thrown.

How to make sure you don't hit this error
Check entity objects you pass around to make sure they're valid using the global function IsValid().
if IsValid( Ent ) then
print( Ent:pos() )
end

2. Function argument type check
Once the object is verified as valid (or if it's a library function), the next check is on the type of value being passed through. A function that expects a Vector will throw an error if the argument is not a Vector, and so on.

How to make sure you don't hit this error
You should already be aware of what types you're passing round. The most common type check failure will be passing nil instead of the expected value. You can avoid this by making sure your input isn't nil, and if it's an entity, then it's also valid.
if IsValid( Ent ) and Vec then
EntConfusedetPos( Vec )
end

There shouldn't be any need to verify the type using the type() function, but if you're absolutely unsure of the type going through, you can check it yourself first with:
if type( Vec ) ~= "Vector" then return end

3. Function argument value check
The last stage is only currently applicable to numbers, Vectors and Angles. This check makes sure that the number, or components of the Vector/Angle are not NaN. NaN comes around from division by 0 or other forms of bad input being passed around (such as some vector operations like Vector:GetNormal() and Vector:Normalize(), which divide by the length of the vector).

How to make sure you don't hit this error
Make sure you're not ever dividing by 0. If you're dealing with forces or other vectors that may become large, make sure you clamp the size of the vector so that it does not end up overly large.

To clamp a vector's magnitude:
-- Takes the Vector Vec, and clamps its magnitude between the Min and Max values.
-- Returns Vec after modifying it.
local function VectorClamp( Vec, Min, Max )
local CurrentLength = Vec:Length()
local NewLength = math.clamp( CurrentLength, Min, Max )
if CurrentLength == NewLength then return Vec end

Vec:Normalize()
Vec:Mul( NewLength )

return Vec
end

You can also directly check for NaN values using the standard check:
local function IsNaN( Value )
if type( Value ) == "number" then
return Value ~= Value
end

for i = 1, 3 do
if Value[ i ] ~= Value[ i ] then
return true
end
end

return false
end
But in practise you should not have NaN values lying around.
Find
Reply
« Next Oldest | Next Newest »


Messages In This Thread
Questions and Documentation Requests - by Person8880 - 08-05-2015, 07:38 PM
RE: Questions and Documentation Requests - by Person8880 - 08-05-2015, 08:06 PM
RE: Questions and Documentation Requests - by Liizzii - 05-02-2016, 01:00 PM
RE: Questions and Documentation Requests - by Person8880 - 05-02-2016, 06:37 PM
RE: Questions and Documentation Requests - by Sir_Cuddles - 19-04-2019, 03:47 PM
RE: Questions and Documentation Requests - by Person8880 - 19-04-2019, 04:53 PM
RE: Questions and Documentation Requests - by Sir_Cuddles - 19-04-2019, 05:11 PM
RE: Questions and Documentation Requests - by Person8880 - 19-04-2019, 06:58 PM
RE: Questions and Documentation Requests - by Sir_Cuddles - 19-04-2019, 07:06 PM

  • View a Printable Version
Forum Jump:


Users browsing this thread: 1 Guest(s)
Contact UsTeam Spartan Cookies & MilkReturn to TopLite (Archive) ModeMark all forums readRSS Syndication

Powered By MyBB, © 2002-2026 MyBB Group.