Page 1 of 1

Position Question

Posted: Tue Jul 12, 2016 1:47 am
by TheSAguy
Hi,

I'm having trouble defining a new position from an existing position.
Below is what I'd like to accomplish:

I have an existing position, now I want to create a new position that's 0.5 from that position, if the new position is valid. create an entity:

Code: Select all

local position = entity.position          -- Existing position I have  
local new_position = {x=position + 0.5, y=position + 0.5} -- New position I'm trying to define
if new_position.valid then -- confirm it's a valid position to build something, not water or something else already there.
--create entity goes here.
end
Can someone please help me with this.
Thanks.

Re: Position Question

Posted: Tue Jul 12, 2016 6:27 am
by orzelek
Hmm position.valid ?
I'm not sure what it does.
To check if you can place stuff there you need to use this:

Code: Select all

surface.can_place_entity{name = rname, position = {x = newX,y = newY}}
You can also use get_tile() to read tile on given position and check for water collision using collides_with().

Re: Position Question

Posted: Tue Jul 12, 2016 7:00 am
by prg
position is a table, you need to add the offset to the individual coordinates.

Code: Select all

local new_position = {x=position.x + 0.5, y=position.y + 0.5}

Re: Position Question

Posted: Tue Jul 12, 2016 12:57 pm
by TheSAguy
prg wrote:position is a table, you need to add the offset to the individual coordinates.

Code: Select all

local new_position = {x=position.x + 0.5, y=position.y + 0.5}
Thanks prg, I just could not get this syntax correct.
orzelek wrote:Hmm position.valid ?
I'm not sure what it does.
To check if you can place stuff there you need to use this:

Code: Select all

surface.can_place_entity{name = rname, position = {x = newX,y = newY}}
You can also use get_tile() to read tile on given position and check for water collision using collides_with().
position.valid is not right, I know, I was just trying to describe what I was trying to accomplish.
So I'm not used can_place_entity before. I'll give that a try.
Thanks.