Sorry if this come as a noob question but here is my problem.
I have two script, the first is main.lua and the other is bigNumber.lua. My goal is to make some sort of incremental game.
main.lua :
import "bigNumber"
local bank = BigNumber:init(0, 0)
bank.add(bank, BigNumber:init(1, 0))
print(bank.number)
bigNumber.lua :
class('BigNumber').extends()
function BigNumber:init(_number, _power)
self.number = _number
self.power = _power
return self
end
function BigNumber:add(otherBigNumber)
self.number = self.number + otherBigNumber.number;
self.power = self.power + otherBigNumber.power;
return self
end
I just want to create some sort of class that can have a number and a power (both integer) and add two class like that, but in main.lua it print "2" where it should display "1"
Duplicating "bank.add(bank, BigNumber:init(1, 0))" multiple time and it still display "2"
I don't code in Lua but I think that you have to call BigNumber(1, 0) (and not BigNumber:init(0, 0)) to create a new instance. You don't need to return self in the init method.
In your first code block, this line:
bank.add(bank, BigNumber:init(1, 0))
should be:
bank:add(BigNumber(1, 0))
NOTE: I understand that you are just trying things out but for cases like this, it would be better to just do: