Make a table/variable name out of the table/variable content?
9 replies, posted
So here are some examples to show what I am talking about
[CODE]
local playerid = "lol"
LocalPlayer().players.playerid = "pineapple"
print(LocalPlayer().players.lol)
[/CODE]
Expected output: pineapple
[CODE]
button.id = "player"
function button:DoClick()
LocalPlayer().players.button.id = "lelz"
end
print(LocalPlayer().players.player)
[/CODE]
Expected output: lelz
[CODE]
button.xd = 78
function button:DoClick()
LocalPlayer().players.button.xd = "meme"
end
print(LocalPlayer().players.78)
[/CODE]
Expected output: meme
Player.players is nil and so is Player.players.button. They need to be tables.
You can't use variables like that. All three will be nil because they are undefined on the player table. You would have to do:
[code]LocalPlayer().players.lol = "pineapple"[/code]
For the first example if you wanted the expected output.
[QUOTE=txike;52636750]Player.players is nil and so is Player.players.button. They need to be tables.[/QUOTE]
ah sorry they are defined but I forgot to put them in the examples.
[QUOTE=code_gs;52636755]You can't use variables like that. All three will be nil because they are undefined on the player table. You would have to do:
[code]LocalPlayer().players.lol = "pineapple"[/code]
For the first example if you wanted the expected output.[/QUOTE]
And here is the problem, I want to make a history for each player that the player has chatted with
[CODE]
LocalPlayer().tablet_text_my.Mike = {}
LocalPlayer().tablet_text_my.Bot01 = {}
LocalPlayer().tablet_text_my.Bot02 = {}
[/CODE]
LocalPlayer is not a global player entity: it is a single player -- specifically, the clientside one. I would do this serverside:
[code]local tChatLogs = {}
hook.Add("OnEntityCreated", "ChatLogs", function(pEntity)
if (pEntity:IsPlayer()) then
tChatLogs[pEntity] = {}
end
end)
hook.Add("EntityRemoved", "ChatLogs", function(pEntity)
if (pEntity:IsPlayer()) then
tChatLogs[pEntity] = nil
end
end)
hook.Add("PlayerSay", "ChatLogs", function(pPlayer, sText)
table.insert(tChatLogs[pEntity], sText)
end)[/code]
Note that this does have a few restrictions:
- It is not run after all PlayerSay hooks, so some commands could be logged. You could get around this with a clientside OnPlayerChat/ChatText hook, but the logs would differ between each player
- It is not networked, and must be done so manually. The alternative provided above applies here, as well
- It is not persistent, and will clear between maps and crashes. Alternative is to use a text file to store the strings, or a similar structure.
The best implementation will depend on your specific case.
[QUOTE=code_gs;52636882]LocalPlayer is not a global player entity: it is a single player -- specifically, the clientside one. I would do this serverside:
[code]local tChatLogs = {}
hook.Add("OnEntityCreated", "ChatLogs", function(pEntity)
if (pEntity:IsPlayer()) then
tChatLogs[pEntity] = {}
end
end)
hook.Add("EntityRemoved", "ChatLogs", function(pEntity)
if (pEntity:IsPlayer()) then
tChatLogs[pEntity] = nil
end
end)
hook.Add("PlayerSay", "ChatLogs", function(pPlayer, sText)
table.insert(tChatLogs[pEntity], sText)
end)[/code]
Note that this does have a few restrictions:
- It is not run after all PlayerSay hooks, so some commands could be logged. You could get around this with a clientside OnPlayerChat/ChatText hook, but the logs would differ between each player
- It is not networked, and must be done so manually. The alternative provided above applies here, as well
- It is not persistent, and will clear between maps and crashes. Alternative is to use a text file to store the strings, or a similar structure.
The best implementation will depend on your specific case.[/QUOTE]
But that's clientside derma with DTextEntry
[QUOTE=matik04;52637121]But that's clientside derma with DTextEntry[/QUOTE]
What? There is no derma or DTextEntry.
[QUOTE=G4MB!T;52637142]What? There is no derma or DTextEntry.[/QUOTE]
They were examples on how I think it'll work or to show what I mean
[QUOTE=thejjokerr;52637306]Is this what you mean?
[CODE]
local playerid = "lol"
LocalPlayer().players[playerid] = "pineapple"
print(LocalPlayer().players.lol)
[/CODE]
output: pineapple
[CODE]
button.id = "player"
function button:DoClick()
LocalPlayer().players[self.id] = "lelz"
end
print(LocalPlayer().players.player)
[/CODE] output: lelz
[CODE]
button.xd = 78
function button:DoClick()
LocalPlayer().players.button[xd] = "meme"
end
print(LocalPlayer().players[78])
[/CODE] output: meme[/QUOTE]
You're my savior thank you, I mean... it isn't what I asked for but it'll still work.
Sorry, you need to Log In to post a reply to this thread.