English [Solved] How to insert missing values to table?

7 replies
Goto Page
To the start Previous 1 Next To the start
Up
Mora
User
Offline Off
Guys, how's possible to check if a playertable has values from other table? For example, the table that is copied when a NEW player is joined:
Code:
1
2
3
4
5
6
7
PlayerBasicStats = {
Mora = 24,
Inventory = {},
Number = 1322,
String = "String",
Table = {1,{s}},
}

And for example one is joined but don't have any of them in the table:
Code:
1
2
3
4
Player = {
Mora = 24,
Inventory = {},
}

How's possible to check if player has no Number, String and Table values? For example through a loop, because such thing like
Code:
1
2
if not Player[id].Number then
Player[id].Number = 1322

requires manual coding, but I'd like to make it auto, whenever player joins, so the special code will check if Player has no specific value and insert it.

It required because anytime I add new items in
PlayerBasicStats
, the player that already has a save file wouldn't have these and I need to delete saves to load all items from
PlayerBasicStats
, so I need them to be inserted, if player has no values.

Any ideas?
Well, Idk if I explained it good enough to understand, but if You have a questions ask.
edited 1×, last 01.06.22 09:06:58 am
31.05.22 06:26:39 pm
Up
Bowlinghead
User
Offline Off
https://stackoverflow.com/questions/55108794/what-is-the-difference-of-pairs-vs-ipairs-in-lua

Isnt pairs exactly what you need?

Code:
1
2
3
4
5
6
--untested
for k,v in pairs(PlayerBasicStats) do
     if (v == nil) then
          PlayerBasicStats.k = 1337;
     end
end
Share time limited free games here
31.05.22 07:57:08 pm
Up
Gaios
Security Supporter
Offline Off
Something like @user Bowlinghead
Code:
1
2
3
4
5
for _, i in pairs(thePlayerOrSth) do
     if (_ == 'theKey' and not i) then
          thePlayerOrSth.theKey = 1337
     end
end
31.05.22 10:33:58 pm
Up
Mora
User
Offline Off
I mean, Is possible to insert items from PlayerBasicStats into Player table, if Player table has no items that PlayerBasicStats has without modifying manually Player table. And anytime I add new item (s) into PlayerBasicStats, the Player table will copy this(these) new item(s) for example in join hook
31.05.22 11:03:28 pm
Up
Mami Tomoe
User
Offline Off
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
local s = 'example' -- Used below in defaultPlayerData['String'], can be deleted.
local defaultPlayerData = {
     Mora = 'noob',
     Inventory = { },
     Number = 69,
     String = "mami best girl",
     Table = { 1, { s } },
}
local playerData = {
     -- On join:
     --[[
          init_player(playerId)
     ]]

     -- On leave:
     --[[
          playerData[playerId] = nil
     ]]
     -- You can create a function for
     -- the above, but I decided not to.
}

local function init_player(p)
     -- Define player entry.
     playerData[p] = { }

     -- Load player data from file.
     -- !!!LOAD PLAYER DATA FROM FILE HERE!!!
     -- !EXACTLY, RIGHT HERE!

     -- Set default values.
     local playerEntry = playerData[p]

     for key, defaultValue in pairs(defaultPlayerData) do
          playerEntry[key] = playerEntry[key] or defaultValue
     end
end

Look at me standing, here on my own again
01.06.22 07:48:53 am
Up
ohaz
User
Offline Off
https://www.lua.org/pil/16.2.html
Just use "inheritance"
https://ohaz.engineer - Software Engineering
01.06.22 07:51:52 am
Up
Mora
User
Offline Off
Yeah, this thing is exactly what I need.
Code:
1
2
3
4
5
local playerEntry = playerData[p]

     for key, defaultValue in pairs(defaultPlayerData) do
          playerEntry[key] = playerEntry[key] or defaultValue
     end

In fact I did something like that myself, but I get confused and thought that I'm wrong 100% and this thing wouldn't work. Anyway You're (or Mami or whatever is it) best girl, okay. Just 4 lines and it working like a swiss fucking watch. Thank you


@user ohaz: oh, bro, You ninjad my reply. Well, I'm not experienced enough to understand metatables and OOP thingy(that is my problem and still, mb I'm dumb). But I'll read it fully and try to learn something, thank You.

Thread might be closed, the problem is solved √
edited 1×, last 01.06.22 09:07:50 am
01.06.22 10:52:20 am
Up
Mami Tomoe
User
Offline Off
My approach won't work for boolean values perfectly as I didn't use a
nil
check, so if the value is
false
it would be turned into the default as well.
If this bothers you, let me know.
Look at me standing, here on my own again
To the start Previous 1 Next To the start