Page 1 of 1

nil value by acces to global variable

Posted: Tue Jun 07, 2016 7:21 pm
by derbobo
Hello,
I have problem with a mod i try to make , beacaue everytime when i try to run a function called add_newelementextension i´ll get this error message:"control.lua:23: attempt to index field 'taxiextension' (a nil value)"

here is my code:

Code: Select all

require "util"
require "defines"
script.on_load(function()
if global.taxiextension ==nil then 
global.taxiextension={}
end

 
end)

function add_newelementextension(name)
local taxi
		for chunk in game.get_surface(1).get_chunks() do
			local entities = game.get_surface(1).find_entities_filtered{backer_name=name, area={{chunk.x*32, chunk.y*32}, {(chunk.x+1)*32, (chunk.y+1)*32}}}
				for _, entity in pairs(entities) do
					if entity.name == "taxi" then
					 taxi=entity
					 end
				end
		end
		if taxi~=nil then 
			local a={}
			for i=0, global.taxiextension.getn, 1 do
				if i ~=0 then
				a[i]= global.taxiextension[i]
				end
			end
			local t =global.taxiextension.getn + 1
			a[t]={key=taxi.backer_name,val ={caller=player,available=true}}
		end
end

Re: nil value by acces to global variable

Posted: Tue Jun 07, 2016 7:31 pm
by Adil
On_load is called on savegame load only.
Add a print statement to a line 4 for example and you'll see that nothing is printed when you start a new game.
Also these: why don't you use more these of these: "_" in compound names?

Re: nil value by acces to global variable

Posted: Tue Jun 07, 2016 8:12 pm
by DedlySpyder
Adil wrote:Also these: why don't you use these in compound names "_"?
Or use camelCaseNames

Re: nil value by acces to global variable

Posted: Tue Jun 07, 2016 8:20 pm
by Adil
But undrescores are Factorio devteam standard :P

Also, getn is deprecated lua has # now:

Code: Select all

for i=1, #global.taxiextension do
And by default tables are indexed from 1.

Re: nil value by acces to global variable

Posted: Tue Jun 07, 2016 9:03 pm
by derbobo
Adil wrote: And by default tables are indexed from 1.
I know but when the table is empty the for statement will never stop ;)

Re: nil value by acces to global variable

Posted: Tue Jun 07, 2016 9:09 pm
by Adil
What?

Re: nil value by acces to global variable

Posted: Tue Jun 07, 2016 11:19 pm
by ArderBlackard
derbobo wrote: I know but when the table is empty the for statement will never stop ;)
I swear it will