Modding Help Is it possible to create a chunk loader?

Discussion in 'Starbound Modding' started by nickc01, Sep 2, 2016.

Tags:
  1. nickc01

    nickc01 Phantasmal Quasar

    Would it be possible to make an object that would keep anything nearby it loaded, even if there are no players on the planet that it is on? It would allow certain machines to continue operating while the player an away.
     
  2. phenius

    phenius Void-Bound Voyager

    I would absolutely use this. The quarry from Steambound Reloaded is a perfect example of something that would be amazing with a chunk / world loader.
     
  3. lazarus78

    lazarus78 The Waste of Time

  4. bk3k

    bk3k Oxygen Tank

    Yes this is possible, and not even difficult.

    It needs only a single function.
    Code:
    world.loadRegion()
     
  5. Domidoy

    Domidoy Void-Bound Voyager

    Do you mind elaborating on this?
     
  6. bk3k

    bk3k Oxygen Tank

    Sure.
    Code:
    init = function()
      local range = config.getParameter("range", 8)
      local position = entity.position()
      storage.region = {
        position[1] - range,
        position[2] - range,
        position[1] + range,
        position[2] + range
      }
    end
    
    update = function()
      world.loadRegion(storage.region)
    end
    
    That's throwing in the optional ability to load "range" from the object doing the loading. That range applies to every direction from the object. So a range of 8(the default if not set), will get you a 16 x 16 tile area.
     
    jakecool19 and Exilyth like this.
  7. Storm_UK

    Storm_UK Existential Complex

    Put "keepAlive" : true in the .object file, and set its "scriptDelta" : 60 which should be plenty fast enough (to run the update at once per second) if you create such an object. I think the Frackin' Universe already has one (Watcher?)

    If you keep too many chunks loaded it will likely cause fps drop though.
     
    bk3k likes this.
  8. bk3k

    bk3k Oxygen Tank

    You know scrolling back up to this part I realized that a chunk loader might not be what you need.
    There is a better way. I made code that handles machine production while the player is away - even off planet. This is part of my init function
    Code:
      storage.inactiveTime = storage.inactiveTime or 0
      storage.was_inactive = storage.was_inactive or false
    
    Some of the code I'm using
    Code:
    uninit = function()
      if storage.state and not storage.was_inactive then
        storage.was_inactive = true
        storage.inactiveTime = os.time()
      end
    end
    
    
    update = function(dt)
      if storage.ship then
        return
      end
     
      local active = world.regionActive(storage.region)
    
      if storage.state then
        storage.harvest_countDown = storage.harvest_countDown - dt
        if storage.harvest_countDown > 0 then
          --nothing
        else
          storage.harvest_countDown = storage.harvest_timer + math.fRandom(0, storage.harvest_time_over)
       
          if hasFuel() and active then
            addItem( pickOre(storage.biome) )
            useFuel()
          else
            processWireInput() --this will actually handle the change well
          end
        end
     
        if active then
          if storage.was_inactive then
            local diff = os.difftime(os.time(), storage.inactiveTime)
            local cycles = math.floor(diff / ( storage.harvest_timer + math.fRandom(0, storage.harvest_time_over)))
            cycles = math.max(math.min(cycles, 10000), 0)
            makeup_cycles(cycles)
            storage.was_inactive = false
          end
        else
          if not storage.was_inactive then
            storage.was_inactive = true
            storage.inactiveTime = os.time()
          end
        end
         
        updateAnimation()
      end
    end
    
    
    makeup_cycles = function(cycles)
      --sb.logInfo("\nmakeup_cycles(" .. tostring(cycles)  .. ")")
      while cycles > 0 and hasFuel() do
        addItem( pickOre(storage.biome) )
        useFuel()
        cycles = cycles - 1
      end
    end
    
    You wouldn't use that exact code per say. But it shows how you can handle continued work without need of chunk loading. If you want to see the whole script, download ISE4, and look at \objects\machines\ise4_miner_script.lua

    Obviously that script could use cleaned up and all that, but it works as is. And thinking about it, maybe I should update to prevent that working in stations(where it doesn't make sense) just like ships.

    For a short time I did chunk loading before deciding this was better. The minor downside to the current code is that it will totally loose the cycles if you exit the game. If that really bothered me(it doesn't), then I could get the object a UUID, and write the time into world properties with that UUID as part of the property's nameSpace.
     
    cpeosphoros and Cyel like this.
  9. cpeosphoros

    cpeosphoros Orbital Explorer

  10. mikeloeven

    mikeloeven Big Damn Hero

    So has a viable chunk loader actually been released yet?? I just started using the Garden bot mod and have been looking for some sort of placeable object to keep my farm fully operational when I head back to the ship. Crops can still grow when your not around but NPC's like the bots stop functioning which makes things grind to a halt.
     
    Last edited: Nov 13, 2017
  11. bk3k

    bk3k Oxygen Tank

    As a stand alone mod? Don't think so. FU has the watcher which IIRC keeps a 16 x 16 tile area active. It wouldn't be hard at all to make a stand alone mod if you wanted.
     
  12. mikeloeven

    mikeloeven Big Damn Hero

    Well i have zero experience with LUA or even basic game programming
     
  13. Geethebluesky

    Geethebluesky Scruffy Nerf-Herder

Share This Page