Proper way to retrieve player position?

Place to get help with not working mods / modding interface.
Solzhenitsyn
Manual Inserter
Manual Inserter
Posts: 1
Joined: Fri Dec 23, 2016 11:33 pm
Contact:

Proper way to retrieve player position?

Post 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.
Articulating
Long Handed Inserter
Long Handed Inserter
Posts: 71
Joined: Mon Oct 17, 2016 10:33 am
Contact:

Re: Proper way to retrieve player position?

Post 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.
Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Proper way to retrieve player position?

Post by Nexela »

Also be aware that placing water under the player is the fastest way to the game over screen.
Post Reply

Return to “Modding help”