Page 1 of 1

Modding problems with the update

Posted: Sun Mar 31, 2013 11:01 am
by ficolas
With the update, my code doesnt work, and I dont know what are the modding changes, so I dont know how to fix it D:

Code: Select all

 require "util"
require "defines"

game.oninit = function()
end

game.onevent = function(event)
   --[[------------------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 :)------------------------------]]--




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 

Re: Modding probles with the update

Posted: Sun Mar 31, 2013 11:25 am
by slpwnd
The code for registering the events have changed. Check out the updated documentation on the wiki https://forums.factorio.com/wiki/index.php/Lua/Events.

Re: Modding probles with the update

Posted: Sun Mar 31, 2013 11:45 am
by ficolas
Now event.name is a number?
and what are the numbers?

Re: Modding probles with the update

Posted: Sun Mar 31, 2013 12:09 pm
by FreeER
from what I read it should look something like this (but I didn't test it):

Code: Select all

    require "util"
    require "defines"

    game.oninit = function()
    end

    game.onevent (defines.events, function(event))
        --[[------------------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 == defines.event.onplayermineditem and event.itemstack.name == "underground-mining-drill" then 
        glob.undergroundMinerPos[#glob.undergroundMinerPos].active=false
        end
       
        if event.name == defines.event.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 == defines.event.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 == defines.event.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 :)------------------------------]]--
)



    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
    
and you may notice I used defines.event which include all events, mainly because I've had 2 hrs of sleep right now

Re: Modding problems with the update

Posted: Sun Mar 31, 2013 3:52 pm
by ficolas
I think that im going to re-code everything...
But not today!

The code is messy in my opinion, and since I didnt know most of the things I know now about factorio when I made it, I will recode it to be more efficient and cleaner, but I will release it today :D!

Re: Modding problems with the update

Posted: Sun Mar 31, 2013 4:12 pm
by FreeER
lol, might not be a bad idea :)

Re: Modding problems with the update

Posted: Thu Apr 04, 2013 9:36 pm
by ficolas
with the gui methods, I tried using

Code: Select all

for _,i in pairs(gui) do game.getplayer().print(i) end
for _,i in pairs(game.gui) do game.getplayer().print(i) end
for _,i in pairs(game.getplayer().gui) do game.getplayer().print(i) end
for _,i in pairs(game.player.character.gui) do game.getplayer().print(i) end
but none of those is, where is it? and how can I find those things? because it says nothing at the wiki...

Re: Modding problems with the update

Posted: Thu Apr 04, 2013 10:40 pm
by kovarex
Hello, some (basic) info is here.
https://forums.factorio.com/wiki/index.php/Lua/Gui

the access is

Code: Select all

game.player.gui
Your methods of finding contents will not work to find properties, the ".gui" is not in fact property of the player, it is function that returns the gui called on demand when you request the ".gui" property.
(The same with game.player).

Re: Modding problems with the update

Posted: Fri Apr 05, 2013 10:41 am
by rk84
kovarex wrote:Hello, some (basic) info is here.
https://forums.factorio.com/wiki/index.php/Lua/Gui

the access is

Code: Select all

game.player.gui
Your methods of finding contents will not work to find properties, the ".gui" is not in fact property of the player, it is function that returns the gui called on demand when you request the ".gui" property.
(The same with game.player).
Wow, I made my first button. I would like to point out though that unlike in wiki says. Add -function only takes one table as parameter. Example of my test usage:

Code: Select all

game.player.gui.add({position="ontherightofthegoal", type="flow", name="myflow", caption="Noone can see me :("})
game.player.gui.myflow.add({position="ontherightofthegoal", type="button", name="button", caption="Hello World!"})
hmm if you remove the flow from root. the button will vanish on screen, but remains in namespace. And can't use remove for button.