# Get turf position

## Recommended Posts

Hey guys

I'm trying to write a piece of code that will build a wall around a turf tile once it's placed. But the problem is, that it depends on where the mouse was clicked (so the wall can have different positions even if the tile is the same, but mouse was clicked in different corners of it).

I get coordinates from the pt variable, how can I get the coordinates of the turf block under that point?

This is the code that builds the wall:

`local function ondeployturf(inst, pt, deployer)    -- ...    local x = math.floor(pt.x) - 3.5    local z = math.floor(pt.z) - 3.5    for i=1,7,1        do            for j=1,7,1                do                    if i == 1 or i == 7 or j == 1 or j == 7 then                        local wall = GLOBAL.SpawnPrefab("wall_wood")                        wall.Physics:Teleport(x + i, 0, z + j)                    end            end    endend`

Thanks a lot!

##### Share on other sites

You can use Transform:GetWorldPosition() on the turf instance.

##### Share on other sites

You can use Transform:GetWorldPosition() on the turf instance.

I don't think so... I don't think ground tiles count as instances.

This probably implies modulo mathemagic. You know, the x%4 thing that returns something 0-3 (base four).

EDIT: Besides, those for loops are cringeworthy. Sorry, but it's really badly done. Change the third number to 7, so it begins with 1 and skips straight to 7 next turn. EDIT2: I'm an idiot when I am tired. Don't judge.

Edited by Mobbstar
##### Share on other sites

I don't think so... I don't think ground tiles count as instances.

This probably implies modulo mathemagic. You know, the x%4 thing that returns something 0-3 (base four).

EDIT: Besides, those for loops are cringeworthy. Sorry, but it's really badly done. Change the third number to 7, so it begins with 1 and skips straight to 7 next turn.

So is a turf placed every 4 positions? I'll try to use that to calculate.

Which third number, the increment? But then it won't do what I need, I need it to loop 7 times, to place walls along the sides, not just the corners (what will happen if I jump straight from 1 to 7)

##### Share on other sites

You can use Transform:GetWorldPosition() on the turf instance.

Did not work

I don't think so... I don't think ground tiles count as instances.

This probably implies modulo mathemagic. You know, the x%4 thing that returns something 0-3 (base four).

EDIT: Besides, those for loops are cringeworthy. Sorry, but it's really badly done. Change the third number to 7, so it begins with 1 and skips straight to 7 next turn.

Tried various things with

`x = x - (x % 4)z = z - (z % 4)`

But did not get what I wanted :/ It seems to align the walls with the turf tiles, but rarely puts it on the correct tile, usually the tile next to it depending on the position where it was clicked to place a turf (within the tile).

Is there a way to see x/z position of the mouse or the player in-game?

Edited by Hoit
##### Share on other sites

Got it to work by rounding to nearest 4s, thanks for help guys

`-- Round to nearest 4s    local x = math.floor((pt.x / 4) + .5) * 4    local z = math.floor((pt.z / 4) + .5) * 4`
##### Share on other sites

I don't think so... I don't think ground tiles count as instances.

This probably implies modulo mathemagic. You know, the x%4 thing that returns something 0-3 (base four).

EDIT: Besides, those for loops are cringeworthy. Sorry, but it's really badly done. Change the third number to 7, so it begins with 1 and skips straight to 7 next turn.

I admit I read too fast the stuff and the make_turf is actually for the inventory item.

EDIT: but I think you would have been able to retrieve the coordinates without redoing the rounding and the modulo like the way Klei did it in the ondeploy function of the turf inventory item with the GetTileCoordsAtPoint function

Edited by ZupaleX
##### Share on other sites

I admit I read too fast the stuff and the make_turf is actually for the inventory item.

EDIT: but I think you would have been able to retrieve the coordinates without redoing the rounding and the modulo like the way Klei did it in the ondeploy function of the turf inventory item with the GetTileCoordsAtPoint function

As in use this one?

`local x, y = map:GetTileCoordsAtPoint(pt:Get())`

I tried, but it gives me completely different coordinates (last time I was 0,0,0 at world position but this returned me 178/178 or something similar).

##### Share on other sites

I did not test it out, just read the function and made a guess (thus the "I think" )