Error -1 while running the event handler

Place to get help with not working mods / modding interface.
ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

Error -1 while running the event handler

Post by ficolas »

I was testing my mod, and everything worked fine, but then I went to the forum to post some things, and I left the game "afk", and then, when I opened the game to close it, I saw "Error -1 while running the event handler", then I pressed Ok, and it sended me to the main menu, I created a new world, and it said "Error -1 while running the event handler", I closed the game, and opened it again, and it worked fine for some time, then, "Error -1 while running the event handler"!! D:

Here is the code:

Code: Select all

require "util"
require "story"
require "defines"

game.oninit = function()
end

game.onevent = function(event)
  if event.name == "ontick" then
    if glob.introduction == nil then
      glob.introduction = true
	  glob.undergroundMinerPos={}
      game.getplayer().insert{name="iron-plate", count=8}
      game.getplayer().insert{name="pistol", count=1}
      game.getplayer().insert{name="basic-bullet-magazine", count=10}
	
    end
    if game.getplayer().getshiplandinginprogress() then
      local timeleft = game.getplayer().gettimetoland()
      if timeleft == 0 then
        game.setgamestate{gamefinished=true, playerwon=true}
      end
      if glob.untilnextattack == nil then
        glob.untilnextattack = 1
      end
      local secondsleft = math.floor(timeleft / 60)
      local minutes = math.floor((secondsleft)/60)
      local seconds = math.floor(secondsleft - 60*minutes)
      game.getplayer().setgoaldescription("Time until the fleet lands: " .. string.format("%d:%02d", minutes, seconds), true)
      glob.untilnextattack = glob.untilnextattack - 1
      if glob.untilnextattack == 0 then
        game.setmulticommand({type=defines.command.attack,
                             target=game.getplayer(),
                             distraction=defines.distraction.byenemy},
                             10)
        glob.untilnextattack = 60 * 10
      end
    end
    if glob.untilnextattacknormal == nil then
      glob.untilnextattacknormal = 60 * 60 * 60
      glob.attackcount = 5
    end
    glob.untilnextattacknormal = glob.untilnextattacknormal - 1
    if glob.untilnextattacknormal <= 0 then
      glob.untilnextattacknormal = 60 * 60 * 4 + game.getrandomnumber() * 60 * 60 * 10
      game.setmulticommand({type=defines.command.attack,
                             target=game.getplayer(),
                             distraction=defines.distraction.byenemy},
                             glob.attackcount)
      glob.attackcount = glob.attackcount + 2
    end
  end
  


   --[[------------------Underground mining drill stuff------------------]]--
    local randomResources={"coal", "coal", "coal","iron-ore","iron-ore","copper-ore","copper-ore","stone","stone","stone","stone","coal", "coal", "coal","iron-ore","iron-ore","copper-ore","copper-ore","stone","stone","stone","stone","stone","stone","stone","stone","gold-ore","silver-ore"}
   
   if event.name == "onplayermineditem" and event.itemstack.name=="underground-mining-drill" then 
   glob.undergroundMinerPos[#glob.undergroundMinerPos].active=false
   end
   
   if event.name == "onbuiltentity" and event.createdentity.name=="underground-mining-drill" then

        local bModified
        for _,fieldValue in pairs(glob.undergroundMinerPos) do
                
                if event.createdentity.position.x==fieldValue.x and event.createdentity.position.y==fieldValue.y then
                        fieldValue.active="true"
                        bModified=true
                        break
                end
        end
        if not bModified then

                local fieldName=#glob.undergroundMinerPos+1
                glob.undergroundMinerPos[fieldName]={}


                glob.undergroundMinerPos[fieldName].entity = event.createdentity


                glob.undergroundMinerPos[fieldName].active="true"
		if glob.undergroundMinerPos[fieldName].resource~="iron-ore" or glob.undergroundMinerPos[fieldName].resource~="coal" or glob.undergroundMinerPos[fieldName].resource~="stone" or glob.undergroundMinerPos[fieldName].resource~="copper-ore" or glob.undergroundMinerPos[fieldName].resource~="silver-ore" or glob.undergroundMinerPos[fieldName].resource~="gold-ore" then
                	glob.undergroundMinerPos[fieldName].resource=randomResources[math.random(1,#randomResources)]
                	glob.undergroundMinerPos[fieldName].resourcevalue=math.random(1,5)
		end
                glob.undergroundMinerPos[fieldName].position={}
                glob.undergroundMinerPos[fieldName].position.x=event.createdentity.position.x
                glob.undergroundMinerPos[fieldName].position.y=event.createdentity.position.y
            glob.undergroundMinerPos[fieldName].name="underground-mining-drill"
        end


  end 

   
   if event.name == "ontick" then


      for fieldName, _ in pairs(glob.undergroundMinerPos) do

            if glob.undergroundMinerPos[fieldName].entity.isvalid() then
			uminingdrill=glob.undergroundMinerPos[fieldName].entity.getinventory(defines.inventory.chest)

         
         if math.random(130)==80 then
            
            randomnumber2=math.random(1,glob.undergroundMinerPos[fieldName].resourcevalue)
            if uminingdrill.isvalid() and uminingdrill.caninsert({name=(glob.undergroundMinerPos[fieldName].resource), count=(randomnumber2)}) then
               uminingdrill.insert({name=(glob.undergroundMinerPos[fieldName].resource), count=(randomnumber2)})
            end
         end
         end
      end
   end

   
    	if event.name == "onbuiltentity" and event.createdentity.name=="underground-scanner" then
		for fieldName, v in ipairs(glob.undergroundMinerPos) do
		local canbescanned={}
            if event.createdentity.position.x==v.position.x and event.createdentity.position.y==v.position.y then
			game.getplayer().print("The scanner has found "..glob.undergroundMinerPos[fieldName].resource.." in this zone, and the richness is "..glob.undergroundMinerPos[fieldName].resourcevalue)
			canbescanned={fieldName=true}
			break
			end
		if canbescanned[fieldname]==nil then
			game.getplayer().print("The zone has not been drilled, the scanner will not work")
		end
	end
	end
   --[[-------------------------------End :)------------------------------]]--



   --[[------------------------Salt purifier stuff------------------------]]--

	if event.name=="onbuiltentity" and event.createdentity.name=="salt-purifier" then
		if glob.salt==nil then
			glob.salt={}
			glob.salt.number=1
			glob.salt[glob.salt.number]={}
			glob.salt[glob.salt.number].position=event.createdentity.position
		else
			glob.salt.number=glob.salt.number+1
			glob.salt[glob.salt.number]={}
			glob.salt[glob.salt.number].position=event.createdentity.position
		end
		
	end
	if glob.salt~=nil then
	for i=1,glob.salt.number do
		local entities = game.findentities{ topleft = {x = glob.salt[i].position.x - 1, y = glob.salt[i].position.y - 1}, bottomright = {x = glob.salt[i].position.x + 1 ,y = glob.salt[i].position.y + 1} }
     		 for i,entity in ipairs(entities) do
         		if (entity.name== "salt-purifier") then
				local saltinv = entity.getinventory(defines.inventory.chest)
				local pipeEntities=game.findentities{ topleft = {x = glob.salt[i].position.x - 25, y = glob.salt[i].position.y - 25}, bottomright = {x = glob.salt[i].position.x + 25 ,y = glob.salt[i].position.y + 25} }
				foundpump=false
				for i,entity in ipairs(pipeEntities) do
					if entity.name=="pump" then
						foundpump=true
						break
					end
				end
				 if saltinv.caninsert({name="iron-ore", count="1"}) then
					if math.random(1,160)==1 and foundpump==true then
          					saltinv.insert({name="iron-ore", count=1})
					end
          			  end
       			 end
		end
	end
	end

   --[[-------------------------------End :)------------------------------]]--
   
      --[[------------------------Boiler removing-------------------------]]--
	if event.name=="onbuiltentity" and event.createdentity.name=="boiler" then
		local saltp=game.findentities{ topleft = {x = event.createdentity.position.x - 25, y = event.createdentity.position.y - 25}, bottomright = {x = event.createdentity.position.x + 25 ,y = event.createdentity.position.y + 25} }
		for i,entity in ipairs(saltp) do
				foundsalt=false
			if entity.name=="salt-purifier" then
				foundsalt=true
				break
			end
		end
		if foundsalt==false then
			event.createdentity.destroy()
			game.getplayer().print("Your boiler is stuck, and it have blown up, make a salt purifier and place it near.")
		end
	end
   --[[-------------------------------End :)------------------------------]]--
end

function cheat(n,a)
	if a == nil then
		game.getplayer().insert{name=n,count=5}
	else
		game.getplayer().insert{name=n,count=a}
	end
end
Also, im running on a shitty computer, maybe it is because the salt purifier is checking for lots of things each tick?

Post Reply

Return to “Modding help”