nil value by acces to global variable

Place to get help with not working mods / modding interface.
derbobo
Manual Inserter
Manual Inserter
Posts: 3
Joined: Sun Jun 05, 2016 1:58 pm
Contact:

nil value by acces to global variable

Post 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

User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: nil value by acces to global variable

Post 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?
Last edited by Adil on Tue Jun 07, 2016 8:21 pm, edited 1 time in total.
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.

User avatar
DedlySpyder
Filter Inserter
Filter Inserter
Posts: 253
Joined: Fri Jun 20, 2014 11:42 am
Contact:

Re: nil value by acces to global variable

Post by DedlySpyder »

Adil wrote:Also these: why don't you use these in compound names "_"?
Or use camelCaseNames

User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: nil value by acces to global variable

Post 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.
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.

derbobo
Manual Inserter
Manual Inserter
Posts: 3
Joined: Sun Jun 05, 2016 1:58 pm
Contact:

Re: nil value by acces to global variable

Post 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 ;)

User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: nil value by acces to global variable

Post by Adil »

What?
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.

User avatar
ArderBlackard
Long Handed Inserter
Long Handed Inserter
Posts: 74
Joined: Thu May 05, 2016 12:41 pm
Contact:

Re: nil value by acces to global variable

Post by ArderBlackard »

derbobo wrote: I know but when the table is empty the for statement will never stop ;)
I swear it will
Gib dich hin bis du Glück bist

Post Reply

Return to “Modding help”