Position Question

Place to get help with not working mods / modding interface.
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Position Question

Post 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.
orzelek
Smart Inserter
Smart Inserter
Posts: 3928
Joined: Fri Apr 03, 2015 10:20 am
Contact:

Re: Position Question

Post 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().
User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: Position Question

Post 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}
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Position Question

Post 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.
Post Reply

Return to “Modding help”