Modding Help Patching lua code

Discussion in 'Starbound Modding' started by Qbi Wan, May 8, 2021.

  1. Qbi Wan

    Qbi Wan Pangalactic Porcupine

    Hi, I'm making language mod. It generally goes well as most of the text is in json. But unfortunatelly for fossil minigame text "excavation progress" is in lua so it can change color.
    Code:
    function drawGui()
      self.frame:draw({0, 0})
    
      gameCanvas:drawText("EXCAVATION", {position = {230, 183}, width = 88}, 12)
      gameCanvas:drawText("PROGRESS", {position = {235, 171}, width = 88}, 12)
      local progressColor = {255,255,255}
      if self.level.fossilDamaged then
        progressColor = {255, 0, 0}
      end
      gameCanvas:drawText(self.level.progress .. "/" .. #self.level.fossilTiles, {position = {255, 153}}, 12, progressColor)
      self.fossilCounter:draw({225, 136})
    
      if (self.treasureComplete) then
        self.treasureIndicator:draw({285, 140})
      end
    
      self.toolButtonSet:draw()
    
      for i,button in pairs(self.toolButtonSet.buttons) do
        local tool = button.data
        if tool.uses >= 0 then
          local textCol = self.tools[i].uses == 0 and {255, 0, 0} or {255, 255, 255}
          local textPos = {button.position[1] + button.size[1] - 8, button.position[2] + 8}
          gameCanvas:drawText(tool.uses, {position = textPos}, 8, textCol)
        end
      end
    end
    How can I patch it so it'll draw different text? Code above is only a fragment of a file fossilgame.lua
     
  2. Zaakari

    Zaakari Pangalactic Porcupine

    Unfortunately the Lua files can't be patched, which leaves two options: replace the whole "fossilgame.lua" file, or make a new Lua that just replaces the "drawGui" function, and patch that file into the "fossilgamegui.config" file's "scripts" array. I recommend the latter, as overwriting Lua files as little as possible is usually more desirable.

    translation_gui.lua
    Code:
    function drawGui()
      self.frame:draw({0, 0})
    
      gameCanvas:drawText("EXCAVATION TRANSLATED", {position = {230, 183}, width = 88}, 12)
      gameCanvas:drawText("PROGRESS TRANSLATED", {position = {235, 171}, width = 88}, 12)
      local progressColor = {255,255,255}
      if self.level.fossilDamaged then
        progressColor = {255, 0, 0}
      end
      gameCanvas:drawText(self.level.progress .. "/" .. #self.level.fossilTiles, {position = {255, 153}}, 12, progressColor)
      self.fossilCounter:draw({225, 136})
    
      if (self.treasureComplete) then
        self.treasureIndicator:draw({285, 140})
      end
    
      self.toolButtonSet:draw()
    
      for i,button in pairs(self.toolButtonSet.buttons) do
        local tool = button.data
        if tool.uses >= 0 then
          local textCol = self.tools[i].uses == 0 and {255, 0, 0} or {255, 255, 255}
          local textPos = {button.position[1] + button.size[1] - 8, button.position[2] + 8}
          gameCanvas:drawText(tool.uses, {position = textPos}, 8, textCol)
        end
      end
    end
    
    /interface/games/fossilgame/fossilgamegui.config.patch
    Code:
    [
      { "op": "add",
        "path": "/scripts/-"
        "value": "/interface/games/fossilgame/translation_gui.lua"
      }
    ]
    - Edit -
    Now that I think about it, it might be possible to not have to overwrite anything. Perhaps the original function could be called (which would draw the GUI), then our new function could be called, which would draw a rectangle over the old text, and then draw the translated text. I haven't tested it, so maybe it would look too ugly. But the code would look something like this:

    translation_gui.lua
    Code:
    local oldDrawGui = drawGui
    
    function drawGui()
      -- First, call the original draw function.
      oldDrawGui()
    
      -- Second, draw a black rectangle over the original text area (my Y co-ordinates might be accurate,
      -- as I don't remember if lower Y values are higher or lower on the screen in Starbound)
      gameCanvas:drawRect({230, 183, 230 + 88, 171 - 12}, {0, 0, 0, 100})
    
      -- Third, draw the translated text.
      gameCanvas:drawText("EXCAVATION TRANSLATED", {position = {230, 183}, width = 88}, 12)
      gameCanvas:drawText("PROGRESS TRANSLATED", {position = {235, 171}, width = 88}, 12)
    end
    As I said, I haven't tested this, but something similar might work if you don't like the first solution.
     
    Last edited: May 8, 2021

Share This Page