• I've never used tables before, halp
    7 replies, posted
I looked through the wiki page and it wasn't that helpful with tables. What I'm trying to do is use ents.FindInSphere() to make a table of entities, then choose an entity from the table based on it's class (IE if there's a jeep, select it, if theres no jeep but there is an airboat, select that, if there's neither select a random entity) I can probably work out most of it, in particular I just have no idea how to select certain entities from a table of them. All of this keeping in mind there might not be an entity in the sphere. Anyone got a brief rundown on how to do stuff like this?
well , i'm not too sure about this. Been a while since i've used tabled myself but [lua] function spherestuff() local orgin_ents = ents.FindInSphere( Vector(0, 0, 0), 32 ) for k,v in pairs(origin_ents) do if v:GetClass() == "prop_physics" then -- do shit end [/lua] This is bound not to work , but you get the idea.
From that, if "v:GetClass() == "prop_physics"", does that mean during that if statement "v" is the prop_physics?
Try this. [lua]local mytable = ents.FindInSphere(mypos,mydist) -- Returns a sequential table of entities for key,value in ipairs(mytable) -- Loops through the table, one key/value pair at a time local entity = value -- not really necessary, but shows you that the value of each table entry is an entity if string.find(entity:GetClass(),"prop_physics") then -- If you can find the word 'prop_physics' in the entity's class then (some are prop_physics_multiplayer see) -- do mah shits end end[/lua] A sequential table is one where all the keys are numbers in order. Tables returned by player.GetAll() or the ents functions are sequential. Any table initialised by the form mytable = { "value", "value2", "value3" } is also sequential. Generally anything else isn't, and you should use pairs instead.
If you don't know how to do this by yourself, you should read the pil better.
the pill?
[url=http://www.lua.org/pil/]PiL[/url]
Well that's going to be helpful. I really should have thought about finding out more about lua, rather than just winging it in gmod from the start :P
Sorry, you need to Log In to post a reply to this thread.