• 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
Re: Spawn Entity at multiple locations?
« Reply #15 on: September 13, 2016, 03:40:30 pm »
No errors, but no entity. I think I did it right.

init.lua:
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.  
  20.             phys:Wake()
  21.            
  22.     end
  23.        
  24. end
  25.  
  26. function ENT:Use( caller, activator )
  27.  
  28.     for k, v in pairs( caller:GetWeapons() ) do
  29.  
  30.        if v == "weapon_popcorn" then
  31.  
  32.             caller:PrintMessage( HUD_PRINTTALK, "You already have popcorn!" )
  33.  
  34.           else
  35.  
  36.             caller:Give( "weapon_popcorn" )
  37.  
  38.         end
  39.  
  40.  
  41.     end
  42.  
  43. end
  44.  


cl_init.lua:
Code: Lua
  1. include( "shared.lua" )
  2.  
  3.  
  4. function ENT:Draw()
  5.  
  6.  
  7.         self:DrawModel()
  8.  
  9.  
  10. end


shared.lua:
Code: Lua
  1. ENT.Author = "iViscosity"
  2. ENT.Information = "Gives players popcorn on cinema."
  3. ENT.Category = "Cinema"
  4.  
  5.  
  6. ENT.Spawnable = false
  7. ENT.AdminOnly = false
  8. ENT.Type = "anim"
  9. ENT.Base = "base_anim"
  10.  
  11.  
  12. ENT.PrintName = "Popcorn"
  13.  
  14.  
  15. ENT.Model = "models/teh_maestro/popcorn.mdl"


Actual spawn (/garrysmod/lua/entities is where I have it, should I move it?)
Code: Lua
  1. ENT.Author = "iViscosity"
  2. ENT.Information = "Gives players popcorn on cinema."
  3. ENT.Category = "Cinema"
  4.  
  5.  
  6. ENT.Spawnable = false
  7. ENT.AdminOnly = false
  8. ENT.Type = "anim"
  9. ENT.Base = "base_anim"
  10.  
  11.  
  12. ENT.PrintName = "Popcorn"
  13.  
  14.  
  15. ENT.Model = "models/teh_maestro/popcorn.mdl"
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 #16 on: September 13, 2016, 03:50:03 pm »
You did it completely wrong...

Review the sent_ball.lua like I said.

You don't need a shared.lua, init.lua or cl_init.lua.


You need just 1 file that has your entity code in it. Then the code to SPAWN the entity... the code that has ents.Create, needs to not be in the Entity code.. it needs to be somewhere else. Somewhere that autoruns serverside.

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Spawn Entity at multiple locations?
« Reply #17 on: September 13, 2016, 06:12:37 pm »
In the loop, there is an ents.Create.

Whoops, must've missed that :P
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 #18 on: September 14, 2016, 01:42:48 pm »
I wasn't actually planning to make it with a single file... I was going to try it with a different file... but oh well


popcorn_spawner.lua:
 
Code: Lua
  1. ENT.Author = "iViscosity"
  2. ENT.Information = "Gives players popcorn on cinema."
  3. ENT.Category = "Cinema"
  4.  
  5.  
  6. ENT.Type = "anim"
  7. ENT.Base = "base_anim"
  8. ENT.PrintName = "Popcorn Spawner"
  9.  
  10.  
  11. function ENT:Initialize()
  12.  
  13.  
  14.     if ( CLIENT ) then return end
  15.  
  16.     self:SetModel( "models/teh_maestro/popcorn.mdl" )
  17.     self:PhysicsInit( SOLID_VPHYSICS )
  18.     self:SetMoveType( MOVETYPE_NONE )
  19.     self:SetSolid( SOLID_VPHYSICS )
  20.     self:SetUseType( SIMPLE_USE )      
  21.     self:DrawShadow( true )
  22.     self:Spawn()
  23.  
  24.     local phys = self:GetPhysicsObject()
  25.        
  26.     if IsValid( phys ) then
  27.  
  28.        phys:Wake()
  29.            
  30.     end
  31.        
  32. end
  33.  
  34. function ENT:Use( activator, caller )
  35.  
  36.     for k, v in pairs( activator:GetWeapons() ) do
  37.  
  38.        if v == "weapon_popcorn" then
  39.  
  40.             activator:PrintMessage( HUD_PRINTTALK, "You already have popcorn!" )
  41.  
  42.           else
  43.  
  44.             activator:Give( "weapon_popcorn" )
  45.  
  46.         end
  47.  
  48.  
  49.     end
  50.  
  51. end
  52.  
  53.  
  54. if ( SERVER ) then return end
  55.  
  56.  
  57. function ENT:Draw()
  58.  
  59.  
  60.     self:DrawModel()
  61.  
  62.  
  63. end
would it be like this? That's what I got from looking at sent_ball.lua


EDIT: Using that code, and this in garrysmod/lua/autorun/server
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( "popcorn_spawner" )
  14.             popcorn:SetPos( v )
  15.             popcorn:Spawn()
  16.  
  17.         end
  18.  
  19. end )
  20.  


and nothing happens. No errors, no spawn.
« Last Edit: September 14, 2016, 02:02:16 pm 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 #19 on: September 14, 2016, 04:29:06 pm »
Have you tried any debugging yet? Learn to properly debug, it's the most powerful thing you can do as a developer (in any language).

Is your spawn hook running? Is it creating the entity? Is the Initialize being called?
All of these things can be learned with just a couple lines of code. print is your friend. :)

  • Print