Community Forum

Get field at position (not farmland)

Forum Overview >> Scripting

Created19.03.2022 10:32

Dirk Hameier (DirkH42) 19.03.2022 10:32
I know the funtion to get the Farmland (g_farmlandManager:getFarmlandIdAtWorldPosition) but I need a method to get the exact field, especially if the farmland has more than one field.
Thx a lot

Michael T. (mx11de) 21.03.2022 18:46

local isOnField, _ = FSDensityMapUtil.getFieldDataAtWorldPosition(wx, wy, wz)
if isOnField then
return true

Dirk Hameier (DirkH42) 24.03.2022 18:01
Thank you very much, I will try it out right away.

Dirk Hameier (DirkH42) 26.03.2022 19:08
Unfortunately, the function does not help with my problem. It only returns the info if I am standing on a field, but not on which one.

Colin Smith (WrinkleysRule) 26.03.2022 20:16
Have you looked in the debugger dataS scripts under fields ---Field.Lua

Loki Laufeyson (loki_79) 27.03.2022 21:27
It seems like there is no in-game function to obtain the field id. I had a look in CoursePlay, and they have written a function to get the id of the owned plot of land, but not the specific field id.

Jon Sage (jtsage) 28.03.2022 03:57
There isn't a base game function for it, so far as I've found.

The important bit is line 332 on. Courtesy somewhat of HappyLooser on his VehicleInspector mod, with some changes by me. Keep in mind, it is not terrible accurate at field edges, and even less so when you join fields together.

Michael T. (mx11de) 29.03.2022 12:22
In the Fieldmanager.lua at line 130 is a part with an connection from field to farmland.
You could try to use the table farmlandIdFieldMapping

for i, field in ipairs(self.fields) do
local posX, posZ = field:getCenterOfFieldWorldPosition()
local farmland = g_farmlandManager:getFarmlandAtWorldPosition(posX, posZ)
if farmland ~= nil then

if self.farmlandIdFieldMapping[] == nil then
self.farmlandIdFieldMapping[] = {}

table.insert(self.farmlandIdFieldMapping[], field)
Logging.error("Failed to find farmland in center of field '%s'", i)

Note: Log in to post. Create a new account here.