• Entity method argument sending as nil?
    8 replies, posted
Hey! I have an entity which I need to send a umsg with. The only way i seem to be able to achieve this with is if I do a normal method like so: [CODE]function sendInfo(argString) end[/CODE] Which works just fine if I call it like so: [code] sendInfo(self.MyString) [/code] However, if I change the sendInfo method to: [code] function ENT:sendInfo(argString) end [/code] Then anything that gets passed through the argument is nil. Is this because my MyString is declared like so: [CODE]ENT.MyString = "nothing"[/CODE] It has a value before sending. What can I do to make it work?
Show us how you're actually calling ENT:senfInfo
[code] function ENT:StartTouch( hitEnt ) self.sendInfo(self.Model2) end [/code] Thats the way I call it to replicate the problem.
You're supposed to use colons when calling methods. [code]ent.Stuff( ent )[/code] is the same as [code]ent:Stuff()[/code] And when you declare a function with the colon operator (a method) then it automatically assigns the 1st argument as being "self" and thus you must call it again with the colon operator if you want it to automatically set itself as the first parameter. [editline]1st December 2013[/editline] That means that this: [code]function ENT.Stuff( self ) end[/code] is the same as [code]function ENT:Stuff() end[/code]
Your a life saver. Would have never figured that out. Thanks! [editline]1st December 2013[/editline] usermessage.Hook("informationModel", self:getInfo) What about doing it in there? It just throws an error when doing it there.
You could do something like: [code]usermessage.Hook( "informationModel", function( um ) um:ReadEntity( ):GetInfo( um ) end )[/code] Which would expect the first entity of the message to be an instance of your sent.
[QUOTE=Chizbang;43036951]Your a life saver. Would have never figured that out. Thanks! [editline]1st December 2013[/editline] usermessage.Hook("informationModel", self:getInfo) What about doing it in there? It just throws an error when doing it there.[/QUOTE] The colon operator is [I]only[/I] used for defining and calling methods. You cannot access the reference to the function through a method. However, it's actually really simple to fix. Change your colon to a dot, [I]however[/I] I kind of doubt that "self" is defined in the scope that you're defining that function in. Also, why are you using usermessages? They're outdated.
I have moved on to Data Tables to do it. Would that be best? It seems to work for the most part.
Depends what you're trying to do.
Sorry, you need to Log In to post a reply to this thread.