This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
augustas656's profile picture

Functions Inside Table

Started by augustas656, 30 April 2014 - 04:55 PM
augustas656 #1
Posted 30 April 2014 - 06: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
CometWolf #2
Posted 30 April 2014 - 07: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
}
augustas656 #3
Posted 30 April 2014 - 07: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
MKlegoman357 #4
Posted 30 April 2014 - 08: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 on 30 April 2014 - 06:02 PM
HometownPotato #5
Posted 30 April 2014 - 11:51 PM
'local myFunction = …'
That doesn't actually make the function local. You would have to do:
'local myFunction; myFunction = …'
Edited on 30 April 2014 - 09:51 PM
CometWolf #6
Posted 30 April 2014 - 11:52 PM
Yes it does. You don't need to use a forward declaration.
HometownPotato #7
Posted 30 April 2014 - 11:55 PM
Oh never mind, now that I think about it I was getting it confused when using recurse.