Can't Change Table Value

User avatar
octacian
Member
 
Posts: 408
Joined: Mon Dec 21, 2015 22:18
GitHub: octacian
IRC: octacian
In-game: octacian

Can't Change Table Value

by octacian » Thu Aug 18, 2016 17:49

I'm still in the midst of finishing a rank "module" for my servertools mod (will be released after this). My current problem is that when a player's rank is changed, I need to update it in a file containing a table (file is rank.db, table is called "mem"). Though the rest of the process completes just fine, I can't get it to change the rank in the table.

Issue:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
for _,i in ipairs(mem) do
    if i.name == name then
      mem[_].rank = newrank -- set new rank value
      datalib.table.write(modpath.."/rank.db", mem) -- write updated table
      servertools.log(name.."'s rank set to "..newrank..".")
    end
  end


Full set player rank function:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
-- set rank
function servertools.set_player_rank(name, newrank)
  -- check params
  if not minetest.get_player_by_name(name) then
    return "Invalid player: "..name
  elseif not newrank then
    return "New rank field required."
  end

  local rank_privs = servertools.get_rank_privs(newrank) -- get rank privileges
  -- check that rank exists
  if not rank_privs or type(rank_privs) ~= "table" then
    return "Rank nonexistent."
  end

  -- update player privileges
  if not minetest.set_player_privs(name, rank_privs) then return false end -- unsuccessful
  -- TODO: fix setting table value
  -- update database
  for _,i in ipairs(mem) do
    if i.name == name then
      mem[_].rank = newrank -- set new rank value
      datalib.table.write(modpath.."/rank.db", mem) -- write updated table
      servertools.log(name.."'s rank set to "..newrank..".")
    end
  end
end


RankDB (example):
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
return {{["name"] = "singleplayer", ["rank"] = "player"}, {["name"] = "octacian", ["rank"] = "owner"}, {["name"] = "oct", ["rank"] = "player"}}


mem table (example):
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
mem = {
    { name = "singleplayer", rank = "player" },
    { name = "octacian", rank = "owner" },
    { name = "oct", rank = "player" },
}


I was thinking about removing the record, then inserting a new one, but that should definitely not be required.

Thanks.
God isn't dead!

My Coolest Mods:
MicroExpansion, Working Computers, Interchangeable Hands

Check out my YouTube channel! (octacian)
 

User avatar
srifqi
Member
 
Posts: 508
Joined: Sat Jun 28, 2014 04:31
GitHub: srifqi
IRC: srifqi
In-game: srifqi

Re: Can't Change Table Value

by srifqi » Fri Aug 19, 2016 11:35

Have you tried to log the new mem[_] ? Does it's value change?
I'm from Indonesia! Saya dari Indonesia!
Terjemahkan Minetest!
Mods by me. Modifikasi oleh saya.

Pronounce my nick as in: es-rifqi (IPA: /es rifˈki/)
 

User avatar
rubenwardy
Member
 
Posts: 4500
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy

Re: Can't Change Table Value

by rubenwardy » Fri Aug 19, 2016 11:38

Try replacing _ with key?
 


Return to Modding Discussion

Who is online

Users browsing this forum: Bing [Bot] and 4 guests

cron