English Activate script if map prefix detected

6 replies
Goto Page
To the start Previous 1 Next To the start
Up
Ace Howl
User
Offline Off
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:
Code:
1
2
3
4
5
mapPrefix = de_

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 ) based on it.
22.11.15 11:26:37 am
Up
tontonEd
User
Offline Off
a little bit of this : cs2d lua cmd map
and this : string.match("string","^%a+_")
edited 2×, last 22.11.15 11:44:41 am
22.11.15 11:48:37 am
Up
Talented Doge
User
Offline Off
Code:
1
local map_prefix = game("sv_map"):sub(1,3)
Talented Doge
22.11.15 01:33:35 pm
Up
Ace Howl
User
Offline Off
Alright. Will inform for problems later.

@user tontonEd: I do not understand those because I am not an advanced scripter lol. By the way thanks for helping though.

@user Talented Doge: That might work. I will try it later.
22.11.15 02:31:11 pm
Up
Talented Doge
User
Offline Off
That must work. Believe in me.
Talented Doge
22.11.15 02:43:10 pm
Up
EngiN33R
Moderator
Offline Off
The problem with user 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.

user tontonEd's approach is better, but he didn't provide the full solution. You can get the map prefix like this (ignoring the _):

Code:
1
2
3
4
5
local prefix = game("sv_map"):match("^(%a+)_")

-- 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
22.11.15 03:29:07 pm
Up
Cure Pikachu
User
Offline Off
The variable user EngiN33R provided is actually the best approach to the OP. Back in 2012 this was my solution:
thread cs2d Random maps+maptypes - user Cure Pikachu has written:
Code:
1
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
edited 1×, last 22.11.15 06:25:59 pm
IMG:https://i.imgur.com/uUrye71.png
IMG:https://i.imgur.com/xpsyQRX.png
To the start Previous 1 Next To the start