Page 1 of 1
[Cs:s] SpawnPoints
Posted: Mon Mar 20, 2023 4:52 pm
by cssbestrpg
hi, i have issue with my code, i have been trying make spawnpoints plugin that it get spawn point cordinates from ini file.
Here is the code:
Syntax: Select all
import path
from configobj import ConfigObj
from mathlib import Vector
from entities.entity import Entity
from listeners import OnLevelInit
from listeners.tick import Delay
@OnLevelInit
def map_start(map):
Delay(0, create_spawn_point, (map,))
maps_path = ConfigObj(path.Path(__file__).dirname() + '/spawn_points.ini')
def create_spawn_point(map):
for i in maps_path:
x = int(maps_path['maps'][i][map]['tx'])
y = int(maps_path['maps'][i][map]['ty'])
z = int(maps_path['maps'][i][map]['tz'])
t_spawn = Entity.create('info_player_terrorist')
t_spawn.spawn()
t_spawn.origin = Vector(x,y,z)
Here is the ini file
Syntax: Select all
[maps]
[[de_dust2]]
[[1]]
tx = "0"
ty = "0"
tz = "0"
I don't get why it keeps giving me this error:
Code: Select all
[Source.Python]
[SP] Caught an Exception:
Traceback (most recent call last):
File "..\addons\source-python\packages\source-python\listeners\tick.py", line 79, in _tick
self.pop(0).execute()
File "..\addons\source-python\packages\source-python\listeners\tick.py", line 160, in execute
return self.callback(*self.args, **self.kwargs)
File "..\addons\source-python\plugins\spawn_points\spawn_points.py", line 19, in create_spawn_point
x = int(maps_path['maps'][i][map]['tx'])
File "..\addons\source-python\packages\site-packages\configobj.py", line 554, in __getitem__
val = dict.__getitem__(self, key)
KeyError: 'maps'
Re: [Cs:s] SpawnPoints
Posted: Mon Mar 20, 2023 5:31 pm
by satoon101
First,
map is a built-in function in Python, so I would avoid using it as a variable name. The issue is that you are looping through all of the keys in
maps_path, which from your example is just "maps", and then trying to get
maps_path['maps'][i] where
[i] is "maps". So literally looking up
maps_path['maps']['maps'] which doesn't exist. This might work better for your situation:
Syntax: Select all
def create_spawn_point(current_map):
if current_map not in maps_path['maps']:
return
for values in maps_path['maps'][current_map].values():
t_spawn = Entity.create('info_player_terrorist')
t_spawn.spawn()
t_spawn.origin = Vector(int(values['tx']), int(values['ty']), int(values['tz']))
Re: [Cs:s] SpawnPoints
Posted: Tue Mar 21, 2023 11:37 am
by cssbestrpg
I have updated the code, but i got an other issue.
It keep giving me this error:
Code: Select all
[SP] Caught an Exception:
Traceback (most recent call last):
File "..\addons\source-python\packages\source-python\listeners\tick.py", line 79, in _tick
self.pop(0).execute()
File "..\addons\source-python\packages\source-python\listeners\tick.py", line 160, in execute
return self.callback(*self.args, **self.kwargs)
File "..\addons\source-python\plugins\spawn_points\spawn_points.py", line 19, in create_spawn_point
x = values['x']
TypeError: string indices must be integer
Here is the current code
Syntax: Select all
import path
from configobj import ConfigObj
from mathlib import Vector
from entities.entity import Entity
from listeners import OnLevelInit
from listeners.tick import Delay
maps_path = ConfigObj(path.Path(__file__).dirname() + '/spawn_points.ini')
@OnLevelInit
def map_start(current_map):
Delay(0, create_spawn_point, (current_map,))
def create_spawn_point(current_map):
if current_map not in maps_path['maps']:
return
for values in maps_path['maps'][current_map].values():
x = values['x']
y = values['y']
z = values['z']
t_spawn = Entity.create('info_player_terrorist')
t_spawn.spawn()
t_spawn.origin = Vector(int(x),int(y),int(z))
Re: [Cs:s] SpawnPoints
Posted: Tue Mar 21, 2023 5:56 pm
by satoon101
Your ini file above used tx not just x.
Re: [Cs:s] SpawnPoints
Posted: Tue Mar 21, 2023 6:12 pm
by L'In20Cible
cssbestrpg wrote:Syntax: Select all
[maps]
[[de_dust2]]
[[1]]
tx = "0"
ty = "0"
tz = "0"
Nested sections must match their brackets. In that file,
1 is under
maps, not under
maps[de_dust2]. That said, the
maps section itself is rather redundant.
EDIT: What about
ConfigFile instead?
Syntax: Select all
from core import ConfigFile
from engines.server import global_vars
from mathlib import Vector
from path import Path
for spawnpoint in ConfigFile(
Path(__file__).parent /
'spawnpoints' /
f'{global_vars.map_name}.txt'
):
print(Vector(*map(float, spawnpoint[~2:])))
Code: Select all
// ../spawnpoints/de_dust2.txt
0 0 0
1 2
3
4 5 6
7 8 9 1 2 4
Vector(0.0, 0.0, 0.0)
Vector(1.0, 2.0, 0.0)
Vector(3.0, 0.0, 0.0)
Vector(4.0, 5.0, 6.0)
Vector(1.0, 2.0, 4.0)
Re: [Cs:s] SpawnPoints
Posted: Tue Mar 21, 2023 6:46 pm
by cssbestrpg
L'In20Cible wrote:What about
ConfigFile instead?
Syntax: Select all
from core import ConfigFile
from engines.server import global_vars
from mathlib import Vector
from path import Path
for spawnpoint in ConfigFile(
Path(__file__).parent /
'spawnpoints' /
f'{global_vars.map_name}.txt'
):
print(Vector(*map(float, spawnpoint[~2:])))
Code: Select all
// ../spawnpoints/de_dust2.txt
0 0 0
1 2
3
4 5 6
7 8 9 1 2 4
Vector(0.0, 0.0, 0.0)
Vector(1.0, 2.0, 0.0)
Vector(3.0, 0.0, 0.0)
Vector(4.0, 5.0, 6.0)
Vector(1.0, 2.0, 4.0)
Didn't think of use ConfigFile, does it create de_dust2.txt automatically or do i need to create de_dust2.txt manually?
EDIT:
How i can use ConfigFile to separate spawn points for terrorist and counter-terrorist?
Re: [Cs:s] SpawnPoints
Posted: Tue Mar 21, 2023 7:03 pm
by L'In20Cible
cssbestrpg wrote:How i can use ConfigFile to separate spawn points for terrorist and counter-terrorist?
While a bit repetitive, you could define the classname as first argument. For example:
Syntax: Select all
from core import ConfigFile
from engines.server import global_vars
from mathlib import Vector
from path import Path
for spawnpoint in ConfigFile(
Path(__file__).parent /
'spawnpoints' /
f'{global_vars.map_name}.txt'
):
print(
spawnpoint[0], # classname
Vector(*map(float, spawnpoint[1:][~2:]))
)
Code: Select all
// ../spawnpoints/de_dust2.txt
info_player_terrorist 0 0 0
info_player_terrorist 1 2
info_player_terrorist 3
info_player_terrorist 4 5 6
info_player_terrorist
info_player_terrorist 7 8 9 1 2 4
info_player_terrorist Vector(0.0, 0.0, 0.0)
info_player_terrorist Vector(1.0, 2.0, 0.0)
info_player_terrorist Vector(3.0, 0.0, 0.0)
info_player_terrorist Vector(4.0, 5.0, 6.0)
info_player_terrorist Vector(0.0, 0.0, 0.0)
info_player_terrorist Vector(1.0, 2.0, 4.0)
Re: [Cs:s] SpawnPoints
Posted: Tue Mar 21, 2023 7:06 pm
by L'In20Cible
cssbestrpg wrote:Didn't think of use ConfigFile, does it create de_dust2.txt automatically or do i need to create de_dust2.txt manually?
It should simply return an empty list if the file is missing but, it will currently produce an error because I forgot to import
CFG_PATH. For now, you can just catch
NameError to skip missing files or update
this file.