Jump to content




Functions Inside Table


  • You cannot reply to this topic
6 replies to this topic

#1 augustas656

  • Members
  • 158 posts

Posted 30 April 2014 - 05:55 PM

Can you do that?

I'd like to do something like:

object = {
health = 10
  function removeHealth(health)
  self.health = self.health - health
  }
}
zombie = object
zombie.removeHealth(9)
print(zombie.health)

How could I?

Regards,
Augustas

#2 CometWolf

  • Members
  • 1,283 posts

Posted 30 April 2014 - 06:02 PM

looks to me like you got that code from somewhere that probably shows you how,given your use of the self variable... Anyways
object = {
  health = 10,
  removeHealth = function(self,health)
    self.health = self.health - health
  end
}


#3 augustas656

  • Members
  • 158 posts

Posted 30 April 2014 - 06:57 PM

Not really, I know lua a limited level because of computercraft, but I know java object oriented programming, I'm trying to get the true version of how you do it in java by writing what I think is the closest between java and lua. Since I know you kind of have to do it in tables, nvm, I'm not gonna explain how my brain works xD

Thanks

#4 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 30 April 2014 - 07:00 PM

Creating functions is as easy as creating a simple variable. Functions act as variables:

local myFunction = function (text)
  print(text)
end

function myFunction () ... end is only a syntactical sugar. That's why you can assign functions to any variable you want.

Also, this line here:

zombie = object

Variables don't actually hold a table, only it's pointer to the memory, which means that now those two variables hold the same table. To have a copy of the table you could make a function which would take all contents from a table and put it into a new table.

It looks like you're creating a some sort of game using 'classes' in Lua. Many people prefer to use setmetatable to 'copy' and make a new instance of that table:


Using your metod:
local myClass = {
text = "Hello"
}

local myClassInstance = myClass

print(myClassInstance.text) -->> Hello
print(myClass.text) -->> Hello

myClassInstance.text = "World"

print(myClassInstance.text) -->> World
print(myClass.text) -->> World

Using setmetatable:
local myClass = {
text = "Hello"
}

local myClassInstance = setmetatable({}, {__index = myClass})

print(myClassInstance.text) -->> Hello
print(myClass.text) -->> Hello

myClassInstance.text = "World"

print(myClassInstance.text) -->> World
print(myClass.text) -->> Hello

If you have questions just ask!

Off topic: is there a chance you're from in Lithuania :P/>?

Edited by MKlegoman357, 30 April 2014 - 07:02 PM.


#5 HometownPotato

  • Members
  • 62 posts

Posted 30 April 2014 - 10:51 PM

'local myFunction = ...'
That doesn't actually make the function local. You would have to do:
'local myFunction; myFunction = ...'

Edited by HometownPotato, 30 April 2014 - 10:51 PM.


#6 CometWolf

  • Members
  • 1,283 posts

Posted 30 April 2014 - 10:52 PM

Yes it does. You don't need to use a forward declaration.

#7 HometownPotato

  • Members
  • 62 posts

Posted 30 April 2014 - 10:55 PM

Oh never mind, now that I think about it I was getting it confused when using recurse.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users