Cannot access global 'script' (Solved)

Place to get help with not working mods / modding interface.
Post Reply
BarkerZA
Burner Inserter
Burner Inserter
Posts: 6
Joined: Sun Mar 27, 2016 9:30 pm
Contact:

Cannot access global 'script' (Solved)

Post 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.
Last edited by BarkerZA on Mon Mar 28, 2016 3:55 pm, edited 1 time in total.

User avatar
Impatient
Filter Inserter
Filter Inserter
Posts: 883
Joined: Sun Mar 20, 2016 2:51 am
Contact:

Re: Cannot access global 'script'

Post 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.

BarkerZA
Burner Inserter
Burner Inserter
Posts: 6
Joined: Sun Mar 27, 2016 9:30 pm
Contact:

Re: Cannot access global 'script'

Post 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

User avatar
Impatient
Filter Inserter
Filter Inserter
Posts: 883
Joined: Sun Mar 20, 2016 2:51 am
Contact:

Re: Cannot access global 'script'

Post 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.

Choumiko
Smart Inserter
Smart Inserter
Posts: 1352
Joined: Fri Mar 21, 2014 10:51 pm
Contact:

Re: Cannot access global 'script'

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

BarkerZA
Burner Inserter
Burner Inserter
Posts: 6
Joined: Sun Mar 27, 2016 9:30 pm
Contact:

Re: Cannot access global 'script'

Post by BarkerZA »

Im using v 0.12.3

Choumiko
Smart Inserter
Smart Inserter
Posts: 1352
Joined: Fri Mar 21, 2014 10:51 pm
Contact:

Re: Cannot access global 'script'

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

BarkerZA
Burner Inserter
Burner Inserter
Posts: 6
Joined: Sun Mar 27, 2016 9:30 pm
Contact:

Re: Cannot access global 'script'

Post 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.

Post Reply

Return to “Modding help”