Activate script if map prefix detected
6 replies



22.11.15 11:15:55 am
Hello there.
As the title states, how the code would be if mapname prefix is detected?
For example, if I run any map with same prefix (de_dust2, de_inferno, de_season), a script will run, determining that it will only active when these prefix are detected. Optional: print message after script successfully loaded.
It should be something like this in server.lua:
I ask this because I am reworking on my BF 2 Class System script and will create custom map (don't compliment for its cerativity
) based on it.
As the title states, how the code would be if mapname prefix is detected?
For example, if I run any map with same prefix (de_dust2, de_inferno, de_season), a script will run, determining that it will only active when these prefix are detected. Optional: print message after script successfully loaded.
It should be something like this in server.lua:
Code:
1
2
3
4
5
2
3
4
5
mapPrefix = de_
if mapPrefix == true then
dofile("sys/lua/script.lua")
end
if mapPrefix == true then
dofile("sys/lua/script.lua")
end
I ask this because I am reworking on my BF 2 Class System script and will create custom map (don't compliment for its cerativity

a little bit of this :
map
and this : string.match("string","^%a+_")

and this : string.match("string","^%a+_")
edited 2×, last 22.11.15 11:44:41 am
Alright. Will inform for problems later.
@
tontonEd: I do not understand those because I am not an advanced scripter lol. By the way thanks for helping though.
@
Talented Doge: That might work. I will try it later.
@

@

The problem with
Talented Doge's approach is that it assumes the map prefix is three characters long. This will mean that the prefix to de_dust will be "de_", ctf_cs2dfortress will be "ctf" and any custom prefixes longer than 3 characters will get cut off.
tontonEd's approach is better, but he didn't provide the full solution. You can get the map prefix like this (ignoring the _):
If you need the prefix with the _, change your code to this:


Code:
1
2
3
4
5
2
3
4
5
local prefix = game("sv_map"):match("^(%a+)_")
-- ctf_cs2dfortress = ctf
-- de_dust = de
-- dfhgjdsfhigdfg_test = dfhgjdsfhigdfg
-- ctf_cs2dfortress = ctf
-- de_dust = de
-- dfhgjdsfhigdfg_test = dfhgjdsfhigdfg
If you need the prefix with the _, change your code to this:
Code:
1
local prefix = game("sv_map"):match("^%a+_")
I code, therefore I exist. | Visit my blog for Lua tips and other interesting info
The variable
EngiN33R provided is actually the best approach to the OP. Back in 2012 this was my solution:
Random maps+maptypes -
Cure Pikachu has written:




Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
-- Function
function prefix(n)
local x = tonumber(string.len(n))
if string.sub(map("name"),1,x) == n then
return true
end
return false
end
-- Examples
if prefix("ctf_") then -- If current map is a CTF map
-- Do something
end
if prefix("zm_") then -- If current map is a ZM map
-- Do something
end
function prefix(n)
local x = tonumber(string.len(n))
if string.sub(map("name"),1,x) == n then
return true
end
return false
end
-- Examples
if prefix("ctf_") then -- If current map is a CTF map
-- Do something
end
if prefix("zm_") then -- If current map is a ZM map
-- Do something
end
edited 1×, last 22.11.15 06:25:59 pm





