How to limit searched for path length

Actually. In your case it is probably better to do a check against the F score instead.
The F score is the estimated total length of the path (and is usually calculated as G score + straight line distance to the destination).
Assuming your map does not contain any magic portals or things like that, it should work better than the G score and it does not have the same potential performance regression as the check with the G score does.

I have edited the code in my previous answer.