Modding problems with the update

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:

Modding problems with the update

Post 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 
Last edited by ficolas on Sun Mar 31, 2013 3:40 pm, edited 1 time in total.

slpwnd
Factorio Staff
Factorio Staff
Posts: 1835
Joined: Sun Feb 03, 2013 2:51 pm
Contact:

Re: Modding probles with the update

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

ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

Re: Modding probles with the update

Post by ficolas »

Now event.name is a number?
and what are the numbers?

User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: Modding probles with the update

Post 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
<I'm really not active any more so these may not be up to date>
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me :)
Or drop into #factorio on irc.esper.net

ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

Re: Modding problems with the update

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

User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: Modding problems with the update

Post by FreeER »

lol, might not be a bad idea :)
<I'm really not active any more so these may not be up to date>
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me :)
Or drop into #factorio on irc.esper.net

ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

Re: Modding problems with the update

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

kovarex
Factorio Staff
Factorio Staff
Posts: 8194
Joined: Wed Feb 06, 2013 12:00 am
Contact:

Re: Modding problems with the update

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

User avatar
rk84
Filter Inserter
Filter Inserter
Posts: 556
Joined: Wed Feb 13, 2013 9:15 am
Contact:

Re: Modding problems with the update

Post 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.
Test mode
Searching Flashlight
[WIP]Fluid handling expansion
[WIP]PvP gamescript
[WIP]Rocket Express
Autofill: The torch has been pass to Nexela

Post Reply

Return to “Modding help”