Master_11 Posted April 23, 2018 Share Posted April 23, 2018 Hi everyone, I am facing a small problem with tables, well I am not sure how to add something to a table by using addCommandHandler. Like, if this is my table local random = { -- x = y ['a'] = 'Two', ['b'] = 'One', ['c'] = 'Ten', } Then how can I add another line to this table by a command (addCommandHandler) For example If the command is "/add Q A" ~~~ ("/add d six") then it should add a new line to the table like -> local random = { ['a'] = 'Two', ['b'] = 'One', ['c'] = 'Ten', ['d'] = 'Six', } On the other hand, I have something like this in my mind for the command: local random = { -- x = y ['a'] = 'Two', ['b'] = 'One', ['c'] = 'Ten', ['d'] = 'Six', } function addabcdtotable(client, commandName, x, y) if (blabla ~= "18") then return false end if (x) then return false end --(Checks if (['d'] = 'Six',) already exists, if yes then it will simply return {where x = column and y = column-2}) Add-> (x,y) --(This will add {['d'] = 'Six',} to the table) end addCommandHandler("add", addabcdtotable) --For removing a line from table -> function removeabcdfromtable(client, commandName, x, y) if (blabla ~= "18") then return false end if (x) then --(Checks if (['d'] = 'Six',) already exists, if yes then it will be removed from the table {where x = column-1 and y = column-2) Remove-> (x, y) --(This will remove the whole line and fix the gap ['d'] = 'Six',) else return false end end end addCommandHandler("rem", removeabcdfromtable) -- NOW VERY IMPORTANT THE D IS ONLY THE EXAMPLE IT CAN BE ANYTHING LIKE (['ab'] = 'CD',) etc.. {X and Y is not the position please...} Link to comment
Bonsai Posted April 23, 2018 Share Posted April 23, 2018 Not exactly sure what you mean but adding and removing works like this: --add table['a'] = "abc" --remove table['a'] = nil --check if table['a'] then Link to comment
Master_11 Posted April 24, 2018 Author Share Posted April 24, 2018 Not working, I tried it You must use something to check the table name in which you want to add the line. Link to comment
Moderators IIYAMA Posted April 25, 2018 Moderators Share Posted April 25, 2018 I wouldn't say that so quickly. To me his example looks valid. /add d six random[x] = y (or overwrite, `set` would be a better command based on it's functionality) /rem d if random[x] then random[x] = nil end 1 Link to comment
Master_11 Posted April 26, 2018 Author Share Posted April 26, 2018 My Sincere apology, I got confused! and thanks! it is working well and fine, but If I may ask, is it possible to show the existing table into a GUI? Link to comment
Master_MTA Posted April 27, 2018 Share Posted April 27, 2018 4 hours ago, Master_11 said: into a GUI sure you can by using loop for example: ---------------define table local ta={} ---------------add Values to table addCommandHandler('insert',function(cmd,key,value) if key~=nil and value~=nil then ta[key]=value end end) --------------get all table values and keys local text='' for k,v in pairs(ta)do text=text..'['..k..']='..v..',' end Link to comment
Master_11 Posted May 1, 2018 Author Share Posted May 1, 2018 I successfully converted it into a GUI, now the problem I face is that when I restart the script the stuff added to the table by the GUI gets removed from the table. how can I solve that, like something which can save the table after every 24 hours or something? Link to comment
Moderators IIYAMA Posted May 1, 2018 Moderators Share Posted May 1, 2018 How about xml? https://wiki.multitheftauto.com/wiki/XML Link to comment
Master_11 Posted May 2, 2018 Author Share Posted May 2, 2018 Didn't really got what you guys meant, can you give me a small example? Link to comment
Moderators IIYAMA Posted May 2, 2018 Moderators Share Posted May 2, 2018 There are already examples on the wiki: -- Creates a file named "new.xml" with root node "newroot" and childnode "newchild". function createFileHandler() local rootNode = xmlCreateFile("new.xml","newroot") local childNode = xmlCreateChild(rootNode, "newchild") xmlSaveFile(rootNode) xmlUnloadFile(rootNode) end addCommandHandler("createfile", createFileHandler) example by Shadowstep07052 It shows you how to create a xml file, which has a new child. https://wiki.multitheftauto.com/wiki/XmlCreateFile XML syntax: https://www.tutorialspoint.com/xml/xml_syntax.htm Link to comment
ShayF2 Posted May 3, 2018 Share Posted May 3, 2018 (edited) 8 hours ago, IIYAMA said: There are already examples on the wiki: -- Creates a file named "new.xml" with root node "newroot" and childnode "newchild". function createFileHandler() local rootNode = xmlCreateFile("new.xml","newroot") local childNode = xmlCreateChild(rootNode, "newchild") xmlSaveFile(rootNode) xmlUnloadFile(rootNode) end addCommandHandler("createfile", createFileHandler) example by Shadowstep07052 It shows you how to create a xml file, which has a new child. https://wiki.multitheftauto.com/wiki/XmlCreateFile XML syntax: https://www.tutorialspoint.com/xml/xml_syntax.htm You there, why are you teaching him to use xml? xml sucks, it uses up 75-90% of the file just to create nodes WITHOUT the values themselves, and it's got complicated af functions. The best way to go for storage is the following: Internal - internal database with sqlite element data account data lua tables External - JSON MYSQL or MariaDB On 4/23/2018 at 15:48, Master_11 said: Hi everyone, I am facing a small problem with tables, well I am not sure how to add something to a table by using addCommandHandler. Like, if this is my table local random = { -- x = y ['a'] = 'Two', ['b'] = 'One', ['c'] = 'Ten', } Then how can I add another line to this table by a command (addCommandHandler) For example If the command is "/add Q A" ~~~ ("/add d six") then it should add a new line to the table like -> local random = { ['a'] = 'Two', ['b'] = 'One', ['c'] = 'Ten', ['d'] = 'Six', } On the other hand, I have something like this in my mind for the command: local random = { -- x = y ['a'] = 'Two', ['b'] = 'One', ['c'] = 'Ten', ['d'] = 'Six', } function addabcdtotable(client, commandName, x, y) if (blabla ~= "18") then return false end if (x) then return false end --(Checks if (['d'] = 'Six',) already exists, if yes then it will simply return {where x = column and y = column-2}) Add-> (x,y) --(This will add {['d'] = 'Six',} to the table) end addCommandHandler("add", addabcdtotable) --For removing a line from table -> function removeabcdfromtable(client, commandName, x, y) if (blabla ~= "18") then return false end if (x) then --(Checks if (['d'] = 'Six',) already exists, if yes then it will be removed from the table {where x = column-1 and y = column-2) Remove-> (x, y) --(This will remove the whole line and fix the gap ['d'] = 'Six',) else return false end end end addCommandHandler("rem", removeabcdfromtable) -- NOW VERY IMPORTANT THE D IS ONLY THE EXAMPLE IT CAN BE ANYTHING LIKE (['ab'] = 'CD',) etc.. {X and Y is not the position please...} Okay and you here, this actually very simple, I'll give you some premium code a little tip before hand however ['a'] is the same as .a, I suggest you start using that. You only want to use [''] method when you have spaces in your string. I'll also show you how to combine commands into a single function and work a table properly. local items = {} function commands(player,cmd,name,value) if cmd == 'add' then if name and value and tonumber(value) then if not items[name] then items[name] = tonumber(value) end end elseif cmd == 'remove' then if name and items[name] then for i=1,#items do if items[i] == items[name] then table.remove(items,i) end end end elseif cmd == 'test' then iprint(items)-- only works on MTA 1.5.5+ servers end end local cmds = {'add','remove','test'} for i=1,#cmd do addCommandHandler(cmds[i],commands) end I hoped this has helped you, if not send me a private message. Example Command: /add six 6 Example Command: /remove six And to the rest of you out there, for the love of god please don't use xml, and just because you set a nil value to an item in a table doesn't mean that the item is gone, it's still there it just doesn't have a value.. Edited May 3, 2018 by ShayF Link to comment
Moderators IIYAMA Posted May 3, 2018 Moderators Share Posted May 3, 2018 @ShayF It is not "you" for you, it is IIYAMA. A little bit respect please. Why XML? - client and server-side support.(giving the user more options) - Related to html, so more chance that the user would understand it. - Data read able without tools. - lots of MTA documentation. - not required to bind data to a client account. So yes why xml? Because this user doesn't care about optimization or file size. He want to learn how to do it and I gave him this solution because it matches his profile. Which is only to learn XML. If you think that the user wants to learn 3 different things: - accountdata - Data communication (server/client) The end user is using a GUI. - JSON Then hmmm, why not everything one step at the time? Link to comment
ShayF2 Posted May 3, 2018 Share Posted May 3, 2018 (edited) 5 hours ago, IIYAMA said: @ShayF It is not "you" for you, it is IIYAMA. A little bit respect please. Why XML? - client and server-side support.(giving the user more options) - Related to html, so more chance that the user would understand it. - Data read able without tools. - lots of MTA documentation. - not required to bind data to a client account. So yes why xml? Because this user doesn't care about optimization or file size. He want to learn how to do it and I gave him this solution because it matches his profile. Which is only to learn XML. If you think that the user wants to learn 3 different things: - accountdata - Data communication (server/client) The end user is using a GUI. - JSON Then hmmm, why not everything one step at the time? json is not that hard to use, its compact and really simplified. You make a lua table, then make a json file, convert the lua table to json with toJSON and then write it to the json file, and when you want to load it you just use fromJSON to convert it back to a lua table. That's why I prefer JSON over everything. local data = { {'blah','blah','blah'}, {'blah','blah','blah'} } function save() local file = fileOpen('save.json') or fileCreate('save.json') fileWrite(toJSON(data),file) fileClose(file) end function load() if fileExists('save.json') then local file = fileOpen('save.json') local size = fileGetSize(file) local buffer = fileRead(file,size) data = fromJSON(buffer) fileClose(file) end end If you were to use different saving methods than xml you would see why I treat it with disrespect. I personally prefer json and mysql. Edited May 3, 2018 by ShayF 2 Link to comment
ShayF2 Posted May 3, 2018 Share Posted May 3, 2018 (edited) For all of you people out there needing to save data, just put it in a lua table and save it with JSON, that is if you want it saved on the server or on the client machine, it's the easiest way to do it. Do NOT use xml, xml is the worst saving method I have ever seen. It's complicated af, it uses tags just like html but other than that it's not even remotely close to html. XML - Hard to learn Hard to use Requires a LOT of functions Uses 75-90% of the file to simply create nodes and or categories Requires more than 3 XML functions to save or load data MTA has functions for xml but without them it is not compatible. JSON or SQLite for permanent local storage. MYSQL for permanent external storage. Element Data for temporary storage stored on element. Account Data for permanent storage stored in the servers internal.db registered to a specific account. Edited May 3, 2018 by ShayF Link to comment
Moderators IIYAMA Posted May 3, 2018 Moderators Share Posted May 3, 2018 You may have disrespect for XML, but I am not XML... lol Databases are indeed better, just not for new users. XML hard? I think that is a personal opinion. More writing code for XML? Yes indeed, but you will know what you will be saving. If you are storing large objects, I am very sure that it might get dirt in it. (Like user data) JSON? Replacement of XML from Javascript. But can be used within XML. Which means you can parse object models of your choice. Text file client-side? Bad practice for small updates. Parsing JSON is slow. \/ If I am would use an interface client-side for client-side data. Would you resave the whole object model for every small update? I do not recommend that. JSON Problem with lua objects. {G = 33, [2]=444} After saving + parsing this, the index 2 will be converted to a string. Which can be be a surprising annoying. You might be right about that XML is not as flexible as people would hope. But there are really benefits for it, which it seems you have just to discover. Let's end the discussion here. Link to comment
Master_11 Posted May 4, 2018 Author Share Posted May 4, 2018 I already started working with JSON, I'll update this topic if I face any problem with the code, thanks ShyF and IIYAMA. You guys helped me a lot. 1 Link to comment
Master_11 Posted May 4, 2018 Author Share Posted May 4, 2018 Could not find the modify buttons ~ So; @ShyF while working on JSON, I found an error on your code, just mentioning if someone could not figure it out in near future. It's fileWrite(file, toJSON(data)) Not fileWrite(toJSON(data),file) Link to comment
ShayF2 Posted May 4, 2018 Share Posted May 4, 2018 1 hour ago, Master_11 said: Could not find the modify buttons ~ So; @ShyF while working on JSON, I found an error on your code, just mentioning if someone could not figure it out in near future. It's fileWrite(file, toJSON(data)) Not fileWrite(toJSON(data),file) yeah thanks bud, json ftw. Link to comment
Master_11 Posted May 4, 2018 Author Share Posted May 4, 2018 Thanks, everyone for helping! Moderator may lock this topic. 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