• What should be an easy question.
    18 replies, posted
I am new to LUA in the sense that i have not ever bothered coding smaller stuff. So i don't know some of the API/methods people might be using to complete simple tasks like this one: I need to detect when an entity isn't in the spawn location of the entity. Take a look at the code and it will take more sense. [CODE]function RespawnItems() local ItemTable1 = {} ItemTable1[1] = "watermelon" ItemTable1[2] = "milk" local ent = ents.Create(table.Random( ItemTable1 )) local entmilk = ents.Create("milk") local entwatermelon = ents.Create("watermelon") if entmilk:GetPos() != Vector( -169.696579, 1135.181396, 135.0000 ) then if entwatermelon:GetPos() != Vector( -169.696579, 1135.181396, 135.0000 ) then ent:Spawn() ent:SetPos( Vector( -169.696579, 1135.181396, 135.0000 ) ); end end end timer.Create( "RespawnTimer", 5, 0, RespawnItems )[/CODE] I'm just looking to make the entity(the one that picks from the table) spawn if nothing in the table is currently in the spawn position. I'm using spawn positions because that is the only thing that came to mind. I tried !IsValid, wasn't getting anything done there. Side Question: When there's two ents ontop of one-another, if i push 'use' (e) it picks up both, rather than just the 'top' one. or the one i was actually looking at. Thanks in advance.
Try this: [lua] local entityxx local ItemTable1 = {'watermelon','milk'} local distance = 10 // How far away it can get before it respawns. local SpawnPos = Vector( -169.696579, 1135.181396, 135.0000 ) function Start() entityxx = ents.Create(table.Random(ItemTable1)) entityxx:SetPos(SpawnPos) entityxx:Spawn() end function RespawnItems() if((entity:GetPos():Distance(SpawnPos)) > distance) then Start() end end timer.Create( "RespawnTimer", 5, 0, RespawnItems ) [/lua] Change the distance. (I did this because gravity moves them a little) (Untested)
[QUOTE=Coffeee;45175217]Try this: [lua] local entityxx local ItemTable1 = {'watermelon','milk'} local distance = 10 // How far away it can get before it respawns. local SpawnPos = Vector( -169.696579, 1135.181396, 135.0000 ) function Start() entityxx = ents.Create(table.Random(ItemTable1)) entityxx:SetPos(SpawnPos) entityxx:Spawn() end function RespawnItems() if((entity:GetPos():Distance(SpawnPos)) > distance) then Start() end end timer.Create( "RespawnTimer", 5, 0, RespawnItems ) [/lua] Change the distance. (I did this because gravity moves them a little) (Untested)[/QUOTE] physics are turned off on them. They won't move.
That doesn't matter. It still works. (You're welcome)
[QUOTE=Coffeee;45175288]That doesn't matter. It still works. (You're welcome)[/QUOTE] Thanks you so much for trying :P It doesn't work though, i'll tinker around with it for a while.
Sorry! I forgot to call Start initially. (And put entity instead of entityxx) [lua] local entityxx local ItemTable1 = {'watermelon','milk'} local distance = 10 // How far away it can get before it respawns. local SpawnPos = Vector( -169.696579, 1135.181396, 135.0000 ) function Start() entityxx = ents.Create(table.Random(ItemTable1)) entityxx:SetPos(SpawnPos) entityxx:Spawn() end function RespawnItems() if((entityxx:GetPos():Distance(SpawnPos)) > distance) then Start() end end Start() timer.Create( "RespawnTimer", 5, 0, RespawnItems ) [/lua] It should work now.
Nope, Not respawning on map anymore. :c Any other ideas?
Just tested it and it works fine. As long as watermelon and milk are valid entities it should work. And even if they're not you should still get errors.
[QUOTE=Coffeee;45175569]Just tested it and it works fine. As long as watermelon and milk are valid entities it should work. And even if they're not you should still get errors.[/QUOTE] init.lua, yes? ._. Fixed up some stuff, This is what i've got. [CODE]function RespawnItems() local distance = 10 local ItemTable1 = {} ItemTable1[1] = "watermelon" ItemTable1[2] = "milk" local SpawnPos1 = Vector( -169.696579, 1135.181396, 135.0000 ) local ent = ents.Create(table.Random( ItemTable1 )) local entmilk = ents.Create("milk") local entwatermelon = ents.Create("watermelon") if ((entmilk:GetPos():Distance(SpawnPos1)) > distance) then if ((entwatermelon:GetPos():Distance(SpawnPos1)) > distance) then ent:Spawn() ent:SetPos( Vector( -169.696579, 1135.181396, 135.0000 ) ); end end end timer.Create( "RespawnTimer", 5, 0, RespawnItems )[/CODE] It's spawning those two entities at random every 5 seconds. Even if there's already an entity there. I want it to spawn one, then stop when there's either milk OR watermelon there. Thanks so far.
[EDIT]: Make sure the code isn't in the entities init.lua. It can be any serverside file as long as it isn't in the entities init.lua ---- Any serverside file. Here's what I did: I made a new file called props.lua in /lua/autorun/server and put this inside of it [lua] local entityxx local ItemTable1 = {'sent_deployableballoons','sent_deployableballoons'} // Because I couldn't think of another entity. local distance = 10 // How far away it can get before it respawns. local SpawnPos = Vector(-320.000000,-192.000000,-12223.968750) function Start() entityxx = ents.Create(table.Random(ItemTable1)) entityxx:SetPos(SpawnPos) entityxx:Spawn() end function RespawnItems() if((entityxx:GetPos():Distance(SpawnPos)) > distance) then Start() end end Start() timer.Create( "RespawnTimer", 5, 0, RespawnItems ) [/lua] then started a new flatgrass game and it worked fine. I'm sure if you or anybody else did the same it would work. (Assuming you have wire installed for the entities)
[QUOTE=Coffeee;45175633][EDIT]: Make sure the code isn't in the entities init.lua. It can be any serverside file as long as it isn't in the entities init.lua ---- Any serverside file. Here's what I did: I made a new file called props.lua in /lua/autorun/server and put this inside of it [lua] local entityxx local ItemTable1 = {'sent_deployableballoons','sent_deployableballoons'} // Because I couldn't think of another entity. local distance = 10 // How far away it can get before it respawns. local SpawnPos = Vector(-320.000000,-192.000000,-12223.968750) function Start() entityxx = ents.Create(table.Random(ItemTable1)) entityxx:SetPos(SpawnPos) entityxx:Spawn() end function RespawnItems() if((entityxx:GetPos():Distance(SpawnPos)) > distance) then Start() end end Start() timer.Create( "RespawnTimer", 5, 0, RespawnItems ) [/lua] then started a new flatgrass game and it worked fine. I'm sure if you or anybody else did the same it would work. (Assuming you have wire installed for the entities)[/QUOTE] I actually copy and pasted into a lua file. spawnmedamnit.lua put it in autorun/server restarted server nothing. If it works for you, that's good. but it doesn't for me (which doesn't make sense in code at all.) I must have code that interferes or something. I'll attempt to do it on my own. but i have a final question. What condition can i use other than distance? Distance isn't working for me either. They just keep spawning over and over with this code. [Lua]function RespawnItems() local distance = 10 local ItemTable1 = {} ItemTable1[1] = "watermelon" ItemTable1[2] = "milk" local SpawnPos1 = Vector( -169.696579, 1135.181396, 135.0000 ) local ent = ents.Create(table.Random( ItemTable1 )) local entmilk = ents.Create("milk") local entwatermelon = ents.Create("watermelon") if ((entmilk:GetPos():Distance(SpawnPos1)) > distance) then if ((entwatermelon:GetPos():Distance(SpawnPos1)) > distance) then ent:Spawn() ent:SetPos( Vector( -169.696579, 1135.181396, 135.0000 ) ); end end end timer.Create( "RespawnTimer", 5, 0, RespawnItems ) [/Lua]
It's either because don't have the 'sent_deployableballoons' entity or you're not on flatgrass. The code definitely works. There aren't differences between how our code is run, and it's unlikely that something is interfering with the script.
[QUOTE=Coffeee;45176362]It's either because don't have the 'sent_deployableballoons' entity or you're not on flatgrass. The code definitely works. There aren't differences between how our code is run, and it's unlikely that something is interfering with the script.[/QUOTE] Changed the ents and the vector. It's not respawning the ents, and sitting here looking at the code, idk why. :c It seems to check out.
When i 'use' (push E) on the ent, it gets destroyed way before it moves the required distance to call Start() again. Could this be the problem? Or maybe, It's not actually being removed, just un-drawn. Is that possible? The server still detects it there, but it's not visible on the client side. [CODE]function ENT:Use( ply ) -- thing that touches the ent. player. if (ply:IsPlayer() ) then self:Remove() -- remove the ent end end[/CODE]
Wait what? You didn't say that they get removed. Why would you need to check if it's moved if that's the case? I thought you had some sort of grav gun pickup thing. Just check if it was removed! [lua] local entityxx local ItemTable1 = {'sent_deployableballoons','sent_deployableballoons'} local SpawnPos = Vector(-320.000000,-192.000000,-12223.968750) function Start() entityxx = ents.Create(table.Random(ItemTable1)) entityxx:SetPos(SpawnPos) entityxx:Spawn() end function RespawnItems() if not IsValid(entityxx) then Start() end end Start() timer.Create( "RespawnTimer", 5, 0, RespawnItems ) [/lua] Change to your values again.
[QUOTE=Coffeee;45177746]Wait what? You didn't say that they get removed. Why would you need to check if it's moved if that's the case? I thought you had some sort of grav gun pickup thing. Just check if it was removed! [lua] local entityxx local ItemTable1 = {'sent_deployableballoons','sent_deployableballoons'} local SpawnPos = Vector(-320.000000,-192.000000,-12223.968750) function Start() entityxx = ents.Create(table.Random(ItemTable1)) entityxx:SetPos(SpawnPos) entityxx:Spawn() end function RespawnItems() if not IsValid(entityxx) then Start() end end Start() timer.Create( "RespawnTimer", 5, 0, RespawnItems ) [/lua] Change to your values again.[/QUOTE] Wow. I'm so sorry i wasted all of your time xD. I'm an idiot. Thank you so much, it's working perfectly now. 10/10 would bang. [editline]21st June 2014[/editline] I attempted to PM you for the sake of letting this thread die, but apparently they're turned off? Anywho, the respawn code is perfect. For this ONE spawn. I figure, i can copy and paste all of that code 100+ times and rename them spawn1, spawn2, and so on. OR. I could attempt to create a table of positions and have the entities respawn on the positions where nothing currently is. Here's what i've done: [Lua] local entityxx -- local SpawnPos = table.Random(table.Random(SpawnTable)) function Start() entityxx = ents.Create(table.Random(ItemTable1)) entityxx:SetPos(table.Random(SpawnTable)) entityxx:Spawn() end function RespawnItems() if not IsValid(entityxx) then Start() end end Start() timer.Create( "RespawnTimer", 5, 0, RespawnItems ) [/Lua] (I have the table at the top of the init.lua for organization reasons.) Now to me, that seemed like it was going to work in the way i wanted it to. Wrong. When i pick up and entity from vector x1 y1 z1, it can respawn at vector x2 y2 z2. Hopefully the way i said that made sense. The over-all thing i'm trying to get to is: There's a lot of entities around the map. If a player uses one, it sets a timer to respawn independent from all other respawns. If there IS a quick way to acomplish this, please let me know how, and if there's not, i'll tough it out tonight for a few hours. Thanks! maybe this can be used? [url]http://facepunch.com/showthread.php?t=1403382[/url]
It's a little hacky, but this should work [lua] local ItemTable1 = {'sent_deployableballoons','sent_anotherentity'} // Change local SpawnPositions = { Vector(745.819458,-592.645508,-12223.968750), // Change or add more Vector(680.750305, -482.730988, -12223.968750), Vector(857.145203, -421.092865, -12223.968750) } local EntityIDs = {} function Setup() for k,v in pairs(SpawnPositions) do local tempent = ents.Create(table.Random(ItemTable1)) tempent:SetPos(v) tempent:Spawn() table.insert(EntityIDs, {tempent:EntIndex(), v}) end end function RespawnItems() for k,v in pairs(EntityIDs) do if (not IsValid(Entity(v[1]))) then local newent = ents.Create(table.Random(ItemTable1)) local pos = v[2] newent:SetPos(pos) newent:Spawn() table.insert(EntityIDs, {newent:EntIndex(), pos}) table.remove(EntityIDs, k) end end end // Put the Setup() function in the right hook if you want to be a Lua pro. I can't be bothered to Google it, so a timer will do for now. timer.Simple(1,function() Setup() end) timer.Create( "RespawnTimer", 5, 0, RespawnItems ) [/lua] Change anything that says change on it. You don't need to create threads for small questions like [URL="http://facepunch.com/showthread.php?t=1403382"]this[/URL], use the megathread [URL="http://facepunch.com/showthread.php?t=1348923"]here[/URL]
[QUOTE=Coffeee;45178559]It's a little hacky, but this should work [lua] local ItemTable1 = {'sent_deployableballoons','sent_anotherentity'} // Change local SpawnPositions = { Vector(745.819458,-592.645508,-12223.968750), // Change or add more Vector(680.750305, -482.730988, -12223.968750), Vector(857.145203, -421.092865, -12223.968750) } local EntityIDs = {} function Setup() for k,v in pairs(SpawnPositions) do local tempent = ents.Create(table.Random(ItemTable1)) tempent:SetPos(v) tempent:Spawn() table.insert(EntityIDs, {tempent:EntIndex(), v}) end end function RespawnItems() for k,v in pairs(EntityIDs) do if (not IsValid(Entity(v[1]))) then local newent = ents.Create(table.Random(ItemTable1)) local pos = v[2] newent:SetPos(pos) newent:Spawn() table.insert(EntityIDs, {newent:EntIndex(), pos}) end end end // Put the Setup() function in the right hook if you want to be a Lua pro. I can't be bothered to Google it, so a timer will do for now. timer.Simple(1,function() Setup() end) timer.Create( "RespawnTimer", 5, 0, RespawnItems ) [/lua] Change anything that says change on it. You don't need to create threads for small questions like [URL="http://facepunch.com/showthread.php?t=1403382"]this[/URL], use the megathread [URL="http://facepunch.com/showthread.php?t=1348923"]here[/URL][/QUOTE] Oh i didn't know about that. :P Ty. Those ents are being spawned two at a time. I lied. I had [CODE]/* function GM:InitPostEntity( ) local ent = ents.Create(table.Random( ItemTable )) ent:Spawn() ent:SetPos( Vector( -169.696579, 1135.181396, 135.0000 ) ); -- in broken building local ent2 = ents.Create(table.Random( ItemTable )) ent2:Spawn() ent2:SetPos( Vector( -2000.762817, 1181.251221, 168.000000 ) ); -- hidden room local ent3 = ents.Create(table.Random( ItemTable )) ent3:Spawn() ent3:SetPos( Vector(-1551.812134, 1083.409668, 263.282471) ); -- above hidden room local ent4 = ents.Create(table.Random( ItemTable )) ent4:Spawn() ent4:SetPos( Vector(-986.553955, 1528.284424, 168.727219) ); -- middle on table local ent5 = ents.Create(table.Random( ItemTable )) ent5:Spawn() ent5:SetPos( Vector(-36.200439, 1059.177246, 265.023651) ); -- above 'orange door' end */[/CODE] Which was spawning an entity along with the other code. All i needed. Thank you!
Edited. Should have fixed it. Stop editing your posts like that -_-
Sorry, you need to Log In to post a reply to this thread.