Jump to content

surface index out of range?


havenphillip
 Share

Recommended Posts

I keep getting this error. How can I fix this? I'm trying to grab everything in the scene and change the color of it with a bounding box. It works fine so long as I don't put a model in the scene that has a model childed to it. The "generator_withbutton.mdl" is an example. When I put that in the scene I get the error.

 

        for k,v in pairs (self.modelTable) do
            surface = self.modelTable[k]:GetSurface(0)
            mat = surface:GetMaterial()
            mat:SetShader(shader)
            shader:SetVec4("ex_color",self.color)
            mat:SetColor(self.color)
            self.modelTable[k] = nil
        end

Link to comment
Share on other sites

The model probably does not have a surface. You should get the number of surfaces first and then iterate over the surfaces. Something like the following (I don't know much about lua, so probably this code won't work without modification):

for k,v in pairs (self.modelTable) do
    for i = 0, v:CountSurfaces(), 1 do
    	surface = v:GetSurface(i)
    	mat = surface:GetMaterial()
    	mat:SetShader(shader)
    	shader:SetVec4("ex_color",self.color)
    	mat:SetColor(self.color)
    	self.modelTable[k] = nil
    end
end

 

  • Thanks 1
Link to comment
Share on other sites

Ah thanks! This seems to be working so far:

        for k,v in pairs (self.modelTable) do
            for i = 0, self.modelTable[k]:CountSurfaces()-1, 1 do
                surface = self.modelTable[k]:GetSurface(i)
                mat = surface:GetMaterial()
                mat:SetShader(shader)
                shader:SetVec4("ex_color",self.color)
                mat:SetColor(self.color)
                self.modelTable[k] = nil
            end
        end

Link to comment
Share on other sites

Ah man now I'm getting "attempt to index nil value" error when I add a crawler to the scene. What's that about? Do I need to do some kind of CountChildren() cycle?

        for k,v in pairs (self.modelTable) do
            for i = 0, self.modelTable[k]:CountSurfaces()-1, 1 do
                surface = self.modelTable[k]:GetSurface(i)  <- giving me an error on this line
                mat = surface:GetMaterial()
                mat:SetShader(shader)
                shader:SetVec4("ex_color",self.color)
                mat:SetColor(self.color)
                self.modelTable[k] = nil
            end
        end

Link to comment
Share on other sites

Oh, duh. I was telling it to go nil

        for k,v in pairs (self.modelTable) do
            for i = 0, self.modelTable[k]:CountSurfaces()-1, 1 do
                surface = self.modelTable[k]:GetSurface(i)
                mat = surface:GetMaterial()
                mat:SetShader(shader)
                shader:SetVec4("ex_color",self.color)
                mat:SetColor(self.color)
                self.modelTable[k] = nil      -- derp
            end
        end

  • Haha 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...