Modding Help world.loadRegion() and world.loadUniqueEntity() not working

Discussion in 'Starbound Modding' started by jakecool19, Feb 4, 2019.

  1. jakecool19

    jakecool19 Pangalactic Porcupine

    Hello there,just needed help with these two functions which I need for my mod. This is an automated farming machine that, when wired to helper, will till and water the land directly beneath it in between it and the helper. It will then plant and harvest crops as neccessary. I've tried using world.loadRegion first, but the moment I go to deep underground or beam up the Hello world stops. I've also put it in the log and it returns true, so trying a different route I tried to use the latter function but that didn't work too. What am I doing wrong? (P.S. No point putting in a .log file there are no errors that pop up into it)

    Code:
    function findHelper()
      local map = object.getInputNodeIds(0)
      if storage.found ~= "success" then
        for e,_ in pairs(map) do
          if world.entityName(e) == "farmerhelper" then
            storage.helper = e
            storage.found = "success"
            sb.logInfo("Found a helper!%s",storage.helper)
            break
          else
            sb.logInfo("No helper found")
          end       
        end
      end
    end
    
    function prepareEarth()
      if storage.helper ~= nil then
        local start = world.entityPosition(entity.id())
        local stop = world.entityPosition(storage.helper)
        --local test = world.material(stop,"foreground")
        --sb.logInfo("Its here %s",test)
        start[2] = start[2] - 1
        stop[2] = stop[2] - 1
        if start[1] > stop[1] then
          for i=start[1],stop[1],-1 do
            stop[1] = stop[1] + i
            if world.material({i,start[2]},"foreground") == "dirt" and world.mod({i,start[2]},"foreground") ~= "tilled" then
            --sb.logInfo("Placing")
              world.placeMod({i,start[2]},"foreground","tilled")
              else
              --sb.logInfo("Error")
            end
          end
          elseif stop[1] > start[1] then
          for i=stop[1],start[1],-1 do
            start[1] = start[1] + i
            if world.material({i,start[2]},"foreground") == "dirt" and world.mod({i,start[2]},"foreground") ~= "tilled" then
            --sb.logInfo("Placing")
              world.placeMod({i,start[2]},"foreground","tilled")
              else
              --sb.logInfo("error")
            end
          end
        end
      end
    end
    
    function harvestCrops()
      if storage.helper ~= nil then
        local crops = world.entityLineQuery(world.entityPosition(entity.id()),world.entityPosition(storage.helper))
        for _,obj in pairs(crops) do
          if world.getObjectParameter(obj,"objectType") == "farmable" then
            local stages = world.getObjectParameter(obj,"stages")
            --sb.logInfo("Break me %s",#stages)
            if #stages-1 == world.farmableStage(obj) then
            local loc = world.entityPosition(obj)
            world.damageTiles({{loc[1],loc[2]}},"foreground",loc,"plantish",1,1)
            end
            --world.callScriptedEntity(obj, "dropHarvest",{sourceId = entity.id()})
          end
          if world.entityType(obj) == "itemDrop" then
          local item = world.itemDropItem(obj)
          local stackProtect = world.containerItemsCanFit(entity.id(),item)
          if stackProtect ~= nil and stackProtect > 0 then
            if world.takeItemDrop(obj,entity.id()) ~= nil then
            --sb.logInfo("Test")
            world.containerAddItems(entity.id(),item)
            end
            --sb.logInfo("Test")
            end
          end
        end
      end
    end
    
    function plantCrops()
      local items = world.containerItems(entity.id())
      --sb.logInfo("Hello World")
      for slot,item in pairs(items) do
      local info = root.itemType(item.name)
      --sb.logInfo("Hello World %s",info)
        if item ~= nil and root.itemType(item.name) == "object" and item.count > 0 and string.find(item.name,"seed") ~= nil then
          --sb.logInfo("Hello Wo")
          if storage.helper ~= nil then
            local stop = world.entityPosition(storage.helper)
            local start = world.entityPosition(entity.id())
          if start[1] > stop[1] then
            for i=start[1],stop[1],-1 do
              stop[1] = stop[1] + i
              --sb.logInfo("Hello Start")
              if world.placeObject(item.name,{i,start[2]}) == true then
                item.count = 1
                world.containerConsume(entity.id(),item)
                break           
              end
            end
          elseif stop[1] > start[1] then
            for i=stop[1],start[1],-1 do
              start[1] = start[1] + i
              --sb.logInfo("Hello World")
              if world.placeObject(item.name,{i,start[2]}) == true then
                item.count = 1
                world.containerConsume(entity.id(),item)
                break
              end
            end
          end
        end
      end
    end
    end
    
    function findFarm()
      if storage.helper ~= nil then
        local start = world.entityPosition(entity.id())
        local stop = world.entityPosition(storage.helper)
        storage.region = {
        start[1],
        start[2] + 3,
        stop[1],
        stop[2] - 3
        }
        elseif storage.helper == nil then
        storage.region = nil
      end
    end
    
    function onNodeConnectionChange(args)
      --sb.logInfo("Working")
      storage.helper = nil
      storage.found = "reset"
      findHelper()
      findFarm()
    end
    function update(dt)
      prepareEarth()
      harvestCrops()
      plantCrops()
      sb.logInfo("Hello World %s",entity.uniqueId())
      if storage.region ~= nil then
        world.loadRegion(storage.region)
      end
      if entity.uniqueId() == nil then world.setUniqueId(entity.id(),35232431) end
      world.loadUniqueEntity(entity.uniqueId())
    end
    
     
  2. jakecool19

    jakecool19 Pangalactic Porcupine

    Edit: Apparently there is no way to load regions while on different planets
     
  3. bk3k

    bk3k Oxygen Tank

    That's correct. What you can do though is record the time the chunk was unloaded or uninit() was called, then when it is reloaded you can compare the current time and do the math to see how many cycles should have passed.
     
    jakecool19 likes this.
  4. jakecool19

    jakecool19 Pangalactic Porcupine

    The problem with that is, I'm trying to make a Factoriolike mod that overhauls the colony system so that colonists and assemblers need power and food. If I do something like that in order to have a steady supply of food I would need to make greenhouse objects that could continuously make produce since crops won't be harvested which kinda defeats the purpose of the auto farmer. But that will have to do, thank you again bk3k!!
     

Share This Page