Jump to content

HELP!


WiBox

Recommended Posts

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 = { ... } ?

  1. 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

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 by MrTasty
  • Thanks 1
Link to comment
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 by MrTasty
  • Thanks 1
Link to comment

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 by SSKE
Link to comment
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

{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

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 by MrTasty
Link to comment

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...