Forum

> > CS2D > Scripts > Activate script if map prefix detected
Forums overviewCS2D overview Scripts overviewLog in to reply

English Activate script if map prefix detected

6 replies
To the start Previous 1 Next To the start

old Activate script if map prefix detected

Ace Howl
User Off Offline

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

old Re: Activate script if map prefix detected

EngiN33R
Moderator Off Offline

Quote
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 _):

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:

1
local prefix = game("sv_map"):match("^%a+_")

old Re: Activate script if map prefix detected

Cure Pikachu
User Off Offline

Quote
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
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
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview