Page 1 of 1

Cannot access global 'script' (Solved)

Posted: Sun Mar 27, 2016 9:36 pm
by BarkerZA
Hi all,

pretty new here but I've really been enjoying the modding aspect.

I have a problem. I'm trying to access script.on_init so I can save a global variable but I keep getting the error " attempt to index global 'script' (a nil value)"

Here is my code:

Code: Select all

--[[ include used code ]]
require "util"
require "defines"


script.on_init(function()
	glob.combined = {}
end)

game.on_event(defines.events.on_player_created, function()
	game.player.insert({name="small-electric-pole", count =100})
end)

game.on_event(defines.events.on_built_entity, function(event)
	if event.created_entity.name == "small-electric-pole" then
      local newpole = event.created_entity
      local newlight = game.get_surface(1).create_entity{name="small-lamp", position = newpole.position, force=game.forces.player}
      
      table.insert(glob.combined,{pole,light})

      i = 0
      for k,v in pairs(glob.combined) do
         i = i + 1
      end

      glob.combined[i].pole = newpole
      glob.combined[i].light = newlight
    end
end)

game.on_event(defines.events.on_preplayer_mined_item, function(event)
   if (event.entity.name == "small-lamp") then
      for k,v in pairs(glob.build) do
         if (event.entity.equals(v.drill)) then
            v.pole.destroy()
         end
      end
   end
end)
I've been racking my brain but cannot figure out whats going wrong. I'd appreciate any help.

Re: Cannot access global 'script'

Posted: Sun Mar 27, 2016 9:45 pm
by Impatient
strange

this is the code i use in control.lua and it works:

Code: Select all

require "defines"
require "utils"

script.on_init(function() handleOnInit() end)
script.on_load(function() handleOnLoad() end)
.
.
.
i assume the reason must be outside your control.lua. is the code you posted in your control.lua? that kind of code has to be in control.lua.

Re: Cannot access global 'script'

Posted: Sun Mar 27, 2016 9:56 pm
by BarkerZA
Yep it is within control.lua - would any external files effect it ? I have a couple custom entities/equipment. They all work fine without my control code. In fact everything works great without the script.on_init

Re: Cannot access global 'script'

Posted: Sun Mar 27, 2016 10:14 pm
by Impatient
maybe something earlier causes "script" in control.lua not to hold the reference to an instance of LuaBootstrap. something that does not throw an error message. i beleive data.lua is parsesd earlier. so yes. maybe have a look at the factorio log files. maybe there is a hint on the cause.

Re: Cannot access global 'script'

Posted: Sun Mar 27, 2016 10:16 pm
by Choumiko
Which Factorio version are you running? script.on_load is 0.12.12+ (or 0.12+ ?) Your use of glob instead of global indicates Factorio 0.11, since glob got changed to global in 0.12

I would recommend at least 0.12.27, the last version which fixed a bug with modding iirc (textfiled related)

Re: Cannot access global 'script'

Posted: Mon Mar 28, 2016 1:23 pm
by BarkerZA
Im using v 0.12.3

Re: Cannot access global 'script'

Posted: Mon Mar 28, 2016 1:59 pm
by Choumiko
That's where you'd have to use game.on_load() etc.
I highly recommend updating Factorio, as everything that's working in your version won't work for for versions > 0.12.10 (see the 0.12.11 release notes: viewtopic.php?f=3&t=17087)

Re: Cannot access global 'script'

Posted: Mon Mar 28, 2016 3:55 pm
by BarkerZA
Thanks so much. I was being a little stupid thinking I had version 0.12.30 instead of 0.12.3 for some reason. Upgrading sorted this out.