Andmann Posted August 5, 2016 Share Posted August 5, 2016 (edited) didn't find anything on google: Do all player Ids have the same length? Working on a ladder table that is stored in a csv and wasn't sure how to best slice the strings when reading from the file. problem Solved: function ladder:SliceString(s, c, t) t = t or {} local subs = "" if s == nil or type(s) ~= "string" then print(s) return t else local i = s:find(c) print("found", c, "at", i) if i ~= nil then local j = i-1 subs = s:sub(1,(i-1)) print("adding", subs, "to table") s = s:sub(i+1) else subs = s s = nil end table.insert(t,(#t+1) ,subs) print(t[#t]) return SliceString(s, c, t) end end Edited August 5, 2016 by Andmann problem solved =) Link to comment Share on other sites More sharing options...
Joachim Posted August 5, 2016 Share Posted August 5, 2016 I think so, but I wouldn't rely on it. You can easily use a delimiter for CSV files, such as a comma or semicolon. So why bother? Link to comment Share on other sites More sharing options...
CarlZalph Posted August 5, 2016 Share Posted August 5, 2016 Every Klei ID I have seen is of fixed length, though as Joachim there stated it's better practice to use a delimiter to ensure it's future proof lest you need to reformat your database (not fun). Link to comment Share on other sites More sharing options...
Andmann Posted August 5, 2016 Author Share Posted August 5, 2016 (edited) you mean with format( formatstring, e1,e2,...) edit: no i guess gsub is the one I'm after? lua is just absolute newgrounds to me ~.~ Edited August 5, 2016 by Andmann Link to comment Share on other sites More sharing options...
Serpens Posted August 5, 2016 Share Posted August 5, 2016 (edited) if you need something to test lua code, you can use this site: http://www.lua.org/demo.html Or is there even something were we can test gamecode, without starting a game and see logfile for errors? Edited August 5, 2016 by Serpens Link to comment Share on other sites More sharing options...
Joachim Posted August 5, 2016 Share Posted August 5, 2016 (edited) You can also install Lua on your computer and run standalone scripts. Lua isn't very difficult to learn. While certain tasks are cumbersome in Lua (e.g., the fact that you can't simply copy a table without having to do extra work), most of the time you will find that you can accomplish things pretty easily. Edited August 5, 2016 by Joachim Link to comment Share on other sites More sharing options...
Andmann Posted August 5, 2016 Author Share Posted August 5, 2016 (edited) 22 minutes ago, Serpens said: if you need something to test lua code, you can use this site: http://www.lua.org/demo.html Or is there even something were we can test gamecode, without starting a game and see logfile for errors? I'll try it thanks. Well what I do to test my stuff so far is: local function myfunctin(world, parameters) ...end AddPrefabPostInit("world", function() inst:ListenFOrEvent ("myevent", myfunction) end then start up my dedicated server and use the console with: TheWorld:PushEvent("myevent", parameters). It works just fine ^_^ well I just decided to write my own slice (s, c) function that just runs through the string with recursion and uses s:sub(1,(s:find(c)-1)) >_> should do just fine Edited August 5, 2016 by Andmann Link to comment Share on other sites More sharing options...
Serpens Posted August 5, 2016 Share Posted August 5, 2016 (edited) 1 hour ago, Joachim said: You can also install Lua on your computer and run standalone scripts. Lua isn't very difficult to learn. While certain tasks are cumbersome in Lua (e.g., the fact that you can't simply copy a table without having to do extra work), most of the time you will find that you can accomplish things pretty easily. talking about copying tables, do you know a deepcopy function that works in DS ? I just tried: local function clone (t) -- deep-copy a table if type(t) ~= "table" then return t end local meta = getmetatable(t) local target = {} for k, v in pairs(t) do if type(v) == "table" then target[k] = clone(v) else target[k] = v end end setmetatable(target, meta) return target end but getmetatable and setmetatable are not known by DS =/ Edited August 5, 2016 by Serpens Link to comment Share on other sites More sharing options...
Andmann Posted August 5, 2016 Author Share Posted August 5, 2016 (edited) 1 hour ago, Serpens said: talking about copying tables, do you know a deepcopy function that works in DS ? I just tried: local function clone (t) -- deep-copy a table if mytype(t) ~= "table" then return t end local meta = getmetatable(t) local target = {} for k, v in pairs(t) do if mytype(v) == "table" then target[k] = clone(v) else target[k] = v end end setmetatable(target, meta) return target end but getmetatable and setmetatable are not known by DS =/ this should do <3 function deepcopy (t) local tOut = {} if type(t) == "table" then for k,v in pairs(t) do if type(v) == "table" then tOut[k] = deepcopy(v) else tOut[k] = v end end return tOut end end local t1 = {{"a","b","c"},{"d","e","f"}} local t2 = deepcopy(t1) print (tostring(t1), tostring(t2)) t1[1][1] = "A" for k,v in ipairs{1,2,3} do print(t1[1][v], t2[1][v]) end gives me table: 0x14d6df0 table: 0x14deb50 A a b b c c actually more importantly for k,v in ipairs{1,2} do print(t1[v], t2[v]) end results in table: 0x64aa10 table: 0x64ab50 table: 0x64aa90 table: 0x643350 Edited August 5, 2016 by Andmann ipairs -> pairs Link to comment Share on other sites More sharing options...
Serpens Posted August 5, 2016 Share Posted August 5, 2016 (edited) @Andmann: Hmm.. it does not give the same result as the clone function =/ it tested the following at lua demo: Spoiler function deepcopy (t) local tOut = {} if type(t) == "table" then for k,v in ipairs(t) do if type(v) == "table" then tOut[k] = deepcopy(v) else tOut[k] = v end end return tOut end end local function clone (t) -- deep-copy a table if type(t) ~= "table" then return t end local meta = getmetatable(t) local target = {} for k, v in pairs(t) do if type(v) == "table" then target[k] = clone(v) else target[k] = v end end setmetatable(target, meta) return target end tab1 = {} tab1["in"]={1,2,3} tab2 = clone(tab1) --tab2 = deepcopy(tab1) print("__") for k,v in pairs(tab1) do print(v) end print("__") for k,v in pairs(tab2) do print(v) end print("__") Edited August 5, 2016 by Serpens Link to comment Share on other sites More sharing options...
Andmann Posted August 5, 2016 Author Share Posted August 5, 2016 (edited) yeah apparently it returns an empty table lol... the logic should be fine though... it worked in my tests ^^ Either I made a copy paste error ( I'm sry) xD or I haven't considered all cases. - let me check, though why do you need the function if you have clone? As a matter of fact my function does exactly the same as clone lol... just that I'm not half as good a programmer so I'm missing some checks =D Edited August 5, 2016 by Andmann Link to comment Share on other sites More sharing options...
Serpens Posted August 5, 2016 Share Posted August 5, 2016 (edited) I thougt clone would not work, cause I get a global error for getmetatable. hm.. I just saw that the game scripts itself also using getmetatable, so it is known by DS. But I get a global error. With GLOBAL.getmetatable(t) I don't get an error, but I don't know if it works like it should... does it? Edited August 5, 2016 by Serpens Link to comment Share on other sites More sharing options...
Andmann Posted August 5, 2016 Author Share Posted August 5, 2016 (edited) as long as you're within modmain GLOBAL.getmetatable should work just fine. I have the same with my i/o operations so i defined a local io = Global.io my suggestion just add local getmetatable = GLOBAL.getmetatable to the top of your file that way copy pasting prebuilt functions will probably net you the least mistakes Edited August 5, 2016 by Andmann Link to comment Share on other sites More sharing options...
Serpens Posted August 5, 2016 Share Posted August 5, 2016 7 minutes ago, Andmann said: as long as you're within modmain GLOBAL.getmetatable should work just fine. I have the same i/o operations so i defined local io = Global.io my suggestion just add local getmetatable = GLOBAL.getmetatable to the top of your file that way copy pasting prebuilt functions will probably net you the least mistakes yes thanks... I did not expected that behaviour, cause math.max(), type() and other lua functions do work without GLOBAL. Link to comment Share on other sites More sharing options...
Andmann Posted August 5, 2016 Author Share Posted August 5, 2016 just if you're interested I fixed the deepcopy... xD ipairs -> pairs ipairs iterates through 1 to #table but you had the key "in" so of course it wouldnt find the table at least that's what I would gather from the results which is highly interesting because one could use that as a filter for dirty tables :3 ^_^ Link to comment Share on other sites More sharing options...
Andmann Posted August 5, 2016 Author Share Posted August 5, 2016 I also finished the string slicer So i guess we can mark this topic as Solved =D function SliceString(s, c, t) t = t or {} local subs = "" if s == nil or type(s) ~= "string" then print(s) return t else local i = s:find(c) print("found", c, "at", i) if i ~= nil then local j = i-1 subs = s:sub(1,(i-1)) print("adding", subs, "to table") s = s:sub(i+1) else subs = s s = nil end table.insert(t,(#t+1) ,subs) print(t[#t]) return SliceString(s, c, t) end end Link to comment Share on other sites More sharing options...
DarkXero Posted August 6, 2016 Share Posted August 6, 2016 Line 698 of util.lua, deepcopy. Lua functions available in the mod environment are defined in the CreateEnvironment function of mods.lua. Of course, you have global access too. Link to comment Share on other sites More sharing options...
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