Forum

> > CS2D > Scripts > Activate script if map prefix detected
ForenübersichtCS2D-Übersicht Scripts-ÜbersichtEinloggen, um zu antworten

Englisch Activate script if map prefix detected

6 Antworten
Zum Anfang Vorherige 1 Nächste Zum Anfang

alt Activate script if map prefix detected

Ace Howl
User Off Offline

Zitieren
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.

alt Re: Activate script if map prefix detected

EngiN33R
Moderator Off Offline

Zitieren
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+_")

alt Re: Activate script if map prefix detected

Cure Pikachu
User Off Offline

Zitieren
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 hat geschrieben
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
1× editiert, zuletzt 22.11.15 18:25:59
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antworten Scripts-ÜbersichtCS2D-ÜbersichtForenübersicht