Help with functions and parameters

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Snuux
Prole
Posts: 49
Joined: Sun Dec 15, 2013 10:43 am
Location: Russia, Moskow
Contact:

Help with functions and parameters

Post 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?
My library for easy saving Slib! Try this! Now you can encrypt your save!
- Drop of light LD#30
- OUTRANGE LD#31
(Sorry for my english. I learn it myself, and I don't have enough experience)
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Help with functions and parameters

Post 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?
Help us help you: attach a .love.
User avatar
DaedalusYoung
Party member
Posts: 412
Joined: Sun Jul 14, 2013 8:04 pm

Re: Help with functions and parameters

Post 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)
User avatar
Snuux
Prole
Posts: 49
Joined: Sun Dec 15, 2013 10:43 am
Location: Russia, Moskow
Contact:

Re: Help with functions and parameters

Post 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(
My library for easy saving Slib! Try this! Now you can encrypt your save!
- Drop of light LD#30
- OUTRANGE LD#31
(Sorry for my english. I learn it myself, and I don't have enough experience)
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: Help with functions and parameters

Post 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.
Attachments
watch.love
Debugging tool.
(1.73 KiB) Downloaded 37 times
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Help with functions and parameters

Post 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.
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
User avatar
Snuux
Prole
Posts: 49
Joined: Sun Dec 15, 2013 10:43 am
Location: Russia, Moskow
Contact:

Re: Help with functions and parameters

Post 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.
My library for easy saving Slib! Try this! Now you can encrypt your save!
- Drop of light LD#30
- OUTRANGE LD#31
(Sorry for my english. I learn it myself, and I don't have enough experience)
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Help with functions and parameters

Post 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
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
User avatar
Snuux
Prole
Posts: 49
Joined: Sun Dec 15, 2013 10:43 am
Location: Russia, Moskow
Contact:

Re: Help with functions and parameters

Post by Snuux »

Second link is awesome. Thanks. I know, what metatables are. But I miss practice...
My library for easy saving Slib! Try this! Now you can encrypt your save!
- Drop of light LD#30
- OUTRANGE LD#31
(Sorry for my english. I learn it myself, and I don't have enough experience)
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 11 guests