Modding Help Can anyone help me with some LUA?

Discussion in 'Starbound Modding' started by Wulf_Oman, May 22, 2015.

  1. Wulf_Oman

    Wulf_Oman Existential Complex

    I'm pretty much at a stand still because I'm not fluent in Lua; a beginner really. If anyone could figure out why these scripts aren't doing what they're meant to, it would really help. Also, if you'd like, I will give a credit on the mod once it's released.

    Just a note; you do need the Bees! mod because that's what this is meant for

    Objects with Lua download (pulled these from the actual mod directly, so no clue if this version will work right away when installed as is)

    Mutagen Extracter:
    It's supposed to take any item with the word "queen" or "drone" in it and automatically craft it (with varying chances) into Mutagen OR Genetic Waste in a specific slot

    Genetic Infuser:
    It's supposed to take recipes (defined in said Lua, not as recipe files, there would be FAR too many then) and output them in a specific slot
     
  2. Peelz

    Peelz Giant Laser Beams

    What IS the mod doing? Is it throwing an error? Not doing anything? Is anything showing up in the log?
     
  3. Wulf_Oman

    Wulf_Oman Existential Complex

    Simply put:the Lua just isn't doing what it's supposed to. I assume everything is right, then, but the slots may be wrong? That's a random guess, though.
     
  4. Peelz

    Peelz Giant Laser Beams

    Let's focus on the mutagen extractor first. One error I see right off. This:
    Code:
        if string.find(contents[2].name, "queen") and contents[1].name == "glassvialempty" then
                if math.random(2) == 1 then
                    self.item = "glassvialmutagen"
                    itemchance = queenitem
                end
                if math.random(2) == 2 then
                    self.item = "geneticwaste"
                    itemchance = wasteitem
                end
        end
    should be changed to this:
    Code:
     if string.find(contents[2].name, "queen") and contents[1].name == "glassvialempty" then
                if math.random(2) == 1 then
                    self.item = "glassvialmutagen"
                    itemchance = queenitem
                else
                    self.item = "geneticwaste"
                    itemchance = wasteitem
                end
        end
    for both the queen block and the drone block.

    But I also suspect that you have a bigger problem. Rather than having a main() function, starbound calls an update() function that runs automatically every fraction of a second or so. I would try renaming the main() function to update(). declare it like this:
    Code:
    function update(dt)
     
  5. Wulf_Oman

    Wulf_Oman Existential Complex

    Why would you get rid of the different chance at getting the second item? Just asking.
    Also, I changed the main() to update() to no luck, probably cuz I couldn't figure out the first thing
    Edit: I think I see what you pointed out now and I fixed it, still no luck
     
    Last edited: May 23, 2015
  6. Peelz

    Peelz Giant Laser Beams

    I got the impression that you were trying to make it a 50/50 chance of either getting the "glassvialmutagen" or "geneticwaste". With the code you posted you would be re-rolling the random number twice and potentially be getting multiple outputs.

    Ok, so let's try a few more things. Firstly, just to be safe, you should probably declare your "queenitem" , "droneitem" and "wasteitem" variables at the beginning of your Lua script to ensure that all of your functions can access them. So the first bit of your code should look like this:
    Code:
    local contents
    local queenitem
    local droneitem
    local wasteitem
    Next, there's a few things that I would change in your update() function to simplify things and to make it more clear what you are doing. Change this code:
    Code:
    function update(dt)
        contents = world.containerItems(entity.id())
        if not contents[1] and not contents[2] then
                return
        end
        if contents[1] and contents[2] and not contents[3] then
            equipped()
            workingMutagen()
        end
    end
    to this:
    Code:
    function update(dt)
        contents = world.containerItems(entity.id())
        if not contents[1] === nil and not contents[2] === nil and contents[3] === nil then
            equipped()
            workingMutagen()
        end
    end
     
  7. Wulf_Oman

    Wulf_Oman Existential Complex

    This is the error I'm receiving now
    [13:54:06.128] Error: Failed to instantiate object for placement preview. mutagenextractor {} : (LuaException) Error code 3, [string "/objects/mutagenextractor/mutagenextractor.lu..."]:50: unexpected symbol near '='

    Are you sure it's ===?
     
  8. Peelz

    Peelz Giant Laser Beams

    I *thought* that's how Lua does it... I might be thinking of Javascript though. Try it with '==' instead.
     
    Wulf_Oman likes this.
  9. Kayuko

    Kayuko Oxygen Tank

    Apparently, Lua does it with = and ~=, I know, confusing coming from another language (C# here), but yeah.

    == == ==
    ~= == !=

    Apparently :D

    /confusion on
     
    Last edited: May 27, 2015
    Wulf_Oman likes this.

Share This Page