Jump to content

Recommended Posts

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 prefab

inst.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 CONVINIENCEend

But 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 by pickleplayer

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...

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"

@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.

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
  • Create New...