Weapon Spawn rotation.

Bare with me i’m new to development and i’m trying to work on a game mode - the idea i’m trying to complete is to have weapons spawn in locations on a map for example like playing DayZ loot spawns in areas of a map however i don’t want it to be the same loot i want it to be different each time + i want the loot to re spawn after a certain time, is this possible? and does any experienced developer have any clue how we could get closer to achieving this i was looking into “ents.Create” but i just can’t shine a light on how to do it.

Simple way:



local PossibleDrops = { "weapon_crowbar", "weapon_pistol", "weapon_smg", "whatever_other_class" }
local LastDrop = 1

timer.Create( "LootDrop", 240, 0, function()

    local IdenticalTable = table.Copy( PossibleDrops ) -- Create an identical copy of the PossibleDrops table, if you use a reference you would alter the original, which we do not want.
    IdenticalTable[ LastDrop ] = nil -- Remove the previous drop index from the table, to eliminate it being a choice for our next drop.

    local NextDrop = math.random( 1, #IdenticalTable ) -- Generate a random index from 1 and whatever the length of the IdenticalTable is.

    local Weapon = ents.Create( IdenticalTable[ NextDrop ] ) -- Create an entity by obtaining whatever class belongs to the NextDrop which is a random index.

    if ( not ( IsValid( Weapon ) ) ) then

        return -- If for some reason the entity isn't valid, don't carry out the rest of the code ( To prevent errors you should always make sure things are valid )

    end

    -- Carry out whatever manipulations you wish to apply to the Weapon

    Weapon:SetPos( Vector( x, y, z ) )
    Weapon:Spawn()
    
    LastDrop = NextDrop -- Set the LastDrop variable to the NextDrop, that way when the timer calls back, this choice will be eliminated from IdenticalTable, as to not spawn the same weapon.

end )


Forgive me if the code doesn’t work, I came up with it on the fly.

Complex way:

In order to avoid having the weapons piling up because nobody picks them up, you need to come up with a way to check that there is only one entity at the time, this means that somebody else has already picked it up.
You could also spawn the weapon in a random location similar to how the random weapon selection is done.

Thank you another friend said this to me, my lack of experience in lua just makes this hard for me, can I add you on steam and maybe you can help me out.

I’ve never actually done said thing. I understand the concept and the steps you’d have to take but contacting someone who has already done it would be better.

The previous code will function except it’ll pile weapons as I said before.

Simplest way that comes to mind is to find all the entities in a sphere relative to the position the weapons spawn, if any entity’s class matches a class you want to spawn, then return to not carry out the rest of the code ( spawning the weapon, etc )

You can use ents.FindInSphere( Vector Origin, Number Radius ) which returns a table of entities which you can loop through

In pseducode because I’m not a fan of the code tags:

Timer callback function
----Carry out the operations in the previous code I posted
----Before spawning the weapon, loop through all the entities found in the returned table by FindInSphere
--------Loop through the table holding all the possible classes that can spawn
--------If any of the two classes match return early to not carry out the code below
----Do the weapon spawning here as you would in the previous post’s code

Brb: compiling all of this into code.
Edit:


local PossibleLocations = { Vector( 0, 0, 0 ), Vector( 0, 0, 0 ), Vector( 0, 0, 0 ) }
local PossibleDrops = { "weapon_crowbar", "weapon_pistol", "weapon_smg", "whatever_other_class" }
local LastDrop = 1


timer.Create( "LootDrop", 240, 0, function()


    local Position = table.Random( PossibleLocations )


    for Key, Entity in pairs( ents.FindInSphere( Position, 256 ) ) do


        for i = 1, #PossibleDrops do


            if ( Entity:GetClass() == PossibleDrops[ i ] ) then


                return


            end


        end


    end


    local IdenticalTable = table.Copy( PossibleDrops ) -- Create an identical copy of the PossibleDrops table, if you use a reference you would alter the original, which we do not want.
    IdenticalTable[ LastDrop ] = nil -- Remove the previous drop index from the table, to eliminate it being a choice for our next drop.


    local NextDrop = math.random( 1, #IdenticalTable ) -- Generate a random index from 1 and whatever the length of the IdenticalTable is.


    local Weapon = ents.Create( IdenticalTable[ NextDrop ] ) -- Create an entity by obtaining whatever class belongs to the NextDrop which is a random index.


    if ( not ( IsValid( Weapon ) ) ) then


        return -- If for some reason the entity isn't valid, don't carry out the rest of the code ( To prevent errors you should always make sure things are valid )


    end


    -- Carry out whatever manipulations you wish to apply to the Weapon


    Weapon:SetPos( Position )
    Weapon:Spawn()
    
    LastDrop = NextDrop -- Set the LastDrop variable to the NextDrop, that way when the timer calls back, this choice will be eliminated from IdenticalTable, as to not spawn the same weapon.


end )

Wow, code tags do not like tabs.

As said before, you must check it so weapons don’t pile up.
You could use ents.GetInSphere() in the spawn vector…