|
Post by neotk on Mar 10, 2018 21:26:27 GMT
I'm creating a script and would like to use a more object oriented aproach to keep the code organized. However, when loading a file containing a class, it throws the following error:
attempt to call global 'class' (a nil value)
The line throwing the error has this:
MyClass = class(function()
end)
The whole file is this
MyClass = class(function()
end)
function MyClass:doWork()
scriptExit("Work done!")
end
Do you know if Ankulua supports it?
|
|
|
Post by Mercobots on Mar 10, 2018 22:30:06 GMT
hi there Yes is possible but you are using the wrong approch, on using "class" you need a external lib for what you need! For what i see it seems you had seen the lua-user.org examples i use kikito middleclass github.com/kikito/middleclass , of course you need to addapt it to ankulua (change type to typeOf) after this is really easy create OOP local class = require 'middleclass'
local Fruit = class('Fruit') -- 'Fruit' is the class' name
function Fruit:initialize(sweetness) self.sweetness = sweetness end
Fruit.static.sweetness_threshold = 5 -- class variable (also admits methods)
function Fruit:isSweet() return self.sweetness > Fruit.sweetness_threshold end
local Lemon = class('Lemon', Fruit) -- subclassing
function Lemon:initialize() Fruit.initialize(self, 1) -- invoking the superclass' initializer end
local lemon = Lemon:new()
print(lemon:isSweet()) -- false
|
|
|
Post by enissay on Mar 24, 2019 11:26:41 GMT
Hi there, I have a dummy question: how to require a file in "lib/middleclass.lua" for example ? Doing this doesn't work and says module not found package.path = "lib/?.lua;" .. package.path local class = require 'middleclass'
|
|
|
Post by Mercobots on Mar 24, 2019 12:56:25 GMT
Hi there, I have a dummy question: how to require a file in "lib/middleclass.lua" for example ? Doing this doesn't work and says module not found package.path = "lib/?.lua;" .. package.path local class = require 'middleclass' you forgot scriptPatch() package.path = scriptPatch().."lib/?.lua;" .. package.path local class = require 'middleclass'
|
|
|
Post by enissay on Mar 24, 2019 13:34:11 GMT
Thank you legendary Mercobots <3
|
|
|
Post by enissay on Mar 24, 2019 16:06:38 GMT
One more thing as I have trouble linking my files (actually I am refactoring my working script for a more readable OOP approach since I am adding new functionality and it became gigantic and hard to maintain).
Let's say I have a User class which I need to use in my main:
-- main.lua package.path = scriptPath().."lib/?.lua;" .. package.path package.path = scriptPath().."class/?.lua;" .. package.path
local User = require 'User'
u01 = User:new('user_01') ------> ERROR: User is a boolean value ! u01:speak()
-- class/User.lua local class = require 'middleclass'
-- Declaration User = class('User')
-- Attributes User._name = "" User._img = ""
-- Constructor function User:initialize(name) self._name = name end
-- Methods function User:speak() print('Hi, I am ' .. self._name ..'.') end Please advise what I am doing wrong ?
|
|
|
Post by Mercobots on Mar 24, 2019 16:14:32 GMT
Since you are creating a "local" class you need to return the class him self, try to put return User at the end of your class
|
|
|
Post by enissay on Mar 24, 2019 16:18:56 GMT
Awesome, thanks again <3
|
|
edison12344
Contributors
Target Mission: Help Gamers
Posts: 142
|
Post by edison12344 on Mar 26, 2019 1:15:29 GMT
enissay You can easily create a class without the need of external libraries I will teach you First you need to create a class it is created like this --constructor DailyEvent={} function DailyEvent:new(o) o = o or {} -- create table if user does not provide one setmetatable(o, self) self.__index = self --how to create an attribute --must be inside the construction --use self always self.dailyButton=Location(5,5) return o end --method must have that ":" always when creating new method function DailyEvent:start() --notice the self,must have self always click(self.dailyButton) end to create an object from it, you call the constructor -- use ":" to call the object's method dailyEvent=DailyEvent:new() dailyEvent:start()
|
|
|
Post by enissay on Mar 27, 2019 23:43:29 GMT
enissay You can easily create a class without the need of external libraries I will teach you First you need to create a class it is created like this --constructor DailyEvent={} function DailyEvent:new(o) o = o or {} -- create table if user does not provide one setmetatable(o, self) self.__index = self --how to create an attribute --must be inside the construction --use self always self.dailyButton=Location(5,5) return o end --method must have that ":" always when creating new method function DailyEvent:start() --notice the self,must have self always click(self.dailyButton) end to create an object from it, you call the constructor -- use ":" to call the object's method dailyEvent=DailyEvent:new() dailyEvent:start() Hi edison, Thank you for the intro. You must agree that the definition is weird especially for those coming from other oop languages. Hence, the use of such libraries. And being myself a big fan of native coding, I'l certainly give it a try this weekend since I didn't start translating my code yet to oop (also the game got an update and all images have changed :< ), I was just exploring how the syntax needs to be :-)
|
|
edison12344
Contributors
Target Mission: Help Gamers
Posts: 142
|
Post by edison12344 on Mar 28, 2019 13:00:02 GMT
Here is the full tutorial on how to create class,inheritance, multiple inheritance and creating an object. www.lua.org/pil/16.html
|
|
|
Post by enissay on Mar 30, 2019 15:21:40 GMT
Thank you edison12344, What about static attributes/methods ? I'm lost a bit ... EDIT:I tried also to overload "__tostring()" method, but it doesn't seem to work as it keeps printing the type of the object (table) instead of printing my custom message -- User.lua ... function User:__tostring() return '>> ' .. tostring(self.name) .. ':\n' end
-- program.lua ... local u01 = User:new('user_01') print(u01) -- prints 'table' in ankulua instead of the message above :<
|
|