havenphillip Posted October 12, 2019 Posted October 12, 2019 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 Quote
Ma-Shell Posted October 12, 2019 Posted October 12, 2019 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 1 Quote
havenphillip Posted October 12, 2019 Author Posted October 12, 2019 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 Quote
havenphillip Posted October 12, 2019 Author Posted October 12, 2019 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 Quote
havenphillip Posted October 12, 2019 Author Posted October 12, 2019 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 1 Quote
Recommended Posts
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.