"auto-spawning" ammo on the map with lua

I’m looking to code a gamemode that will automatically spawn ammo where there is space near a player and come in like, 2 ammo boxes, but im not very sure what functions to use, does anyone know what function would look for empty space and which one would get the position so i can spawn the entity?

bump, still having trouble finding them

There are none.
Unless you mean ents.Create.

yeah but i know ents.Create i need the game to pick a random vector/position and see if its valid (which i dont know how) and try to create an ammo box there

Make a table with valid vectors, then choose a random one out of them and use ents.Create to make it spawn.

To get a valid vector I would say no clip around the map and find a position you want, then type getpos into the console to get a vector then put it into the table.


local validSpawns = {}

validSpawns [1] = Vector (100,100,100)


To make them spawn at intervals use timer.Create

okay, I will use this first, thank you very much

Yeah… You could write an algorithm to generate a node graph of the map (which sounds like it would be the ideal solution for you) but it sounds like this is probably a little bit above your current level in terms of coding. The basic idea would be to arrange the map into a grid and basically perform traces through each cell of the grid to determine if it contains a solid surface (or is completely solid) then create a 2D quad out of the surface, most likely a parametric equation defined on the range x,y = (0,0) to (1,1) returning x,y, and z coordinates from your 2d coordinates.

You could then select a random cube which contains a quad, and solve for a random x and y coordinate on the surface quad found, and spawn loot on the given surface.

I don’t actually expect you to figure this out or do this, but this is about the closest you can get to doing exactly what you said (that I know of) though a simple sounding question it actually takes a surprisingly hard amount of mathematics.

On another note… If your map has NPC nodes you could potentially use those as spawn points. They are usually placed in rooms doorways etc. You could code a simple trace system to make sure they are indoors if you wanted.

Pst upon revising this you would actually need a table of quads… But I won’t bother getting into that detail.