Page 1 of 1

Proper way to retrieve player position?

Posted: Fri Dec 23, 2016 11:37 pm
by Solzhenitsyn
Event handler

Code: Select all

script.on_event(defines.events.on_gui_click, function(event)
	if event.element.name == "ocean-factory-button" then 
		player = game.players[event.player_index] 
		place_water(player)
	end
end)
Place water

Code: Select all

function place_water(player)
	position = {
		x = player.position.x, 
		y = player.position.y-1
	}
	player.print(position[1]) -- debug
	water_tiles = {
		{
			name="water",
			position
		}
	}
	player.surface.set_tiles(water_tiles)
end
When place_water runs, position returns nil. What is the proper way to retrieve the player's current location? Thank you.

Re: Proper way to retrieve player position?

Posted: Sat Dec 24, 2016 12:10 am
by Articulating
That is the proper way. You are trying to print position[1], but position only has x and y indexed. Try printing position.x or position.y and you should see the correct values. In the water_tile table, you need to do position=position, like you did with the name.

Re: Proper way to retrieve player position?

Posted: Sat Dec 24, 2016 2:25 am
by Nexela
Also be aware that placing water under the player is the fastest way to the game over screen.