WiBox Posted June 27, 2018 Share Posted June 27, 2018 Hello... I made a panel which add zone on the coordination I write in it, but I just need to ask! Is there a way the zone I create his information get auto-writted in this zones = { ... } ? zones = { --{id,name,owner,ctrl,price,objects,radio,x,y,sx,sy} -- {id = 1, name = "Test zone", owner = "TsT", ctrl = "", price = 10000, objects = 0, radio = "", x = 1577, y = 1723, sx = 60, sy = 140} } Link to comment
Addlibs Posted June 27, 2018 Share Posted June 27, 2018 (edited) You could simply use table.insert to add the new zone into the table, however that would only last until the server or client (depending whether the script file is server side or client side) is restarted. If you want it to be permanent, you would have to either: 1) have the script edit itself and write into new lines in the source file, or 2) implement a saving system so you can save and load the table from a different file that's easier to edit The second option could be easily accomplished by simply writing the toJSON notation of the zones table into a file and then load it by assigning the zones table to the result of fromJSON of the save file's contents. Edited June 27, 2018 by MrTasty 1 Link to comment
WiBox Posted June 28, 2018 Author Share Posted June 28, 2018 Thanks for helping but... I didn't understand on https://wiki.multitheftauto.com how to use toJSON or fromJSON, I would really appreciate if you give me an example of how to do that second option.. Link to comment
Addlibs Posted June 28, 2018 Share Posted June 28, 2018 (edited) local zones = { -- whatever content there would be } function saveZones() if fileExists("zones.json") then fileDelete("zones.json") -- if the file exists, delete it to clear it end local f = fileCreate("zones.json") if not f then outputDebugString("failed to create zones.json") return end fileWrite(f, toJSON(zones)) -- JSONify the zones table and write it to f (file handle for zones.json) fileClose(f) -- make sure to close the file so there's no memory leaks end function loadZones() if not fileExists("zones.json") then return end -- no save file, abort here local f = fileOpen("zones.json", true) if not f then outputDebugString("failed to open zones.json for reading") return end local content = fileRead(f, fileGetSize(f)) zones = fromJSON(content) -- assign the zones table to the de-JSONified content of the zones.json file fileClose(f) -- make sure to close the file so there's no memory leaks end I haven't actually tested this but it should work unless I misspelled some function or forgot an argument somewhere. Also worth noting, toJSON does not work with elements and userdata - you cannot place a player element there but you could place the player name. Edited June 28, 2018 by MrTasty 1 Link to comment
WiBox Posted June 28, 2018 Author Share Posted June 28, 2018 (edited) Thanks for this so much because in future I'll need that. But the thing is I didn't ask to save the zone, I asked if I added a zone using a panel from inside the game how I can auto add it in the table zone = {} Edited June 28, 2018 by SSKE Link to comment
WiBox Posted June 28, 2018 Author Share Posted June 28, 2018 function showZoneOutput() guiSetText(zoneCreator.edit[1], string.format('%.3f', x)) guiSetText(zoneCreator.edit[2], string.format('%.3f', y)) guiSetText(zoneCreator.edit[3], string.format('%.3f', w)) guiSetText(zoneCreator.edit[4], string.format('%.3f', d)) end Those are the format of the x, y, sx, sy. I want to add those into the table zone.. those are in client side, and I want to add them into the server side in the table zones = {} is there a way so I do that? Link to comment
Addlibs Posted June 28, 2018 Share Posted June 28, 2018 Ah. Well, you can use triggerServerEvent with the data and use table.insert to add that data into zones table, unless you want to actually auto-edit the script file to make these entries actually written into the file. Link to comment
WiBox Posted June 29, 2018 Author Share Posted June 29, 2018 {Ah. Well, you can use triggerServerEvent with the data and use table.insert to add that data into zones table} I need to do that, but the thing is how to make the format? I mean in the zones table there is: {id = 1, name = "", owner = "", ctrl = "", price = 5000, objects = 0, radio = "", x = , y = , sx = , sy = } I just need to put example this line into the table with table.insert but on the wiki they didn't give a example with triggerServerEvent.. And I'm still a newbie at coding.. That's why I appreciate if you gave me a small example Thanks~ Link to comment
Addlibs Posted June 29, 2018 Share Posted June 29, 2018 (edited) Something like this should work -- on the client triggerServerEvent("addNewZone", localPlayer, zonename, zonecost, x, y, w, d) -- on the server addEvent("addNewZone", true) addEventHandler("addNewZone", root, function(title,cost,x,y,w,d) if client ~= source then return end -- make sure the client sending this event isn't trying to frame someone into it by using a different source element. `client` cannot be faked, `source` can be. local zone = { id = __, name = title, owner = getPlayerName(client), ctrl = __, price = cost, objects = __, radio = __, x = x, y = y, sx = w, sy = d } table.insert(zones, zone) outputChatBox(getPlayerName(client).." added a new zone.", root, 255, 127, 0, true) end ) Edited June 29, 2018 by MrTasty Link to comment
WiBox Posted June 29, 2018 Author Share Posted June 29, 2018 Thanks, I appreciate what you did for me, Thanks so much Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now