Properly loading plugins based on gamemode

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.."!

" )
end

	if load then
		ASA.IncludeSharedFile( "asa_plugins/"..v )
		Msg( "Plugin "..v.." loaded!

" )
end
end
end
hook.Add( “Initialize”, “ASA.LoadPlugins”, ASA.LoadPlugins )
[/lua]

Help please. :slight_smile:

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…"
" )
Msg( match…"

" )
if match == crgamemode then
load = true
else
load = false
Msg( "Didnt load “…v…”!

" )
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. :smiley:

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.

I see, but string.match is sufficient for this situation.

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 :).


> if 0 then print"uh-oh! You were wrong!" end
uh-oh! You were wrong!

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.."!

" )
end
end

	if load then
		ASA.IncludeSharedFile( "asa_plugins/"..v )
		Msg( "Plugin "..v.." loaded!

" )
end
end
end
hook.Add( “Initialize”, “ASA.LoadPlugins”, ASA.LoadPlugins )
[/lua]

Any objections? :smiley:

OH SHI- you serious?

yes

I see, I wonder what made me think that 0 evaluated to false.

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: