• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua How to make an in lua defined class and call as parameter of a C++ function

darkshin

New old member
Joined
Dec 14, 2010
Messages
231
Reaction score
22
Hey guys!! I'm having a bad time programming here. So I come here to ask for help!
The question is:
"How to make an in lua defined class and call as parameter of a C++ function?"

Explanation:

I have a class in sources like "Creature" and I want to call a function from the sources inside a lua script, the function would be:
Code:
"sendCreatureStats(const Creature& creature)"

So, in my Lua script would look like this:
Code:
local function sendCreatureMove()

local creature = {} --Is this correct as a class?

creature.health = 100
creature.mana = 100

g_game.sendCreatureStats(creature)

end

So when I try to run this code, that returns me an error saying: "attempt to cast a lua number value to class Creature"

Help is appreciated !!
Thanks!!
 
This has all the basic elements of a class like structure in c++, php, java and many more languages except for visibility modifiers.
Code:
x = {id = {1234567}, name = 'your name'} -- default values, inherited
x.__index = x

setmetatable(x, {
    __call = function(cls, ...)
      return cls.constructor(...)
    end,
   })
-- make shift constructor, hence the name :p
function x.constructor(name, id)
    local self = setmetatable({}, x)
    self.id = id or x.id
    self.name = name or x.name
    return self
end

-- get method
function x:get()
    if type(self.id) == 'table' then
        return self.name, unpack(self.id)
    end
    return self.name, self.id
end

-- set method
function x:set(name, id)
    self.name = name
    self.id = id
    return self:get()
end
test
Code:
print(x():get())
print(x('codex', 5678):get())
print(x():set("some one else's name", 45678))
output
Code:
your name    1234567
codex    5678
some one else's name    45678
 
Last edited:
If you just want to inherit Creature then you would do something like this
Code:
x = Creature
x.__index = x
Now x can use all the same methods that a Creature can, provided you apply the basic template suggested.

You would want to write new metamethods that uses the parent metatable's metamethods, allowing you to extend or expand upon the functionality
 
Last edited:
If you just want to inherit Creature then you would do something like this
Code:
x = Creature
x.__index = x
Now x can use all the same methods that a Creature can, provided you apply the basic template suggested.

You would want to write new metamethods that uses the parent metatable's metamethods, allowing you to extend or expand upon the functionality

Wow! Thats complex... I will study it and make some tries, I can barely understand that right now... in the meanwhile, can you help me with one more thing?

Problem: at the output, I receive a name for each i. But for level, I receive each level of every account.characters for each i. So it would look like this:
Code:
Considering "size = 2"
-- a name for each character
name = {DarkShin} {Codex}
-- each level for every character
level = {10, 50} {10, 50}

When I want it:
Code:
Considering "size = 2"
-- a name for each character
name = {DarkShin} {Codex}
-- a level for every character
level = {10} {50}

Can you see whats going wrong in the code? Cause I'm blind as hell hahaha
Thanks Codex!!
 
Last edited:
have you tested the return value of getLevelByName?

Ive added a print, just below getLevelByName, to print the results, take a look:

desktop_error.jpg


At the server, we can see that the value was returned individually, while at the client it was casted once by character, resulting in 5 tables of 5 results. When I wanted 5 tables with 1 result. So the issue is at the parsing method, as you expected?
 
@Codex NG I've found where the mistake was, and it wasn't at parsing or sending. Thanks for the attention to this matter! Regarding the another, I will spent more time on it another day. #Tired xD
 
@Codex NG I've found where the mistake was, and it wasn't at parsing or sending. Thanks for the attention to this matter! Regarding the another, I will spent more time on it another day. #Tired xD
I am sorry I couldn't help, my c++ is terrible, I was backtracking thinking maybe the output for level was binding and this would possibly be why your returning 5 tables instead of 1? idk lol
 
I am sorry I couldn't help, my c++ is terrible, I was backtracking thinking maybe the output for level was binding and this would possibly be why your returning 5 tables instead of 1? idk lol

Hahaha no problem! But maybe u can help me now with the main problem. Ill try something and post it for you to see. Im doing it for a week now :o. You are really helping me
 
Last edited:
AMAGAD IT WORKED IT ALL OUT !!!!!!!!!!!!!!!! UHUUUULL IM SO HAPPY! HUUUGE THANKS CODEX!!
Well that is great, I am sure I wasn't much help in the literal sense but maybe you just needed to type it out or have a discussion on the matter to help you see where your errors were, I do that myself as well, since I personally don't know many actual programmers in real life i'll just ramble on with friends even if they can't follow along until the solution dawns on me :p
 
Well that is great, I am sure I wasn't much help in the literal sense but maybe you just needed to type it out or have a discussion on the matter to help you see where your errors were, I do that myself as well, since I personally don't know many actual programmers in real life i'll just ramble on with friends even if they can't follow along until the solution dawns on me :p
Yeah you're right. Also I learned alot with you lua class up there. I realized how the system worked out and I followed the idea. I will send you a pic of the work later, once I let it pretty :)
 
Back
Top