• Print

Author Topic: Spawn Entity at multiple locations?  (Read 9283 times)

0 Members and 1 Guest are viewing this topic.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Spawn Entity at multiple locations?
« on: September 12, 2016, 06:29:10 pm »
I'm trying to make a spawn for the Popcorn weapon from Cinema, and I need it to spawn at 3 different locations. Right now, I have this:
Code: Lua
  1. AddCSLuaFile( "cl_init.lua" )
  2. AddCSLuaFile( "shared.lua" )
  3.  
  4.  
  5. include( "shared.lua" )
  6.  
  7.  
  8. local positions = {}
  9. positions[1] = Vector( 223.00898742676, 999.78521728516, 41.03125 )
  10. positions[2] = Vector( 311.53573608398, 999.78521728516, 41.03125 )
  11. positions[3] = Vector( 474.81579589844, 999.78521728516, 41.03125 )
  12.  
  13.  
  14. function ENT:Initialize()
  15.  
  16.  
  17.         for k,v in pairs( #positions ) do
  18.        
  19.             self:SetPos( v )
  20.             self:SetModel( self.Model )
  21.             self:PhysicsInit( SOLID_VPHYSICS )
  22.             self:SetMoveType( MOVETYPE_NONE )
  23.             self:SetSolid( SOLID_VPHYSICS )
  24.             self:SetUseType( SIMPLE_USE )      
  25.             self:DrawShadow( true )
  26.  
  27.  
  28.         end
  29.  
  30.  
  31.         local phys = self:GetPhysicsObject()
  32.  
  33.  
  34.         if IsValid( phys ) then
  35.                
  36.                 phys:Wake()
  37.  
  38.  
  39.         end
  40.  
  41.  
  42. end
  43.  
  44.  
  45. function ENT:Use( caller, activator )
  46.  
  47.  
  48.         if caller:IsPlayer() then
  49.  
  50.  
  51.                 local check = table.GetKeys( caller:GetWeapons() )
  52.  
  53.  
  54.                 if check == "weapon_popcorn" then
  55.                        
  56.                         caller:PrintMessage( HUD_PRINTTALK, "You already have popcorn!" )
  57.  
  58.  
  59.                 else
  60.  
  61.  
  62.                         caller:Give( "weapon_popcorn" )
  63.  
  64.  
  65.                 end    
  66.  
  67.  
  68.         end
  69.  
  70.  
  71. end


And then I restarted the server, and when I joined back in, I spawned with the popcorn weapon, but it wasn't at the locations I tried to make it. I'm not amazingly experienced with entities, so how could I do this? Also, on the Use() function, is that how I would use table.GetKeys?
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: Spawn Entity at multiple locations?
« Reply #1 on: September 12, 2016, 08:17:06 pm »
The issue isn't with your entities here, it's with your for loop.

#positions returns a count of the table, not the table. You're trying to iterate through the table.. Use:

for k,v in pairs( positions ) do

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: Spawn Entity at multiple locations?
« Reply #2 on: September 12, 2016, 08:18:40 pm »
Also, instead of

Code: [Select]
local positions = {}
positions[1] = Vector( 223.00898742676, 999.78521728516, 41.03125 )
positions[2] = Vector( 311.53573608398, 999.78521728516, 41.03125 )
positions[3] = Vector( 474.81579589844, 999.78521728516, 41.03125 )

Why not just

Code: [Select]
local positions = {
    Vector( 223.00898742676, 999.78521728516, 41.03125 ),
    Vector( 311.53573608398, 999.78521728516, 41.03125 ),
    Vector( 474.81579589844, 999.78521728516, 41.03125 ),
}

It's the same thing, but makes adding new values much easier since you're not having to manually assign an incremental key for each new entry.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: Spawn Entity at multiple locations?
« Reply #3 on: September 12, 2016, 08:20:05 pm »
Oh.. I actually looked at your code.. you're doing it all wrong too.. you're spawning new entities every time a new one is created. This would recursively create an infinite number of these on your map... Give me a moment and I'll clean it up for you.. :)

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: Spawn Entity at multiple locations?
« Reply #4 on: September 12, 2016, 08:26:18 pm »
This is your entity code:
Code: Lua
  1. AddCSLuaFile( "cl_init.lua" )
  2. AddCSLuaFile( "shared.lua" )
  3.  
  4. include( "shared.lua" )
  5.  
  6.  
  7. function ENT:Initialize()
  8.  
  9.         self:SetModel( self.Model )
  10.         self:PhysicsInit( SOLID_VPHYSICS )
  11.         self:SetMoveType( MOVETYPE_NONE )
  12.         self:SetSolid( SOLID_VPHYSICS )
  13.         self:SetUseType( SIMPLE_USE )      
  14.         self:DrawShadow( true )
  15.  
  16.         local phys = self:GetPhysicsObject()
  17.        
  18.         if IsValid( phys ) then
  19.                 phys:Wake()
  20.         end
  21.        
  22. end
  23.  
  24.  
  25. function ENT:Use( caller, activator )
  26.  
  27.         if caller:IsPlayer() then
  28.                 local check = table.GetKeys( caller:GetWeapons() )
  29.                 if check == "weapon_popcorn" then
  30.                         caller:PrintMessage( HUD_PRINTTALK, "You already have popcorn!" )
  31.                 else
  32.                         caller:Give( "weapon_popcorn" )
  33.                 end
  34.         end
  35.        
  36. end
  37.  

Somewhere else, you need to have a function that spawns them on the map to your locations. Maybe in a PostGamemodeLoaded Hook? http://wiki.garrysmod.com/page/GM/PostGamemodeLoaded
Put this code in some autoloaded server file. You will need to change the "popcornentityname" in the code below to your actual entity name. I don't know what you called it. Whatever the folder is called since you appear to be using the old 3 file in a folder system over the newer 1 file system. Assuming everything else in your entity is coded properly, this will work.

Code: Lua
  1. local popcorn_positions = {
  2.     Vector( 223.00898742676, 999.78521728516, 41.03125 ),
  3.     Vector( 311.53573608398, 999.78521728516, 41.03125 ),
  4.     Vector( 474.81579589844, 999.78521728516, 41.03125 ),
  5. }
  6.  
  7. hook.add( "PostGamemodeLoaded", "Spawn_Popcorn", function()
  8.         for k, v in pairs( popcorn_positions ) do
  9.                 local popcorn = ents.Create("popcornentityname")
  10.                 popcorn:SetPos( v )
  11.                 popcorn:Spawn()
  12.         end
  13. end )
  14.  
« Last Edit: September 12, 2016, 08:27:59 pm by MrPresident »

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Spawn Entity at multiple locations?
« Reply #5 on: September 13, 2016, 04:18:22 am »
I didn't know there was a "new" system lol... I'll try that out tonight, and maybe next time I make something it will be with whatever the one file is...

Also, would the entity always be there after it spawns, even if someone were to use it? Or will I have to make something that will re-spawn it?

Edit: Was how I used table.GetKeys correct? Never used that function before.
« Last Edit: September 13, 2016, 04:20:23 am by iViscosity »
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: Spawn Entity at multiple locations?
« Reply #6 on: September 13, 2016, 12:12:52 pm »
You do not have it set up to remove the entity when someone presses Use on it. You would need to add self:Remove() to your :Use() activator, and then yes, you'd need to write a timer function to respawn it.

No, it's not correct. You'd have to iterate through that as well as it's a table.

Also, the GetWeapons table has the weapon name as the value, not the key.

you could do:

Code: Lua
  1. for k, v in pairs( caller:GetWeapons() ) do
  2.         if v == "weapon_popcorn" then
  3.                 caller:PrintMessage( HUD_PRINTTALK, "You already have popcorn!" )
  4.         else
  5.                 caller:Give( "weapon_popcorn" )
  6.         end
  7. end
  8.  
« Last Edit: September 13, 2016, 12:14:39 pm by MrPresident »

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Spawn Entity at multiple locations?
« Reply #7 on: September 13, 2016, 12:14:09 pm »
Oh, that's much simpler. I'm home now so I'll test it and get back to you.
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Spawn Entity at multiple locations?
« Reply #8 on: September 13, 2016, 12:35:13 pm »
There is another entity I want to spawn in, how can I do it in one file like you mentioned? Just make a shared and specify if SERVER and if CLIENT?


EDIT:
Code: Lua
  1. local popcorn_positions = {
  2.     Vector( 223.00898742676, 999.78521728516, 41.03125 ),
  3.     Vector( 311.53573608398, 999.78521728516, 41.03125 ),
  4.     Vector( 474.81579589844, 999.78521728516, 41.03125 ),
  5. }
  6.  
  7. hook.Add( "PostGamemodeLoaded", "Spawn_Popcorn", function()
  8.  
  9.  
  10.         for k, v in pairs( popcorn_positions ) do
  11.  
  12.  
  13.                 local popcorn = ents.Create( "models/Teh_Maestro/popcorn.mdl" )
  14.                 popcorn:SetPos( v )
  15.                 popcorn:Spawn()
  16.  
  17.  
  18.         end
  19.  
  20.  
  21. end )
  22.  
used that you posted, but I get this:
Code: [Select]
[ERROR] addons/popcorn/lua/autorun/popcorn_spawn.lua:11: attempt to call field 'Create' (a nil value)
  1. fn - addons/popcorn/lua/autorun/popcorn_spawn.lua:11
   2. unknown - addons/ulib/lua/ulib/shared/hook.lua:109
That's where the entity is located, though, so what does this mean?
« Last Edit: September 13, 2016, 12:39:24 pm by iViscosity »
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Spawn Entity at multiple locations?
« Reply #9 on: September 13, 2016, 02:23:11 pm »
Could you show us your full code? You don't call 'Create' anywhere in the code snippet you posted.
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Spawn Entity at multiple locations?
« Reply #10 on: September 13, 2016, 02:33:11 pm »
In the loop, there is an ents.Create.
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: Spawn Entity at multiple locations?
« Reply #11 on: September 13, 2016, 02:43:45 pm »
Entities need to be in the Entity folder.

lua/entities

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Spawn Entity at multiple locations?
« Reply #12 on: September 13, 2016, 02:52:25 pm »
/garrysmod/addons/popcorn/lua/entities/popcorn_spawn/popcorn_spawn.lua like something like that? Or lua/entities/popcorn_spawn.lua?
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: Spawn Entity at multiple locations?
« Reply #13 on: September 13, 2016, 02:58:02 pm »
If you're going to do the 1 file system. ( The new way ) then do:

/garrysmod/addons/popcorn/lua/entities/entity_name.lua

entity_name would be your entity name that you are going to call with ents.Create

Inside that file you would need an AddCSLuaFile() at the top, and then section your code into server and client with if SERVER then and if CLIENT then.

Take a look at /garrysmod/lua/entities/sent_ball.lua as an example.


Your spawn function, would need to go into a separate file that is autoran.. so like..

/garrysmod/addons/popcorn/lua/autorun/server/popcorn_spawn.lua

Let me know if you need further assistance.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Spawn Entity at multiple locations?
« Reply #14 on: September 13, 2016, 03:06:39 pm »
Ohhh so the ents.Create calls a lua file, not the model? Now that I think about it... that makes complete sense. I'm stupid.
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

  • Print