Jump to content

[HELP] Function transfer from within constructor


Recommended Posts

Hi,

 

I created a constructor function to create an object. Inside this function, a function named "onClick" is defined and this function is mandatory because I am writing a DX library. However, when I create the object, even if I define the onClick function inside the object, it always returns the value as "nil". I did a few tests, it has nothing to do with mouse locations or anything else. The button click function works, the only problem is that I cannot define the onClick function.

 

-- // Button.lua constructor:
function createButton(config)
    local self = setmetatable({}, Button)

    self.x = config.x
    self.y = config.y
    self.width = config.width
    self.height = config.height
    self.text = config.text or "Button"
    self.color = config.color or "blue"
    self.fade = config.fade ~= false
    self.onClick = config.onClick or false -- // This always returns false, even if I added onClick function.
    self.alpha = self.fade and 0 or 255
    self.fade_step = 15
    self.visible = true

    self.currentColor = Theme.colors.button[self.color].background

    table.insert(activeButtons, self)

    return self
end

-- // Example.lua, example for using:
loadstring(exports.ui:injectModules())()

local btn = Button({
    x = 20,
    y = 60,
    width = 100,
    height = 30,
    text = "Click",
    color = "blue",
    fade = true,

    onClick = function(self)
        outputChatBox("Clicked the button.") -- // It's not working.
    end
})
 
Link to comment
  • Moderators
11 hours ago, eksorz said:

it always returns the value as "nil".

One of the common problems is that function values are not clone able. Functions rely on their context and can therefore not being copied to another context.

But not sure if that is the issue.

 

For the Button class I am missing some context. In the first file it is used as a metatable.

And in the second file it is used as a function. (which might also be a metatable, but I am unable to verify it because it is missing)

 

11 hours ago, eksorz said:
loadstring(exports.ui:injectModules())()

Is this export importing Button.lua?

 

Other:

 

Link to comment

 

I am sure that the problem is not in the injectModules() function. There is a prepared Window class next to the Button class, I can use it as Window({...}) by using the injectModules() function. Similarly, I can print the button and get the argument variables by using Button({...}). Only function operations return "nil" value.
Link to comment
  • Moderators
Quote

I am sure that the problem is not in the injectModules() function. There is a prepared Window class next to the Button class, I can use it as Window({...}) by using the injectModules() function. Similarly, I can print the button and get the argument variables by using Button({...}). Only function operations return "nil" value.

Hmm.

 

Can you show a trackback from your button constructor?

iprint(debug.traceback())

Just to get an idea trough how many other functions it's constructed. Not sure what the results are, but it might give you some other places to look at and find where your value gets lost.

Make sure to only construct one thing at the time, else it generates too much.

Link to comment
44 minutes ago, IIYAMA said:

Hmm.

 

Can you show a trackback from your button constructor?

iprint(debug.traceback())

Just to get an idea trough how many other functions it's constructed. Not sure what the results are, but it might give you some other places to look at and find where your value gets lost.

Make sure to only construct one thing at the time, else it generates too much.

I printed the traceback method you gave me right after creating the Button object. In the line I printed, it said "in main chunk", I don't know what it means.

Link to comment
  • Moderators
1 hour ago, eksorz said:

I printed the traceback method you gave me right after creating the Button object. In the line I printed, it said "in main chunk", I don't know what it means.

Like this?

function createButton(config)
    local self = setmetatable({}, Button)
    iprint(debug.traceback())
    self.x = config.x
    self.y = config.y
    self.width = config.width
    self.height = config.height
    self.text = config.text or "Button"
    self.color = config.color or "blue"
    self.fade = config.fade ~= false
    self.onClick = config.onClick or false -- // This always returns false, even if I added onClick function.
    self.alpha = self.fade and 0 or 255
    self.fade_step = 15
    self.visible = true

    self.currentColor = Theme.colors.button[self.color].background

    table.insert(activeButtons, self)

    return self
end

 

Link to comment
2 hours ago, IIYAMA said:

Like this?

function createButton(config)
    local self = setmetatable({}, Button)
    iprint(debug.traceback())
    self.x = config.x
    self.y = config.y
    self.width = config.width
    self.height = config.height
    self.text = config.text or "Button"
    self.color = config.color or "blue"
    self.fade = config.fade ~= false
    self.onClick = config.onClick or false -- // This always returns false, even if I added onClick function.
    self.alpha = self.fade and 0 or 255
    self.fade_step = 15
    self.visible = true

    self.currentColor = Theme.colors.button[self.color].background

    table.insert(activeButtons, self)

    return self
end

 

No, like this:

Button({
    ...
})

iprint(debug.traceback())
Link to comment

try this

 

function CreateBtn(config)
    local self = setmetatable({}, Button)

    self.x = config.x
    self.y = config.y
    self.width = config.width
    self.height = config.height
    self.text = config.text or "Button"
    self.color = config.color or "blue"
    self.fade = config.fade ~= false
    self.onClick = config.onClick or false -- // This always returns false, even if I added onClick function.
    self.alpha = self.fade and 0 or 255
    self.fade_step = 15
    self.visible = true

    self.currentColor = Theme.colors.button[self.color].background

    table.insert(activeButtons, self)

    return self
end

function createButton(x,y,w,h,text,color,fade,onClick)
return CreateBtn({x=x,y=y,width=w,height=h,text=text,color=color,fade=fade,onClick=onClick})
end


local btn1 = createButton(50, 200, 120, 40, "Button 1", "blue", true, function(self)
    outputChatBox("Clicked Button 1")
end)

local btn2 = createButton(150, 360, 120, 40, "Button 2", "red", true, function(self)
    outputChatBox("Clicked Button 2")
end)

local btn3 = createButton(250, 420, 120, 40, "Button 3", "green", true, function(self)
    outputChatBox("Clicked Button 3")
end)

or

function createButton(config)
    local self = setmetatable({}, Button)

    self.x = config.x
    self.y = config.y
    self.width = config.width
    self.height = config.height
    self.text = config.text or "Button"
    self.color = config.color or "blue"
    self.fade = config.fade ~= false
    self.onClick = config.onClick or false -- // This always returns false, even if I added onClick function.
    self.alpha = self.fade and 0 or 255
    self.fade_step = 15
    self.visible = true

    self.currentColor = Theme.colors.button[self.color].background

    table.insert(activeButtons, self)

    return self
end

-- // Example.lua, example for using:
loadstring(exports.ui:injectModules())()

--[[The function name Button must match the actual constructor function, which is createButton. You should call it using createButton({ ... }) instead of Button({ ... }).]]

local btn = createButton({
    x = 20,
    y = 60,
    width = 100,
    height = 30,
    text = "Click",
    color = "blue",
    fade = true,

    onClick = function(self)
        outputChatBox("Clicked the button.") -- // It's not working.
    end
})

If everything is already defined correctly, then please share the code that handles the button click. The code you posted has no issues and works fine.

Link to comment
  • Moderators
19 hours ago, eksorz said:

No, like this:

You have to put it at the location where you want to make your trace. If you do not put it inside of another function, it will return "in main chunk", because it is tracing function calls starting from the main chunk.

For example:

function test1()
  test2() -- call test2
end

function test2()
  test3() -- call test3
end

function test3()
  test4() -- call test4
end

function test4()
  -- ENDS HERE
  print(debug.traceback())
end

test1() -- STARTS HERE

Will return:

stack traceback:
	Main.lua:14: in function 'test4'
	Main.lua:10: in function 'test3'
	Main.lua:6: in function 'test2'
	Main.lua:2: in function 'test1'
	Main.lua:17: in main chunk
	[C]: in ?

It will show all functions that have been called.

And for your current code, it might give you the function(s) where, your onClick got lost.

 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...