Modding Help A helping hand?

Discussion in 'Starbound Modding' started by Omekk, Jan 1, 2016.

  1. Omekk

    Omekk Pangalactic Porcupine

    Hey guys,

    I've been trying to come up with a better timer for my incubator in my mod... However, I keep failing.. Over and over. Think one of you could give me a hand with this? I want this to function while away from the planet/area like some other things I've seen around.. (Bees apiaries, etc.) but I can't seem to wrap my mind around what I am doing wrong.

    As it stands, this timer doesn't reset and stay at zero until new eggs/ores are added. It'll count up even when empty after having started once and if I put in another egg/ore it will instantly provide the hatch-able incubated version. How can I resolve this and also get the timer to be persistent?

    Here is what I have currently:

    http://pastebin.com/58wKQF4h

    Thanks in advance
     
  2. Peelz

    Peelz Giant Laser Beams

    Best Answer
    To make your variable persistent, declare it as storage.timer instead of self.timer.

    I refactored your code to solve some of the problems you were encountering. Below is the code that I would write if I were trying to make this mod. I would recommend not copy-pasting it though because I haven't actually tested it.
    Code:
    function update(dt)
        local contents = world.containerItems(entity.id())
        local slot1 = contents[1] or {}
        local slot2 = contents[2] or {}
    
        local preReq = {}
        preReq[slot1.name or "Nil"] = true
        preReq[slot2.name or "Nil"] = true
    
        local spawnType = chickenTypeCheck(preReq)
    
        if spawnType ~= nil then
            storage.timer = (storage.timer or 0) + dt
            if storage.timer >= 300 then
                eggSpawn(spawnType)
                storage.timer = 0
            end
        else
            storage.timer = 0
        end
    end
    
    function chickenTypeCheck(preReq)
        if preReq["normalchickenegg"] and preReq["coalore"] then
            return "normalchicken"
        elseif preReq["robotchickenegg"] and preReq["ironore"] then
            return "robotchicken"
        elseif preReq["copperchickenegg"] and preReq["copperore"] then
            return "copperchicken"
        elseif preReq["goldenchickenegg"] and preReq["goldore"] then
            return "goldenchicken"
        elseif preReq["silverchickenegg"] and preReq["silverore"] then
            return "silverchicken"
        elseif preReq["diamondchickenegg"] and preReq["diamond"] then
            return "diamondchicken"
        elseif preReq["platinumchickenegg"] and preReq["platinumore"] then
            return "platinumchicken"
        elseif preReq["titaniumchickenegg"] and preReq["titaniumore"] then
            return "titaniumchicken"
        elseif preReq["aegisaltchickenegg"] and preReq["aegisaltore"] then
            return "aegisaltchicken"
        elseif preReq["violiumchickenegg"] and preReq["violiumore"] then
            return "violiumchicken"
        elseif preReq["rubiumchickenegg"] and preReq["rubiumore"] then
            return "rubiumchicken"
        elseif preReq["roosterchickenegg"] and preReq["coalore"] then
            return "normalrooster"
        else
            return nil
        end
    end
    
    function eggSpawn(chickenType)
        local contents = world.containerItems(entity.id())
        eggConsume(contents)
        world.containerAddItems(entity.id(), { item= chickenType .. "egghatchable", amount = 1})
    end
    
    function eggConsume(contents)
        world.containerConsume(entity.id(), { name= contents[1].name, count = 1, data={}})
        world.containerConsume(entity.id(), { name= contents[2].name, count = 1, data={}})
    end
    
     
    Monijir and Omekk like this.
  3. iUltimateLP

    iUltimateLP Orbital Explorer

    Hmm, the timer resets because your in another planet and therefore you cause the other celestial world to unload which will unload entities with your script aswell. AS for that persistance problem, there needs to be a way with saving values even when you leave the world.. As far as I know, saving values over worlds isn't working, my attempt would be an external config file, but you don't have access to files in SB's LUA..
     
  4. Peelz

    Peelz Giant Laser Beams

    Best Answer
    To make your variable persistent, declare it as storage.timer instead of self.timer.

    I refactored your code to solve some of the problems you were encountering. Below is the code that I would write if I were trying to make this mod. I would recommend not copy-pasting it though because I haven't actually tested it.
    Code:
    function update(dt)
        local contents = world.containerItems(entity.id())
        local slot1 = contents[1] or {}
        local slot2 = contents[2] or {}
    
        local preReq = {}
        preReq[slot1.name or "Nil"] = true
        preReq[slot2.name or "Nil"] = true
    
        local spawnType = chickenTypeCheck(preReq)
    
        if spawnType ~= nil then
            storage.timer = (storage.timer or 0) + dt
            if storage.timer >= 300 then
                eggSpawn(spawnType)
                storage.timer = 0
            end
        else
            storage.timer = 0
        end
    end
    
    function chickenTypeCheck(preReq)
        if preReq["normalchickenegg"] and preReq["coalore"] then
            return "normalchicken"
        elseif preReq["robotchickenegg"] and preReq["ironore"] then
            return "robotchicken"
        elseif preReq["copperchickenegg"] and preReq["copperore"] then
            return "copperchicken"
        elseif preReq["goldenchickenegg"] and preReq["goldore"] then
            return "goldenchicken"
        elseif preReq["silverchickenegg"] and preReq["silverore"] then
            return "silverchicken"
        elseif preReq["diamondchickenegg"] and preReq["diamond"] then
            return "diamondchicken"
        elseif preReq["platinumchickenegg"] and preReq["platinumore"] then
            return "platinumchicken"
        elseif preReq["titaniumchickenegg"] and preReq["titaniumore"] then
            return "titaniumchicken"
        elseif preReq["aegisaltchickenegg"] and preReq["aegisaltore"] then
            return "aegisaltchicken"
        elseif preReq["violiumchickenegg"] and preReq["violiumore"] then
            return "violiumchicken"
        elseif preReq["rubiumchickenegg"] and preReq["rubiumore"] then
            return "rubiumchicken"
        elseif preReq["roosterchickenegg"] and preReq["coalore"] then
            return "normalrooster"
        else
            return nil
        end
    end
    
    function eggSpawn(chickenType)
        local contents = world.containerItems(entity.id())
        eggConsume(contents)
        world.containerAddItems(entity.id(), { item= chickenType .. "egghatchable", amount = 1})
    end
    
    function eggConsume(contents)
        world.containerConsume(entity.id(), { name= contents[1].name, count = 1, data={}})
        world.containerConsume(entity.id(), { name= contents[2].name, count = 1, data={}})
    end
    
     
    Monijir and Omekk like this.
  5. Omekk

    Omekk Pangalactic Porcupine

    You, sir, are my hero. ONE WORD! Changing self to storage and it is all working beautifully. Thank you. I was also able to fix the eggs/chickens instant incubating/breeding from the timer not stopping while nothing is in the objects all on my own. It's perfect now.

    Edit: Damn, it was only because I had dropped the timer to 10 seconds for testing and the world stays loaded for just as long. Still works seemingly the same as before at higher times.
     
    Last edited: Jan 2, 2016
  6. Peelz

    Peelz Giant Laser Beams

    Can you clarify the problem you are having now?
     
  7. Omekk

    Omekk Pangalactic Porcupine

    Well, nothing changed really. I thought it had fixed it because I was testing it on a 10 second interval and I was getting an item while I was away, however, I should have tried waiting a longer period to see if more items dropped. The only reason it "worked" was because the world stays alive for 10 seconds after you leave. Once I put the interval back up to two minutes it no longer worked.
     
  8. Peelz

    Peelz Giant Laser Beams

    Ah, ok. I'm not sure how to (or even if you can) keep a script running after the world is unloaded. It may be possible, but I just don't know how.

    If it were me, I would use a persistant variable to store the current time on the planet using the world.time() function (http://starbounder.org/Modding:Lua/Tables/World#world.time). That way, whenever you leave the planet, the time that the script stops is stored. In the objects init() function you can get the current time (again using world.time() ) and subtract the time that you left to give you the total time that has passed. Then you can just spawn an egg for each 300 seconds that has passed. I'm not sure if this will work, but you can give it a try.
     
  9. iUltimateLP

    iUltimateLP Orbital Explorer

    But how do you want to store that variable? I mean, this storage thing does not work as we saw, and writing things into files does not work aswell..
     
  10. Peelz

    Peelz Giant Laser Beams

    I'm pretty sure it DOES work though. Declaring your variable as storage.myVariable causes the stored value to persist even after the world is unloaded. The script will not continue to run and you cannot (to my knowledge) continue to modify the variable, but the value will remain stored after you return.
     

Share This Page