Page 1 of 1

scripts.on_event not working

Posted: Sat Jul 02, 2016 7:50 am
by kingarthur
im trying to set up a script that will upon an entity's death create an entity at that position. i think im close to getting it to work but im having issues with factorio telling me (attempt to index gobal 'script' a nil value). im not sure what is wrong with it as any references to script.on_event look the same as what i did.

here is the code

Code: Select all

local oretest = {}

script.on_event(defines.events.on_entity_died, function(event)
  if event.entity.name == "small-biter" then
  local smallbiter = event.entity
  local targetpos = smallbiter.surface.find_non_colliding_position("copper-ore", smallbiter.position, 10, 1)
	end
	
	local tore = player.surface.create_entity({
         name = "copper-ore", 
         position = targetPos
         })
		 end)

Re: scripts.on_event not working

Posted: Sat Jul 02, 2016 9:50 am
by prg
"script" shouldn't be the problem here, if that's nil... are you actually doing this in control.lua, not data.lua? Or are you using some ancient version of Factorio?

Actual problems:
  • You're defining targetpos as a local variable in an if block, then try to use it outside that block where it's not in scope anymore.
  • Lua is case sensitive. You're defining targetpos, then try to use it as targetPos.
  • You're trying to index player, which isn't defined anywhere. Using the entity that died here would make more sense.

Re: scripts.on_event not working

Posted: Sat Jul 02, 2016 10:13 am
by kingarthur
i was not doing it in control.lua or data.lua. i was trying to it inside entity.lua in the prototypes folder of the mod. the player reference was a mistake, as i copyed an example code and forgot to fix it. thanks

Re: scripts.on_event not working

Posted: Sat Jul 02, 2016 10:17 am
by kingarthur
now it works perfectly. thank you

Re: scripts.on_event not working

Posted: Sat Jul 02, 2016 10:26 am
by prg
kingarthur wrote:i was not doing it in control.lua or data.lua. i was trying to it inside entity.lua in the prototypes folder of the mod.
That counts as data.lua, since all those prototype definitions are included from there.

Re: scripts.on_event not working

Posted: Sat Jul 02, 2016 10:31 am
by kingarthur
oh. well that will be good to know moving forward