So i have some code that creates a txt file that contains info i need to save
It saves it in Json format
Example
{"scrapMetal":1}
I have a function to load the data and convert it back to a table but i cant then apply maths to it.
How would i do that?
[code]
function saveInfo(ply)
local info = {} -- Initialize the table (local because we don't need it anywhere else)
-- Store any information you want within the table, as follows.
info.scrapMetal = 1
info = util.TableToJSON(info) -- Convert the info stored in the table to JSON so we can save it to the file
file.Write('scrap/zombierp_info_'..ply:UniqueID()..'.txt', info) -- Write the information stored into the players unique file
end
function loadInfo(ply)
if !ply then return end
ply.storedInfo = {}
local file = file.Read('zombierp_info_'..ply:UniqueID()..'.txt', 'DATA') -- Read the players info file
file = util.JSONToTable(file) -- Convert the information stored from JSON to a table
ply.storedInfo = file -- Now you can get access to this information anywhere once you call the function
end
[/code]
[QUOTE=Zeus2;50776702]
i cant then apply maths to it.[/QUOTE]
Can you be more precise about it?
Also, you should really change this code:
[CODE]if !ply then return end[/CODE]
To:
[CODE]if !IsValid(ply) and (!ply:IsPlayer()) then return end[/CODE]
{"scrapMetal":1}
That is saved
I need to +1 to that value
So i convert the
{"scrapMetal":1}
To a table
I then try to add one to the table but it gives me an error
attempt to perform arithmetic on global 'file' (a table value)
So what im asking if how can i change the value in the table using maths?
[QUOTE=Zeus2;50776761]
I then try to add one to the table but it gives me an error
attempt to perform arithmetic on global 'file' ([B]a table value[/B])
[/QUOTE]
Wait?? You did a [B][I]+1[/I][/B] [B][U]on a table[/U][/B] likes this??
[CODE]ply.storedInfo = file+1 -- The horror if you did it![/CODE]
Instead of:
[CODE]
ply.storedInfo = file
ply.storedInfo.scrapMetal=ply.storedInfo.scrapMetal+1
[/CODE]
[QUOTE=Gedo789;50776789]Wait?? You did a [B][I]+1[/I][/B] [B][U]on a table[/U][/B] likes this??
[CODE]ply.storedInfo = file+1 -- The horror if you did it![/CODE]
Instead of:
[CODE]
ply.storedInfo = file
ply.storedInfo.scrapMetal=ply.storedInfo.scrapMetal+1
[/CODE][/QUOTE]
You forgot that [URL="http://wiki.garrysmod.com/page/Category:file"]file[/URL] is a default table in GLua.
I did do it like that. But the thing is i need it to +1 when the entity is used.
That means i need the +1 to be in the save function so i call it when the entity is used and it updates the value.
[editline]26th July 2016[/editline]
[QUOTE=Liquidsocks;50776825]You forgot that [URL="http://wiki.garrysmod.com/page/Category:file"]file[/URL] is a default table in GLua.[/QUOTE]
I changed that to data a second ago.
So its kind of working
But when i pickup the entity it just saves this
{"scrapMetal":[]}
[code]
function saveInfo(ply)
local info = {} -- Initialize the table (local because we don't need it anywhere else)
-- Store any information you want within the table, as follows.
info.scrapMetal = ply.storedInfo
info = util.TableToJSON(info) -- Convert the info stored in the table to JSON so we can save it to the file
file.Write('scrap/zombierp_info_'..ply:UniqueID()..'.txt', info) -- Write the information stored into the players unique file
end
function loadInfo(ply)
if !ply then return end
ply.storedInfo = {}
local data = file.Read('zombierp_info_'..ply:UniqueID()..'.txt', 'DATA') -- Read the players info file
data = util.JSONToTable(data) -- Convert the information stored from JSON to a table
ply.storedInfo.scrapMetal = data -- Now you can get access to this information anywhere once you call the function
data = data + 1
end
[/code]
any ideas on why?
[code]
function saveInfo(ply)
if !IsValid(ply) and (!ply:IsPlayer()) then return end
local info = ply.storedInfo or {} -- Get player's info OR Initialize the table (local because we don't need it anywhere else)
-- Store any information you want within the table, as follows.
info.scrapMetal = info.scrapMetal or 0 -- If we got somethings, them, let it alone, ELSE, set it to 0
info = util.TableToJSON(info) -- Convert the info stored in the table to JSON so we can save it to the file
file.Write('scrap/zombierp_info_'..ply:UniqueID()..'.txt', info) -- Write the information stored into the players unique file
end
function loadInfo(ply)
if !IsValid(ply) and (!ply:IsPlayer()) then return end
ply.storedInfo = {}
local data = file.Read('zombierp_info_'..ply:UniqueID()..'.txt', 'DATA') -- Read the players info file
data = util.JSONToTable(data) -- Convert the information stored from JSON to a table
ply.storedInfo.scrapMetal = data -- Now you can get access to this information anywhere once you call the function
data = data + 1
end
[/code]
that just sets it to 0
[QUOTE=Zeus2;50776863]that just sets it to 0[/QUOTE]
No. You didn't understand what I wrote.
[CODE]
local info = ply.storedInfo or {}
info.scrapMetal = info.scrapMetal or 0
[/CODE]
This code means that if we got a "non-nil" value, then, it keeps it.
The "or" there, is a fallback in case it couldn't get a satisfying value. (somethings else than "nil")
You wouldn't get a error OR a invalid value, don't you?
If it returns a 0, then it's working. And that also means, that the player didn't collect scrap metal.
no i mean i tried it and it just sets it to 0. If i picksome up it stays as 0.
[QUOTE=Zeus2;50776889]no i mean i tried it and it just sets it to 0. If i picksome up it stays as 0.[/QUOTE]
Ok. I'm going to test it.
[CODE]
] lua_run print("testing without player collecting scrap metal") local ply = Entity(1) local info = ply.storedInfo or {} info.scrapMetal = info.scrapMetal or 0 print(info.scrapMetal)
> print("testing without player collecting scrap metal") local ply = Entity(1) local info = ply.storedInfo or {} info.scrapMetal = info.scrapMetal or 0 print(info.scrapMetal)...
testing without player collecting scrap metal
0
] lua_run print("Setting Entity(1)'s scrap metal to 10...") Entity(1).storedInfo={} Entity(1).storedInfo.scrapMetal=10
> print("Setting Entity(1)'s scrap metal to 10...") Entity(1).storedInfo={} Entity(1).storedInfo.scrapMetal=10...
Setting Entity(1)'s scrap metal to 10...
] lua_run print("testing with player who got scrap metal") local ply = Entity(1) local info = ply.storedInfo or {} info.scrapMetal = info.scrapMetal or 0 print(info.scrapMetal)
> print("testing with player who got scrap metal") local ply = Entity(1) local info = ply.storedInfo or {} info.scrapMetal = info.scrapMetal or 0 print(info.scrapMetal)...
testing with player who got scrap metal
10
[/CODE]
Oh...
well it didnt work so i dont know whats wrong
Try this:
[CODE]
local function saveInfo(ply)
if !IsValid(ply) or !ply:IsPlayer() then return end
local info = ply.storedInfo or {}
info.scrapMetal = info.scrapMetal or 0
file.Write('scrap/zombierp_info_'..ply:UniqueID()..'.txt', util.TableToJSON(info))
end
local function loadInfo(ply)
if !IsValid(ply) or !ply:IsPlayer() then return end
local data = file.Read('zombierp_info_'..ply:UniqueID()..'.txt', 'DATA')
ply.storedInfo = data
end
local function setMetal( ply, metalnum )
if !IsValid(ply) or !ply:IsPlayer() then return end
ply.storedInfo = ply.storedInfo or {}
ply.storedInfo.scrapMetal = metalnum
end
[/CODE]
The reason the last code didn't work was because you were trying to add 1 to a table value for some reason
You got somethings wrong outside of this code you showed Zeus2.
:snip: automerge
[code]
function ENT:Use(a , c)
if self:GetModel() == "models/props_junk/wood_pallet001a.mdl" then
end
if self:GetModel() == "models/props_junk/wood_pallet001a.mdl" then
end
if self:GetModel() == "models/props_junk/wood_pallet001a.mdl" then
end
self:Remove()
saveInfo(a)
end
function VectorRand()
return Vector( math.Rand( -2871, -1175 ), math.Rand( -2403, -791 ), -35 )
end
hook.Add('PlayerInitialSpawn', 'datahook', function(ply) -- This creates the files for the player to load/save information to on the player's initial spawn
if !file.Exists('scrap/zombierp_info_'..ply:UniqueID()..'.txt', 'DATA') then -- If the players file doesn't exist continue
file.Write('scrap/zombierp_info_'..ply:UniqueID()..'.txt', '[]') -- Create the file
end
end)
function saveInfo(ply)
local info = {} -- Initialize the table (local because we don't need it anywhere else)
-- Store any information you want within the table, as follows.
info.scrapMetal = ply.storedInfo
info = util.TableToJSON(info) -- Convert the info stored in the table to JSON so we can save it to the file
file.Write('scrap/zombierp_info_'..ply:UniqueID()..'.txt', info) -- Write the information stored into the players unique file
end
function loadInfo(ply)
if !ply then return end
ply.storedInfo = {}
local data = file.Read('zombierp_info_'..ply:UniqueID()..'.txt', 'DATA') -- Read the players info file
data = util.JSONToTable(data) -- Convert the information stored from JSON to a table
ply.storedInfo.scrapMetal = data -- Now you can get access to this information anywhere once you call the function
data = data + 1
end
[/code]
that is everything that involves the metal saving and stuff
[CODE]
local data = file.Read('zombierp_info_'..ply:UniqueID()..'.txt', 'DATA')
data = util.JSONToTable(data)
ply.storedInfo.scrapMetal = data -- why are you setting their metal number to be a table of data?
data = data + 1
[/CODE]
Why are you adding 1 to a table? Surprisingly, JSONToTable returns a table, and you're adding 1 to that table. How is that meant to work?
[editline]26th July 2016[/editline]
Try the thing I posted before:
[CODE]
local function saveInfo(ply)
if !IsValid(ply) or !ply:IsPlayer() then return end
local info = ply.storedInfo or {}
info.scrapMetal = info.scrapMetal or 0
file.Write('scrap/zombierp_info_'..ply:UniqueID()..'.txt', util.TableToJSON(info))
end
local function loadInfo(ply)
if !IsValid(ply) or !ply:IsPlayer() then return end
local data = file.Read('zombierp_info_'..ply:UniqueID()..'.txt', 'DATA')
data.scrapMetal = data.scrapMetal or 0
ply.storedInfo = data
end
local function setMetal( ply, metalnum ) -- This sets a player's metal to be a certain number
if !IsValid(ply) or !ply:IsPlayer() then return end
ply.storedInfo = ply.storedInfo or {}
ply.storedInfo.scrapMetal = metalnum -- you could make this add 1 metal instead by doing ply.storedInfo.scrapMetal = ply.storedInfo.scrapMetal + 1
end
[/CODE]
tried your way and it just sticks at 0
Did you try the setMetal thing?
Sorry, I never added 1 to the metal total since I didn't really know why you were doing that
wait im stupid
i need to call the setMetal function dont i
:snip: automerge again
And yeah, try doing the setMetal thing. I haven't tested it though, but theoratically I think it should work
It gives me an error
attempt to call global 'setMetal' (a nil value)
do i call the function like this?
[code]
setMetal(a,1) -- A is the player and 1 is the amount of metal
[/code]
Sorry, I made the functions local instead of global since I like doing that sort of thing- but if it gives you an error then I guess global functions are the way to go for what you're doing. Anyway, if it worked before when the functions were global then this should work:
[CODE]
function saveInfo(ply)
if !IsValid(ply) or !ply:IsPlayer() then return end
local info = ply.storedInfo or {}
info.scrapMetal = info.scrapMetal or 0
file.Write('scrap/zombierp_info_'..ply:UniqueID()..'.txt', util.TableToJSON(info))
end
function loadInfo(ply)
if !IsValid(ply) or !ply:IsPlayer() then return end
local data = file.Read('zombierp_info_'..ply:UniqueID()..'.txt', 'DATA')
data.scrapMetal = data.scrapMetal or 0
ply.storedInfo = data
end
function setMetal( ply, metalnum ) -- Yep, A is the player and 1 is the metal
if !IsValid(ply) or !ply:IsPlayer() then return end
ply.storedInfo = ply.storedInfo or {}
ply.storedInfo.scrapMetal = metalnum -- you could make this add 1 metal instead by doing ply.storedInfo.scrapMetal = ply.storedInfo.scrapMetal + 1
end
[/CODE]
now it sets it to one no matter what -_-
Sorry, but I'm confused- what do you want to happen exactly? If you set it to 1 then it'll stay at 1 until you change it
oh wait
i just need to change this
[code]
ply.storedInfo.scrapMetal = metalnum
[/code]
to this
[code]
ply.storedInfo.scrapMetal = play.storedInfo.scrapMetal + metalnum
[/code]
[editline]26th July 2016[/editline]
Thank you guys so much it works now :)
Sorry, you need to Log In to post a reply to this thread.