Replace value in an external table

Discuss the use of Lua in Script measures.

Replace value in an external table

March 7th, 2017, 12:53 am
soyelrafa
   [28 posts]

Hello! I read all the internet and try every script I found but I can't get it to work.

I have a table in test.lua that have:

GameNames = {}
GameNames["ABZU"] = {
["Name"] = "ABZU",
["Program"] = "ABZU.exe",
["Time"] = 21
}

GameNames["AGE OF EMPIRES II HD EDITION"] = {
["Name"] = "AOE2",
["Program"] = "AOE2.exe",
["Time"] = 22
}

Then, in the LUA Script I want to replace the Time value of ABZU from 21 to 22 for example, and write it to the file. I can get the value, but I can't get it to replace in the correct place. The best I get is to write in the end of the file 22.

There is a way to edit it with Lua?

Thanks in advance.

Re: Replace value in an external table

March 8th, 2017, 12:15 pm
soyelrafa
   [28 posts]

I try with:

Code: Select all

mNombreValor = "ABZU"
dofile("Test.lua")

for key,value in pairs(GameNames[mNombreValor]) do
if key == "Program" then
mNombreValor2 = value
print(value)
GameNames[mNombreValor]["Time"] = 3
print(GameNames[mNombreValor]["Time"])
io.write(GameNames[mNombreValor]["Time"])
end
end


But with this io.write act likes print, so I think that dofile only works to read. But if I try with io.open then it opens like a normal text, so I can't modificate the table...

EDIT: I'm using Eclipse for testings, that's why I don't put dofile in the Initialize function.

Re: Replace value in an external table

March 9th, 2017, 9:09 pm
soyelrafa
   [28 posts]

At this time the only way I found to do that is, using the code for write a table to a file of the lua tutorial, rewriting all the table again with the modificated value. Not so much efficent, but it works at least...

Code: Select all

function basicSerialize (o)
      if type(o) == "number" then
        return tostring(o)
      else   -- assume it is a string
        return string.format("%q", o)
      end
    end
   
function save (name, value, saved)
      saved = saved or {}       -- initial value
      io.write(name, " = ")
      if type(value) == "number" or type(value) == "string" then
        io.write(basicSerialize(value), "\n")
      elseif type(value) == "table" then
        if saved[value] then    -- value already saved?
          io.write(saved[value], "\n")  -- use its previous name
        else
          saved[value] = name   -- save name for next time
          io.write("{}\n")     -- create a new table
          for k,v in pairs(value) do      -- save its fields
            local fieldname = string.format("%s[%s]", name,
                                            basicSerialize(k))
            save(fieldname, v, saved)
          end
        end
      else
        error("cannot save a " .. type(value))
      end
    end



dofile("test.lua")
a = 3
GameNames["ABZU"]["Time"] = 24 + a
local f = assert(io.open("test.lua", "w+"))
io.output("test.lua") 
f:write(save ('GameNames', GameNames)) 

Return to “Lua Scripting”



Who is online

Users browsing this forum: No registered users and 1 guest