I don't know how make classes in lua so I used code which was recommended on forum.
But always only one object works. First one have coordinates x,y and the other object share his coordinates. Can you explain what I´m doing wrong in this code.
Thank you for your advice.
My code:
require("class")
asteroid = class:new()
function asteroid:init(x,y)
asteroid.b = love.physics.newBody(world, x ,y , "dynamic")
asteroid.s = love.physics.newCircleShape(35)
asteroid.f = love.physics.newFixture(asteroid.b, asteroid.s)
end
function love.load()
world = love.physics.newWorld(0, 50, true)
asteroid1= asteroid:new(100,100)
asteroid2= asteroid:new(700,100)
end
function love.update(dt)
world:update(dt)
end
function love.draw()
love.graphics.circle("line", asteroid1.b:getX(),asteroid1.b:getY(), asteroid1.s:getRadius(), 35)
love.graphics.circle("line", asteroid2.b:getX(),asteroid2.b:getY(), asteroid2.s:getRadius(), 35)
end
Recommended code:
__HAS_SECS_COMPATIBLE_CLASSES__ = true
local class_mt = {}
function class_mt:__index(key)
return self.__baseclass[key]
end
class = setmetatable({ __baseclass = {} }, class_mt)
function class:new(...)
local c = {}
c.__baseclass = self
setmetatable(c, getmetatable(self))
if c.init then
c:init(...)
end
return c
end
Here is a demo code for you
local MyClass = {}
MyClass.__index = MyClass
setmetatable(MyClass, {
__call = function (cls, ...)
return cls.new(...)
end,
})
function MyClass.new(init)
local self = setmetatable({}, MyClass)
self.value = init
return self
end
-- the : syntax here causes a "self" arg to be implicitly added before any other args
function MyClass:set_value(newval)
self.value = newval
end
function MyClass:get_value()
return self.value
end
local instance = MyClass(5)
-- do stuff with instance...
I would suggest you to follow these tutorials
http://lua-users.org/wiki/ObjectOrientationTutorial
http://lua-users.org/wiki/TutorialDirectory
The : syntax causes an implicit self to be available as a local, referring to the object instance. But you are assigning to b at class level. Use self.b = instead of asteroid.b = so that the assignment is specific to the instance.
Related
I'm still fairly new to OOP but I can't figure out what I'm doing wrong here.
-- TestModule
testClass = {}
local thingClassModule = require(script["ThingModule"])
function testClass:new()
setmetatable({}, self)
self.__index = self
self.Thing = thingClassModule:new(10, 15)
self.Thing2 = thingClassModule:new(30, 70)
end
return testClass
the thing class module:
-- ThingModule
thing = {}
function thing:new(a, b)
local obj = {}
setmetatable(obj, self)
self.__index = self
self.A = a or 0
self.B = b or 0
return obj
end
return thing
The issue is that Thing gets overriden by Thing2.
I believe your problem is not assigning self to the metatable of the object instead it's being assigned to the module instead, therefore it's overwriting the data instead of creating a new object.
Here's a working example of what you're trying to achieve:
-- Thing Class
local Thing = {}
Thing.__index = Thing
function Thing.new(a, b)
local self = {}
setmetatable(self, Thing)
self.A = a
self.B = b
return self
end
return Thing
local Thing = require(script.Parent.Thing)
local TestClass = {}
TestClass.__index = TestClass
function TestClass.new()
local self = {}
setmetatable(self, TestClass)
self.Thing = Thing.new(10, 15)
self.Thing2 = Thing.new(30, 70)
return self
end
return TestClass
You can learn more about Object-oriented through this great article:
https://devforum.roblox.com/t/all-about-object-oriented-programming/8585
In a method, self is whatever comes before the colon in the method call. In thing.new, you create a new table called obj, but you then assign A and B within self, which is the thing table (or thingClassModule; they're both the same table). In testClass.new, you set the metatable of a new table, but you don't store it to a variable. You then go on to modify self, which is testClass.
I am building a game with enemies and players, all of which are set up to have various states for animation and behavior. The parent class for both is Entity.lua, from which they inherit variables and methods. However, while both enemies and players are inheriting the variables, for some reason the enemies do not inherit the methods. So, if I try to call snakey:changeState('search'), for example, it gives me an error message "Attempt to call method 'changeState' (a nil value)".
I have used the same sequence for creating entities in several games in the past and never had this problem. In fact, if I create the Player in the same way, file and location as the enemies, I receive no error messages.
Here is the code where I create the entities.
local snakey
snakey = Snakey {
platform = platform,
player1 = self.player1,
player2 = self.player2,
stateMachine = StateMachine {
['search'] = function() return SnakeySearchState(snakey) end,
['chasing'] = function() return SnakeyChasingState(snakey) end,
['idle'] = function() return SnakeyIdleState(snakey) end
}
}
-- snakey:changeState('search')
-- snakey.stateMachine:change('search', params)
table.insert(self.entities, snakey)
The two coded out lines are where I noticed the problem. The first line gives and error and the second does work, but is not satisfactory because it is a work-around.
Here is the code for Entity.lua: I don't include details of the functions for brevity, but all are working properly for when player calls them.
Entity = Class{}
function Entity:init(def)
-- position
self.x = def.x
self.y = def.y
self.gravity = 6
-- many more variables
end
function Entity:changeState(state, params)
self.stateMachine:change(state)
end
function Entity:update(dt)
self.stateMachine:update(dt)
end
function Entity:collides(entity)
-- do something
end
function Entity:onDamage()
-- do something
end
function Entity:render()
- renders sprite
end
Player code (in brief)
Player = Class{__includes = Entity}
function Player:init(def)
Entity.init(self, def)
-- more variables
end
function Player:update(dt)
Entity.update(self, dt)
end
function Player:render()
Entity.render(self)
end
And perhaps the trouble spot, one one enemy's script
Snakey = Class{__includes = Entity}
function Snakey:init(def)
Entity.init(self, def)
-- yet more variables
end
function Snakey:update(dt)
Entity.update(self, dt)
-- entity behavior (works fine, so omitted)
end
function Snakey:render()
Entity.render(self)
end
Thank you very much for your help. I'm feeling quite frustrated because this sequence has worked in the past and I would really like to know why it's not calling those Entity methods.
Adding the class library
--Copyright (c) 2010-2013 Matthias Richter
local function include_helper(to, from, seen)
if from == nil then
return to
elseif type(from) ~= 'table' then
return from
elseif seen[from] then
return seen[from]
end
seen[from] = to
for k,v in pairs(from) do
k = include_helper({}, k, seen) -- keys might also be tables
if to[k] == nil then
to[k] = include_helper({}, v, seen)
end
end
return to
end
-- deeply copies `other' into `class'. keys in `other' that are already
-- defined in `class' are omitted
local function include(class, other)
return include_helper(class, other, {})
end
-- returns a deep copy of `other'
local function clone(other)
return setmetatable(include({}, other), getmetatable(other))
end
local function new(class)
-- mixins
class = class or {} -- class can be nil
local inc = class.__includes or {}
if getmetatable(inc) then inc = {inc} end
for _, other in ipairs(inc) do
if type(other) == "string" then
other = _G[other]
end
include(class, other)
end
-- class implementation
class.__index = class
class.init = class.init or class[1] or function() end
class.include = class.include or include
class.clone = class.clone or clone
-- constructor call
return setmetatable(class, {__call = function(c, ...)
local o = setmetatable({}, c)
o:init(...)
return o
end})
end
-- interface for cross class-system compatibility (see https://github.com/bartbes/Class-Commons).
if class_commons ~= false and not common then
common = {}
function common.class(name, prototype, parent)
return new{__includes = {prototype, parent}}
end
function common.instance(class, ...)
return class(...)
end
end
-- the module
return setmetatable({new = new, include = include, clone = clone},
{__call = function(_,...) return new(...) end})
It turns out that sequence matters. In trying to create the minimum reproducible code I could not reproduce the error. After some searching (and a little frustration), I noticed that in Dependencies.lua I was requiring the enemies BEFORE Entity.lua, but Player.lua was required after. I would have thought this wouldn't matter, since everything was imported into the program on frame 1 and I was creating entities on something like frame 1000, but alas. Anyway, problem solved! Always require parent classes before child classes... Lesson learned. :)
I just started programming in lua and I created sort of oop structure following this tutorial: http://tylerneylon.com/a/learn-lua/
Problem is, when I created function that returns object or table of objects and call it inside constructor, it returns nil.
Here is my code for first object:
require "ObjectB"
ObjectA = {}
function ObjectA:new(num)
newInstance = {}
newInstance.var = self:foo(num)
self.__index = self
return setmetatable(newInstance, self)
end
function ObjectA:foo(num)
return ObjectB:new(num)
end
, and for second object:
ObjectB = {}
function ObjectB:new(num)
newInstance = {}
newInstance.num = num
self.__index = self
return setmetatable(newInstance, self)
end
When I do this:
myObject = ObjectA:new(5)
print(myObject.var.num)
, I get error: "Error: main.lua:14: attempt to index field 'var' (a nil value)".
But when I do this:
myObject = ObjectA:new(5)
myObject.var = ObjectA:foo(5) //setting var by calling foo outside of constructor
print(myObject.var.num)
, everything seems to work fine and print result is really 5. Can anyone tell me what is reason for this strange behaviour or what am I doing wrong here?
Variables are global by default, so the two variables newInstance in ObjectA:new and ObjectB:new are the same global variables, you assign it a new value, the previous value is gone.
Instead, use local variables like this:
function ObjectA:new(num)
local newInstance = {}
--the rest
end
and
function ObjectB:new(num)
local newInstance = {}
--the rest
end
The data is not inserted successfully.
Output:
dataHolder.variableNames = []
when it should be :
dataHolder.variableNames = [{'area_12345[<>]6789'}, {'apollo123'}, {'guruX'}, {'ok'}];
% USAGE:
elementNames = {'area_12345[<>]6789', 'apollo123', 'guruX', 'ok'};
elementTypes = {'string', 'specialChar', 'int', 'float'};
elementValues = {'charlie', 'vvv', '09', '123.321'};
dataHolder = dynamicVariableNaming;
str = 'test';
result = dataHolder.ensureCellType(str);
for i = 1:3
dataHolder.addVariables(elementNames(i), elementTypes(i), elementValues(i));
end
dataHolder.variableNames
%%% CLASS
classdef dynamicVariableNaming
%HELLO Summary of this class goes here
% -
properties
variableNames = [];
variableValues = [];
variableTypes = [];
end
methods (Access = public) % (Access = private)
function obj = dynamicVariableNaming (variableName, variableValue, variableType)
% class constructor
if(nargin > 0)
obj.variableNames = variableName;
obj.variableValues = variableValue;
obj.variableTypes = variableType;
end
end
% end
%
% methods (Static = true)
function addVariables (obj, variableName, variableValue, variableType)
obj.variableNames = [obj.variableNames ensureCellType(obj, variableName)];
obj.variableValues = [obj.variableValues ensureCellType(obj, variableValue)];
obj.variableTypes = [obj.variableTypes ensureCellType(obj, variableType)];
end
function cellData = ensureCellType(obj, value)
if (~strcmp(class(value), 'cell'))
cellData = {value};
% cell2string(value);
else
cellData = value;
end
end
end
end
You are not returning the changed opbject from the addVariables method as required when you are working with non-handle objects. Remember, matlab is different in comparison with other reference passing-based languages.
To fix it either make your class enherit from the handle class, or return obj from addVariables
Cant say if there are other problems in the code due to its poor formatting and inability to run in matlab (unbalanced ends, missing constructors, etc.)
So, I'm trying to write a simple class in Lua for representing CSV fields:
csv_entry = {}
csv_entry.__index = csv_entry
function csv_entry.create(...)
return arg
end
function csv_entry:tostring()
local str = string.char()
for i,v in ipairs(self) do
if i < #self then
str = str .. v
else
str = str .. v .. ", "
end
end
end
function csv_entry:print()
print(self:tostring())
end
But when I try to use this class like this:
c = csv_entry.create("Volvo", 10000, "Eric")
c:print() -- line 25
I get the error message
lua: csv.lua:25: attempt to call method 'print' (a nil value)
And I can't really figure out the issue here. What am I doing wrong?
You probably meant to do is this:
function csv_entry.create(...)
return setmetatable(arg, csv_entry)
end
Your posted version of cvs_entry.create just returns it's arguments packed into a table, so this code:
c = csv_entry.create("Volvo", 10000, "Eric")
c:print()
Is exactly equivalent to this code:
c = {"Volvo", 10000, "Eric"}
c:print()
c doesn't contain a print entry, so c.print returns nil and c:print() fails because you're trying to "call" nil.
Side note: the implicit arg parameter to variadic functions was removed in Lua 5.1 (6 years ago). The correct way to do this now is:
function csv_entry.create(...)
local arg = {...}
return setmetatable(arg, csv_entry)
end
Or simply:
function csv_entry.create(...)
return setmetatable({...}, csv_entry)
end
As long as we're here: you're going to get no output from csv_entry:tostring because it doesn't return anything. Also, if all you're trying to do is to concatenate a bunch of items with comma separators, you can use table.concat:
function csv_entry:tostring()
return table.concat(self, ', ')
end
I rewrite your code to meet what it is for, it runs OK for me:
csv_entry = {}
function csv_entry:create(...)
o = {content = {}}
self.__index = self;
setmetatable(o, self)
for i = 1, arg.n do
o.content[i] = arg[i];
end
return o;
end
function csv_entry:tostring()
local resStr = ""
for i, v in pairs(self.content) do
resStr = resStr .. v;
if i < #(self.content) then
resStr = resStr .. ", "
end
end
return resStr;
end
function csv_entry:print()
print(self:tostring())
end
c = csv_entry:create("Volvo", 10000, "Eric")
c:print()
Like #Mud said, the create(...) in your code is just a regular call and returns all arguments from ..., if you want csv_entry works like a class, then you have to put codes which set metatable and __index into create(...), and return an instance from csv_entry class