Page 1 of 1

Help with functions and parameters

Posted: Mon Apr 14, 2014 12:33 pm
by Snuux
Hi, I want to ask some question.
It's possible to receive variable name, when we put one parametor into function call?

Some example:

Code: Select all

function getNameAndValue(a)
  --it's pseudo code
  name = a --where name is "key"
  value = a --and value is 50
end

key = 50
getNameAndValue(key)
It is possible to do with one parameter?

Re: Help with functions and parameters

Posted: Mon Apr 14, 2014 12:38 pm
by Robin
I guess you could use the [manual]debug[/manual] library somehow, but it's probably the wrong thing to do. Why do you want the function getNameAndValue?

Re: Help with functions and parameters

Posted: Mon Apr 14, 2014 12:47 pm
by DaedalusYoung
You can do it if it's a table, I think:

Code: Select all

function getKeyAndValue(t)
    local key, value
    for k, v in pairs(t) do
        key = k
        value = v
        break
    end
    return key, value
end

myTable = { key = 50 }
key, value = getKeyAndValue(myTable)

Re: Help with functions and parameters

Posted: Mon Apr 14, 2014 12:58 pm
by Snuux
Thanks all. I want write small function for debug, and I want, that function recieve only one parameter...
So, now I understand, that likely to fail - use one parameter((

In Zoetrope it's really simple((

Code: Select all

debugger.watch('the.block.x')
debugger.watch('the.block.y')
But it's archeticture very powerfull. And without this - it's impossible(

Re: Help with functions and parameters

Posted: Mon Apr 14, 2014 2:35 pm
by Ref
Probably missing your question but had an occasion where I wanted to monitor the values of some variables.
So, I wrote the following:
To use - add the following:
m = require 'monitor' -( to top of script )
m.watchBox() -( to love.draw() )
m.watch['something descriptive'] = variable_of_interest
Example:
m.watch['draw x'] = X
m.watch.y = Y
If the function in which the watched variable is accessed, the window is increased in size and the value displayed.
(In the demo provided, try clicking the mouse.)
For what's it worth.

Re: Help with functions and parameters

Posted: Mon Apr 14, 2014 3:45 pm
by Ranguna259
To use debugger.watch() you need to know the actual name of the variable and from what I understood you wanted to know its name with the variable itself.
So by providing the variable

Code: Select all

a
you'd get a return value of

Code: Select all

'a'
and its value, well that is impossible in lua, to monitor variables then all you need is a simple metatable to warn you everytime the variable is being accesed but I think that only works in table type variables.
Metatables are powerful tools in lua, you should totaly look into them.

Re: Help with functions and parameters

Posted: Mon Apr 14, 2014 4:03 pm
by Snuux
Ref, I will watch monitor example.
Ranguna259, thanks, I thought about it. And today I look into your debug. It's awesome.
Thanks for all!

Metatables.. Metatables.. Please give me some good links about this please.

Re: Help with functions and parameters

Posted: Mon Apr 14, 2014 4:13 pm
by Ranguna259
LoveDebug still has some bugs when love.graphics.translate and scale are used but it should be safe, I'm also getting it ready for android debugging.
Metatables: the link that I've included in my last post..., list of metamethods, more exemples

Re: Help with functions and parameters

Posted: Mon Apr 14, 2014 4:18 pm
by Snuux
Second link is awesome. Thanks. I know, what metatables are. But I miss practice...