• Loading data from JSON.
    1 replies, posted
Hello Facepunch I'm wondering if anyone could help me with the issue I'm having. I'm doing a little test to see if I can assign a value to a door, and when I restart the server it'll be still assigned. This is what I currently have. [CODE] -- Where we assign the values. Doors[eid] = {ent=ent,cost=val,renter=false,roomnum=roomnum} // above is the table we add the values too and we write. // The values above are added like so, /door (price) (roomid) [/CODE] [CODE] -- Below this is where we save our doors. -- we save the doors doing /savedoor function saveDoorValuesForMap(ply, arg) local hSave = util.TableToJSON( Doors ) -- Convert the player table to JSON file.Write( "frank_data/hotel_data.txt", hSave ) -- Write to .txt end [/CODE] [CODE] -- Loading the assigned doors. function loadSavedDoorsValues() if !file.Exists( "frank_data/hotel_data.txt", "DATA" ) then file.CreateDir( "frank_data" ) else local doorData = util.JSONToTable( file.Read( "frank_data/hotel_data.txt", "DATA" ) ) for k, door in pairs( ents.GetAll() ) do door.cost = doorData["cost"] k.cost = doorData["cost"] PrintTable(doorData) end end end hook.Add("Initialize", "frank-loadDoor", loadSavedDoorsValues) [/CODE] Help is much apprecaited, thank you.
To be able to save data for all doors for the map you really should use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/MapCreationID]Entity:MapCreationID[/url] and [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/ents/GetMapCreatedEntity]ents.GetMapCreatedEntity[/url] instead of that, not with the entity id, because that can change eventually over time, so instead it would be something like [CODE] -- Where we assign the values. Doors[mapCreationID] = {cost=val,renter=false,roomnum=roomnum} [/CODE] [CODE] function loadSavedDoorsValues() if !file.Exists( "frank_data/hotel_data.txt", "DATA" ) then file.CreateDir( "frank_data" ) else local doorData = util.JSONToTable( file.Read( "frank_data/hotel_data.txt", "DATA" ) ) for mapid, data in pairs(doorData) ) do local door = ents.GetMapCreatedEntity(mapid) if not IsValid(door) then return end door.cost = doorData["cost"] k.cost = doorData["cost"] end end end hook.Add("Initialize", "frank-loadDoor", loadSavedDoorsValues) [/CODE]
Sorry, you need to Log In to post a reply to this thread.