• Problems with excluding an entity from a 'for' loop
    2 replies, posted
Yes, this is a repost of my previous thread, but just for the sake of me really needing help. I need some help getting part of a script to work. My issue is that I need a value to be a string rather than userdata in a string.find, but I've heard it's pretty much impossible to convert, so I'm hoping it's possible to just use the context of what I'm trying to do to properly get the var I would like. Take a look at the example below to know what I mean. [CODE] --An example, trying to find whether a prop (in this case, a barrel) exists in an environment or not. for k, v in pairs( ents.GetAll() ) do for i=0,v:GetPhysicsObjectCount() do local bone=v:GetPhysicsObjectNum(i) if IsValid(bone) then --I NEED to validate it as I need to use it later (you'd know why if this was the original script, but it shouldn't be important). if not string.find( bone, "models/props_c17/oildrum001.mdl" ) then --'string' expected, got userdata print("Barrel not found!") else print("Barrel found!") end end end end end [/CODE] In case you missed it, [CODE] if IsValid(bone) then [/CODE] returns a userdata value rather than a string, making the string.find impossible. Please help. Also, in case it helps, there is an entity using this script, and I would like the script to exclude the entity (assuming it has a barrel as it's model, as in this example). Maybe I can just use the context of what I want to do?
[code]if v:GetModel() ~= "models/props_c17/oildrum001.mdl" then[/code] [URL="http://wiki.garrysmod.com/page/Entity/GetModel"]Entity/GetModel[/URL] You can also use tostring( physobj ), but the model path is sometimes going to be invalid. [code] print(this:GetModel()) --OUTPUT: models/mossman.mdl print(tostring(this:GetPhysicsObjectNum(1))) --OUTPUT: PhysObject [Mossman.mdl][Entity 645] [/code]
Side note: you should make your code indentation more consistent. Here's a simple example: [url]http://en.wikipedia.org/wiki/Indent_style#Lisp_style[/url] This will help [b][i]massively[/i][/b] with development.
Sorry, you need to Log In to post a reply to this thread.