[LUA] Position isn't accepting even tough it's correct?

So first lemme give you da code

for k,spawnLocation in pairs(spawns) do
	ply:PrintMessage( HUD_PRINTTALK, "Checking every spawn position in data... Current positon: " .. tostring(spawnLocation) )
	if spawnLocation == br_spawn:GetPos() then
		ply:PrintMessage( HUD_PRINTTALK, "Found exact position in data! Removing..." )
		spawns[k] = nil
		ply:PrintMessage( HUD_PRINTTALK, "Spawnpoint removed." )
		ply:PrintMessage( HUD_PRINTTALK, "Wrong position..." )

And the problem is when comparing position of the “br_spawn” with “spawnLocation” it just doesn’t let through, the code underneath isn’t ran. And the message “Wrong position…” is shown…

ply:PrintMessage( HUD_PRINTTALK, "Wrong position..." )

if spawnLocation == br_spawn:GetPos() then

Why is that happening?


That means br_spawn’s positions match none of the spawn locations.

That I know well I don’t know because when I edit something and undo It’ll work. But when I reload it won’t I am confused.

Are you storing the spawns to a file or something? Reloading the game resets the Lua state so tables will be cleared.

I am using my friend’s JSON wrapper. If I think how it works is that it’ll convert a table into a json table and then save it.

EDIT: I am not restarting the game but changing map.

Changing the map is the same. Print out the table once you’ve loaded to make sure the JSON was parsed correctly.

It is loading correctly as I am printing the value of it here:

ply:PrintMessage( HUD_PRINTTALK, "Checking every spawn position in data... Current positon: " .. tostring(spawnLocation) )

and the position is a normal vector.

The only way it would not pass is if br_spawn’s pos doesn’t match any of the vectors. Print out all the vectors and br_spawn’s in the beginning to check.

Spawn position: -1131.970215 928.084961 1543.539307
Checked position: -1131.970215 928.085022 1543.539307



Alternatively, you can use this method if you want to check if the vectors are “equal” within a margin of error:


You can use Vector1:Distance(Vector2) <= r (where r = 5 for example)

Vector:IsEqualTol is a cheaper way to achieve the same thing, and it’s more readable, and it saves a square-root operation.

Vector:Distance calculates the distance using the square root function, unnecessary when you are only using the result as a threshhold. If you really want to go down the road of checking it that way, use

Vector:DistToSqr, however code_gs’ solution is the correct one.

[editline]1st August 2017[/editline]

I spent so long writing that post due to distractions that I got ninja’d by a whole 15 minutes.

Can I ask how to use that? I mean I don’t know what to put at the second argument.

if ( spawnLocation:IsEqualTol( br_spawn:GetPos(), 0.5 ) ) then
– Found exact point in data stuff
– other stuff

I mean what is the second argument? Like the maximum vector can be different than this vector?

next time look it up by yourself please.

Dude… I searched it and didn’t knew what it meant. Or I didn’t knew what the word meant…

It’s tolerance. It means that it will accept values in a range of ± the second arg.