• Random Ent Spawns from table with Vector Values
    11 replies, posted
I am trying to spawn an entity with random map spawns from a table... Here is the code I have: [CODE] local goldpropSpawns = { Vector( -2825.641357, 1420.374146, -3504.891846); Vector(-3366.608398, 1450.263184, -3523.183350); Vector(-5391.474121, 693.360962, -3518.278564); Vector(-4493.145996, 439.987610, -3521.404053); Vector( -3952.261963, 582.641602, -3519.968750); Vector( -3802.943604, 1792.913818, -3507.057617); Vector( -2268.290039, 1683.261597, 69.526070); Vector( -1944.526611, 4118.445801, 83.076447); Vector( -201.243225, 3668.999023, 87.421951); Vector( -2071.832275, 2530.378418, 65.843414); Vector( -2807.138672, -295.113617, 106.355247); Vector(-3002.123779, 558.179932, 61.393742); Vector( -4178.655273, 2861.287354, 54.987335); Vector( -5955.591797, 2358.774658, -96.954010); Vector( -5057.796875, 2933.024170, 65.260193); Vector( -6776.100586, 2178.492432, 67.716553); Vector( -6641.131836, 2950.413818, 57.023567); Vector( -5462.553223, 7227.875977, 145.432922); }; function SpawnGoldenProp() local ent = ents.Create("goldenprop") -- This creates our zombie entity ent:SetPos(table.Random(goldpropSpawns)) ent:Spawn() ent:Activate() end[/CODE] I do not believe I can do that this way since the Vector's have commas in them... How could I do this?
[QUOTE]ent:SetPos(table.Random([U][B]SpawnTests[/B][/U]))[/QUOTE] Is SpawnTests defined somewhere other than the snippet you pasted, or did you mean to use 'goldpropSpawns', since that's the name of the table you created.
Yeah that was a test I used with two values in the array instead of all of the ones I posted. Even with just the two obvious values it would not spawn.
Unless I am entirely mistaken, you need to replace the semicolons at the end of each Vector in the table with a comma, i.e.: [CODE]local goldpropSpawns = { Vector( -2825.641357, 1420.374146, -3504.891846 ), Vector( -3366.608398, 1450.263184, -3523.183350 ), Vector( -5391.474121, 693.360962, -3518.278564 ), Vector( -4493.145996, 439.987610, -3521.404053 ), Vector( -3952.261963, 582.641602, -3519.968750 ), Vector( -3802.943604, 1792.913818, -3507.057617 ), Vector( -2268.290039, 1683.261597, 69.526070 ), Vector( -1944.526611, 4118.445801, 83.076447 ), Vector( -201.243225, 3668.999023, 87.421951 ), Vector( -2071.832275, 2530.378418, 65.843414 ), Vector( -2807.138672, -295.113617, 106.355247 ), Vector( -3002.123779, 558.179932, 61.393742 ), Vector( -4178.655273, 2861.287354, 54.987335 ), Vector( -5955.591797, 2358.774658, -96.954010 ), Vector( -5057.796875, 2933.024170, 65.260193 ), Vector( -6776.100586, 2178.492432, 67.716553 ), Vector( -6641.131836, 2950.413818, 57.023567 ), Vector( -5462.553223, 7227.875977, 145.432922 ), }[/CODE]
You are mistaken, Semi-colons work the same way in arrays.
Neat, I wasn't aware of that. What are you actually having issues with then? I don't particularly see anything wrong with what you're attempting, given what you have so far. The only possible issue I see is that you're not setting the model on the entity you are creating, but that might be just how I've always done it and seen it done.
The entity itself has the model set inside of the goldenprop.lua inside the entities folder. The issue, I believe, is that the commas inside the Vector values are causing Lua to believe I am seperating the contents within the array at the commas for the vector positions. Most arrays are in string or double values and none I've seen before have a comma within a array locker... Lua must be trying to put all the Vectors into seperate lockers and instead of what I want it to do, it seems to be doing this because of where the commas are located: [CODE]local goldpropSpawns = [] goldpropSpawns[0] = Vector( -2825.641357 goldpropSpawns[1] = 1420.374146 goldpropSpawns[2] = -3504.891846)[/CODE]
Your code looks fine to me. Are there any errors? -snip bad reading- Besides this line ent:SetPos(table.Random(goldpropSpawns) you forgot to add an extra ')' to the end
I'm not sure if I came up with a stupid solution that wasn't needed or a silly solution that was, but this is how I've done it: [code] Positions = {"-2460.031250, 1143.031250, -131.968750", "-3206.575439, 1060.031250, -131.968750", "-4681.512207, 1956.396484, -139.968750"} local rnd = math.random( 1, #Positions ) for i in string.gmatch( Positions[rnd]:gsub(',', ''), "%S+") do table.insert(SpawnVectorTable, i) end SpawnVector.x = SpawnVectorTable[1] SpawnVector.y = SpawnVectorTable[2] SpawnVector.z = SpawnVectorTable[3] [/code] Then set your ent pos to SpawnVector
Rating him dumb for what I actually needed? Thank you for doing that. I was actually in the process of making my own tables for x, y, and z all by hand...this makes that much easier
I'm not having any issue doing the following, which is essentially the same as yours: [CODE] local spawns = { Vector( 0, 0, 0 ); Vector( 500, 500, 500 ); Vector( 1000, 1000, 1000 ); } function spawnTest() local ent = ents.Create( "prop_physics" ) ent:SetModel( "models/Cranes/crane_frame.mdl" ) ent:SetPos( table.Random( spawns ) ) ent:Spawn() ent:Activate() end[/CODE] The prop spawns in correctly at one of the randomly chosen vectors in the table every time I call the function. I just tested it using your vectors and it still works fine.
You are exactly correct. Upon moving it further up the chain in my init file, it worked... Seems I may have another issue unforeseen that breaks nothing but what I was working on... Oh, Lua, my hero. Thank you Mista Tea for helping me out and thanks to Dgc for the random table enterer thing..
Sorry, you need to Log In to post a reply to this thread.