• Finding text inside a string then removing the found text.
    1 replies, posted
Hey, I'm currently making a "Change level" mechanic for my admin system, but I've appeared to have stumbled over a problem. The current method I am using the fetch the maps also displays the ".bsp" after the name of the map, as well all know source does not accept this. So, I was wondering how I would go around removing the ".bsp" part of the string, so it can be loaded by source. Heres my code: [LUA]local Changemap = vgui.Create( "DButton" ) Changemap:SetParent( MainPanel ) -- Set parent to our "DermaPanel" Changemap:SetText( "Change map" ) Changemap:SetPos( 5, 355 ) Changemap:SetSize( 150, 35 ) Changemap.DoClick = function () local ChangemapButtonOptions = DermaMenu() -- Creates the menu local list = file.Find("maps/*.bsp", "GAME") for _, f in pairs(list) do ChangemapButtonOptions:AddOption(f, function() RunConsoleCommand("changelevel", f) end ) end ChangemapButtonOptions:Open() -- Open the menu AFTER adding your options end[/LUA] Thank you!
[code] for k, v in pairs(file.Find("maps/*.bsp", "GAME")) do v = string.gsub(v,".bsp","") MENU:AddOption(v, function() RunConsoleCommand("changelevel",v) end) end[/code] From a custom script of mine with menus. What you want is this: [CODE]string.gsub(v,".bsp","")[/CODE] string.gsub(String fullString, String toFind, String toReplace, Int numToReplace) [url]http://lua-users.org/wiki/StringLibraryTutorial[/url]
Sorry, you need to Log In to post a reply to this thread.