Page 1 of 1

Need scripting help to find error

Posted: Sun Nov 18, 2018 10:27 am
by MrFactorio
Hello,

I want to detect the nearest player_port location as an attack location for my biters. I copied some code from the waves mod and now factorio says that "attempt to call field 'insert' (a nil value)". Here is the modified function from waves mod:

Code: Select all

function find_nearest_playerport(tbl)
	local surface = tbl.surface
	local position = tbl.position
	local max_distance = tbl.max_distance
	local units = surface.find_entities_filtered{area={{position.x-max_distance,position.y-max_distance},{position.x+max_distance,position.y+max_distance}}, type = "player-port"}
	local shortest = 9999999
	local return_loc = nil
	for _,unit in pairs(units) do
		local unit_loc = unit.position
		local unit_dist = to_distance(position,unit_loc)
		if unit_dist < shortest then
			return_loc = unit_loc
			shortest = unit_dist
		end
	end
	game.players[1].print(return_loc)
	return return_loc
end
and the function which calls the function above is:

Code: Select all

script.on_event(defines.events.on_tick, function (event)
	local onetimer = true
	if event.tick%10 == 0 and event.tick > 10 and onetimer
		then	
			global.playerstartlocations = {}
			global.playerstartlocations.insert(find_nearest_playerport{surface = game.surfaces[1], position = {x=0,y=0}, max_distance = 3000})	
			player.print(global.playerstartlocations)
			onetimer = false
		end
end
The error happens at the line
"global.playerstartlocations.insert(find_nearest_playerport{surface = game.surfaces[1], position = {x=0,y=0}, max_distance = 3000})"

Can anyone help me? What parameter is nil? I am generating the table and all parameters are exactly like in the waves mod :(. I have no clue...
Thank you!

Re: Need scripting help to find error

Posted: Sun Nov 18, 2018 10:35 am
by darkfrei
Try with simple

Code: Select all

Local surface = game.surfaces[1] 
If surface then
And

Code: Select all

Local nearest_playerport = find_nearest_playerport (...)
If nearest_playerport then
And

Code: Select all

table.insert ( table, inserting)

Re: Need scripting help to find error

Posted: Sun Nov 18, 2018 11:20 am
by MrFactorio
Ok I did that.

I have added a line that tells me if surface is empty or not and it IS empty. But why?

I thought maybe at tick 0 the surface is not generated at all so I added a condition

Code: Select all

if event.tick%10 == 0 and event.tick > 10 and onetimer
but it does not help. Surface is null even after tick > 10

Re: Need scripting help to find error

Posted: Sun Nov 18, 2018 11:35 am
by darkfrei
MrFactorio wrote:
Sun Nov 18, 2018 11:20 am
I have added a line that tells me if surface is empty or not and it IS empty. But why?
May be some another mod has change it. With this code you can take first surface, but if not available, than surface "nauvis", if it also not available, then the surface of the first player.

Code: Select all

local surface = game.surfaces[1] or game.surfaces['nauvis'] or game.players[1].surface

Re: Need scripting help to find error

Posted: Sun Nov 18, 2018 11:56 am
by MrFactorio
edit

Re: Need scripting help to find error

Posted: Sun Nov 18, 2018 12:26 pm
by MrFactorio

Code: Select all

script.on_event(defines.events.on_tick, function (event)
	local onetimer = true
	local player = game.players[1]
	if event.tick%10 == 0 and event.tick > 10 and onetimer
		then	
			global.playerstartlocations = {}
			local localsurface = game.surfaces[1] or game.surfaces['nauvis'] or game.players[1].surface
			
			if localsurface then
				
				local nearest_playerport = game.players[1].position
				player.print(nearest_playerport)
				if global.playerstartlocations then
					if nearest_playerport then
						global.playerstartlocations.insert(nearest_playerport)	
					else 
						player.print("nearest_playerport is empty")
					end
				else
					player.print("global.playerstartlocations is empty")
				end
			else 
				player.print("surface is empty")
			end

			onetimer = false
		end
end
)
now it says that in line "global.playerstartlocations.insert(nearest_playerport)" attempt to insert a nil but how can that be? I check every condition before. Nearest_playerport is not empty and global.playerstartlocations has been created some lines above :(.

Lua developement sucks so much without intellisense and debugging options... biggest respect for you guys!

Re: Need scripting help to find error

Posted: Sun Nov 18, 2018 12:44 pm
by MrFactorio
Ok this works:

Code: Select all

table.insert(global.playerstartlocations, nearest_playerport)
(at least for the error described above)

Re: Need scripting help to find error

Posted: Sun Nov 18, 2018 2:12 pm
by MrFactorio

Code: Select all

if global.playerstartlocations[1] then
	unit.set_command{type = defines.command.attack_area, global.playerstartlocations[1], radius = 1500}
 else
	game.player[1].print("global.playerstartlocations[1] is null")
end
the second line always says "table expected got nil". Why?

Re: Need scripting help to find error

Posted: Sun Nov 18, 2018 2:44 pm
by darkfrei
MrFactorio wrote:
Sun Nov 18, 2018 2:12 pm

Code: Select all

if global.playerstartlocations[1] then
	unit.set_command{type = defines.command.attack_area, global.playerstartlocations[1], radius = 1500}
 else
	game.player[1].print("global.playerstartlocations[1] is null")
end
the second line always says "table expected got nil". Why?
What is unit.set_command? Why whole table looks like unit.set_command{type=area, position, radius=radius}

Command game.print("global.playerstartlocations[1] is null") is also better.