• Properly loading plugins based on gamemode
    20 replies, posted
So I'm really stuck here. Either I'm having a massive brain fart or I'm just overall stupid. lol I've got "kill.lua" and "sandbox_scoreboard.lua". If the gamemode is on sandbox, load sandbox_scoreboard. If the gamemode is gofish, don't load it. But I still want to load kill.lua no matter what. [lua] function ASA.LoadPlugins() for k, v in pairs( file.FindInLua( "asa_plugins/*.lua" ) ) do local load = true local crgamemode = string.lower( gmod.GetGamemode().Name ) local crfile = string.gsub( string.lower( v ), ".lua", "" ) -- instead of kill.lua, returns "kill" if not crgamemode == string.sub( crfile, 1, #crgamemode ) then -- IDK WTF TO DO HERE!!!! load = false Msg( "Didnt load "..v.."!\n\n" ) end if load then ASA.IncludeSharedFile( "asa_plugins/"..v ) Msg( "Plugin "..v.." loaded!\n\n" ) end end end hook.Add( "Initialize", "ASA.LoadPlugins", ASA.LoadPlugins ) [/lua] Help please. :)
Try this: [lua]local match = string.match( crfile, "^(.+)_" ); if string.lower(match) == string.lower(crgamemode) or not match then --load end --don't load[/lua] It's not guaranteed to work as I haven't tested.
That didn't work, but I know what you're aiming at. I'll play around with it a little.
I changed it to this: [lua]local match = string.match( crfile, "^(.+)_" ); if not match or string.lower(match) == string.lower(crgamemode) then --load end[/lua] And when I tested it, it worked fine :smile:.
I got it, Thanks a lot man! [lua] local match = string.match( crfile, "^(.+)_" ) if match then Msg( crgamemode.."\n" ) Msg( match.."\n\n\n\n\n" ) if match == crgamemode then load = true else load = false Msg( "Didnt load "..v.."!\n\n" ) end end [/lua]
Also you may want to note that if you have a file called "sandbox_cl_inti.lua" it will fail because it takes the gamemode as "sandbox_cl". To fix that you can change the pattern to this: "^(.-)_", but the problem with this is that if you have a gamemode with an underscore it it's name, it will fail also.
So I should probably use string.find() eh?
No, string.find doesn't return the matched string, just the position of the first occurrence of the string. I think that's the best you can do with the method that you are looking for.
But if string1 isn't found in the string, it returns 0 which i can use tobool() on. :D
string.match returns nil when a pattern isn't matched.
startpos, endpos, ... = string.find( str, patt ) If you have captures in your pattern, each capture is returned as an additional variable after the start position and end position.
[QUOTE=Kogitsune;19105305]startpos, endpos, ... = string.find( str, patt ) If you have captures in your pattern, each capture is returned as an additional variable after the start position and end position.[/QUOTE] I see, but string.match is sufficient for this situation.
[QUOTE=NullPoint;19105396]I see, but string.match is sufficient for this situation.[/QUOTE] Once you start using captures they're pretty much the same function, I was just saying that because you can get text from string.find :).
[QUOTE=NullPoint;19105206]nil and 0 both will evaluate to false in an if statement.[/QUOTE] [code]> if 0 then print"uh-oh! You were wrong!" end uh-oh! You were wrong![/code]
So I'm using this: [lua] function ASA.LoadPlugins() for k, v in pairs( file.FindInLua( "asa_plugins/*.lua" ) ) do local load = true local crgamemode = string.lower( gmod.GetGamemode().Name ) local crfile = string.gsub( string.lower( v ), ".lua", "" ) -- instead of kill.lua, returns "kill" local match = string.match( crfile, "^(.-)_" ) -- this will return whatever is in front of the "_" in the filename aka the gamemode if match then if util.tobool( string.find( match, crgamemode ) ) then load = true else load = false Msg( "Didnt load "..v.."!\n\n" ) end end if load then ASA.IncludeSharedFile( "asa_plugins/"..v ) Msg( "Plugin "..v.." loaded!\n\n" ) end end end hook.Add( "Initialize", "ASA.LoadPlugins", ASA.LoadPlugins ) [/lua] Any objections? :D
[QUOTE=Lexic;19106007][code]> if 0 then print"uh-oh! You were wrong!" end uh-oh! You were wrong![/code][/QUOTE] OH SHI- you serious?
[QUOTE=NullPoint;19106099]OH SHI- you serious?[/QUOTE] [url=http://codepad.org/J6dMQpNC]yes[/url]
I see, I wonder what made me think that 0 evaluated to false.
[QUOTE=NullPoint;19106246]I see, I wonder what made me think that 0 evaluated to false.[/QUOTE] Every other language probably.
It's not a fatal thing, Lua just doesn't have integer values fall back as booleans. [editline]01:40PM[/editline] Ninja'd. :ninja:
When you put: "if (b) then" It's essentially the same as: "if (b != nil) && (b != false)" Always a worthwhile thing to remember.
Sorry, you need to Log In to post a reply to this thread.