Page 1 of 1

Monitoring variable changes

Posted: Thu Aug 29, 2013 12:19 am
by Ranguna259
Is there any way to monitor variable changes in löve, Ex: I'd say to monitor a variable and then if it ever changes I'd get warned (by a print() or anything else) that that variable was changed in line X

I found this on the web but I don't think it works with löve and if it does I don't know how to set it up :roll:

So do you guys know of any way to monitor variables ? (That doesn't require me to recompile löve)

Re: Monitoring variable changes

Posted: Thu Aug 29, 2013 2:41 am
by ejmr
You can use a custom function for the __newindex metamethod to track access to a table, and since Lua uses a table for its global environment you can change its metamethods. These sections of "Programming in Lua" will give you some ideas:

http://www.lua.org/pil/13.4.2.html
http://www.lua.org/pil/13.4.4.html
http://www.lua.org/pil/14.html

Or you could restrict the variables you want to track to a separate environment instead of manipulating Lua's default. There are different approaches to tracking variables so it depends on the fidelity you want. The links above show one way to do it, and reading about those metamethods and evironments in general will shed light on alternatives.

Re: Monitoring variable changes

Posted: Thu Aug 29, 2013 1:15 pm
by Ranguna259
Thanks for the answer but that only works for tables, I've tried to modify it to work with variables and it didn't work, I'm not tracking a table I'm trying to track a variable

Re: Monitoring variable changes

Posted: Thu Aug 29, 2013 1:19 pm
by raidho36
You can try defining a variable as global and check it via _G["variable"]; for locals, there's pretty much no way you can track them from elsewhere.

Re: Monitoring variable changes

Posted: Thu Aug 29, 2013 1:21 pm
by Plu
You can try to track a single variable by applying the above methods to the table _G, which contains everything in the global scope. But that's probably not a good idea.

It would be better to simply put the variable you want to track in a table. Is there a reason why it needs to be a single variable?

Re: Monitoring variable changes

Posted: Thu Aug 29, 2013 2:26 pm
by Ranguna259
The reason I want to do this is because a variable was exiting a function with the wrong value and I just wanted to know if the value was wrongly set inside the function or if it was changed outside and if so where, but I found where the variable was changing so problem fixed, but I'll come back here on my free time to see if I can code a script for this, it can be really helpfull at some times but I guess it'll take quite some time because I have little to none experience on meta-stuff :P

So, problem fixed and try to come up with a script for this if you can (in your free time of course) and share it with the rest of the community on Projects and Demos :3

Thanks for all the replys everyone

Re: Monitoring variable changes

Posted: Thu Aug 29, 2013 9:45 pm
by BlackBulletIV
Ranguna259 wrote:The reason I want to do this is because a variable was exiting a function with the wrong value and I just wanted to know if the value was wrongly set inside the function or if it was changed outside and if so where, but I found where the variable was changing so problem fixed
Sorry if this sounds obvious, but you can use the print function at multiple points to output the variable's value to the console. That's usually what I do to find the problem.

Re: Monitoring variable changes

Posted: Fri Aug 30, 2013 8:40 pm
by Ranguna259
BlackBulletIV wrote:
Ranguna259 wrote:The reason I want to do this is because a variable was exiting a function with the wrong value and I just wanted to know if the value was wrongly set inside the function or if it was changed outside and if so where, but I found where the variable was changing so problem fixed
Sorry if this sounds obvious, but you can use the print function at multiple points to output the variable's value to the console. That's usually what I do to find the problem.
Thing is the code is big and doing that would make it even bigger and messy, and since it goes up and down along itself it'd be pretty hard to find where the variable change was

Re: Monitoring variable changes

Posted: Fri Aug 30, 2013 9:48 pm
by BlackBulletIV
I don't think you should worry about some print calls making the code bigger and messier. It certainly wouldn't increase the size of your code as much as monitoring variables. And of course you would remove them after you've found the bug.

But I understand that sometimes print statements don't provide all the info you need. It can't hurt though.