pickleplayer Posted December 4, 2015 Share Posted December 4, 2015 (edited) I'm really confused right now.Okay so I have a "hitbox" prefab and a "hitboxes" component. I'm trying to get the hitbox prefab to access a regular function in the hitboxes component. Real basic, right? This function runs in the hitbox prefabinst.components.hitboxes:UpdatePosition()And that calls this function in the hitboxes component.function Hitboxes:UpdatePosition() self.inst.Transform:SetPosition( 0,0,0 ) --A SHORTENED EXAMPLE FOR CONVINIENCEendBut it crashes the game with "attempted to index local 'self' (a nil value)" BUT; I can put "inst.components.hitboxes:UpdatePosition()" in any other prefab's code, like my character's prefab file, and it works fine. (for some weird reason). (assuming I also give that prefab the hitboxes component) But when I throw this exact same line of code in the hitbox's prefab file, I get the error"attempted to index local 'self' (a nil value)" And I just don't understand what's different. They both use the same component. Hitboxes.lua is set up the same way all my other custom components are. The hitbox prefab was based off of a random object prefab like nitre.lua I think I've been misunderstanding how "self.inst" actually works. Can someone explain whats going on? (or maybe how to fix it)I thought using "self.inst" in a component is just a reference to whatever is using that component. Is that not the case? Edited December 4, 2015 by pickleplayer Link to comment https://forums.kleientertainment.com/forums/topic/60028-problems-using-selfinst-in-components/ Share on other sites More sharing options...
Mobbstar Posted December 4, 2015 Share Posted December 4, 2015 In the constructor of a component, the component usually sets "self.inst" to the instance it comes from. If you don't do that, self.inst is nil. However, in your case self is nil. Which is ridiculous. "Component:Function()" should pass self as an variable... Link to comment https://forums.kleientertainment.com/forums/topic/60028-problems-using-selfinst-in-components/#findComment-692500 Share on other sites More sharing options...
pickleplayer Posted December 4, 2015 Author Share Posted December 4, 2015 In the constructor of a component, the component usually sets "self.inst" to the instance it comes from. If you don't do that, self.inst is nil. However, in your case self is nil. Which is ridiculous. "Component:Function()" should pass self as an variable... Yea. And self.inst is declared in the constructor.So I really have no idea why its not recognizing "self" Link to comment https://forums.kleientertainment.com/forums/topic/60028-problems-using-selfinst-in-components/#findComment-692505 Share on other sites More sharing options...
Blueberrys Posted December 5, 2015 Share Posted December 5, 2015 @pickleplayer I think there might be another problem, would it be possible to see the rest of the code for that function? You can read about the colon/self syntax here (temporary link due to hosting issues). self.inst is simply "the inst variable within the self table". Nothing fancy. In the constructor, the instance of whatever uses the component is usually passed in and stored into self.inst. That said, your code/error doesn't have a problem with "inst", it has a problem with "self" being nil. That might be due to using it elsewhere in the function (or component?) or something with the syntax being off somewhere. Link to comment https://forums.kleientertainment.com/forums/topic/60028-problems-using-selfinst-in-components/#findComment-692764 Share on other sites More sharing options...
pickleplayer Posted December 5, 2015 Author Share Posted December 5, 2015 oooohhhh my god I cannot believe... My dumb self was using "inst.components.hitboxes.UpdatePosition()" in the hitbox prefab file.With a period instead of a colon. please disregard my stupidity Link to comment https://forums.kleientertainment.com/forums/topic/60028-problems-using-selfinst-in-components/#findComment-692937 Share on other sites More sharing options...
Mobbstar Posted December 5, 2015 Share Posted December 5, 2015 I'm going to kill a kitten now. Other than that, your stupidity is disregarded. Link to comment https://forums.kleientertainment.com/forums/topic/60028-problems-using-selfinst-in-components/#findComment-692945 Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now