Monitoring variable changes

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
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Monitoring variable changes

Post by Ranguna259 » Thu Aug 29, 2013 12:19 am

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)
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
ejmr
Party member
Posts: 302
Joined: Fri Jun 01, 2012 7:45 am
Location: South Carolina, U.S.A.
Contact:

Re: Monitoring variable changes

Post by ejmr » Thu Aug 29, 2013 2:41 am

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.
ejmr :: Programming and Game-Dev Blog, GitHub
南無妙法蓮華經

User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Monitoring variable changes

Post by Ranguna259 » Thu Aug 29, 2013 1:15 pm

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
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
raidho36
Party member
Posts: 1952
Joined: Mon Jun 17, 2013 12:00 pm

Re: Monitoring variable changes

Post by raidho36 » Thu Aug 29, 2013 1:19 pm

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.

User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: Monitoring variable changes

Post by Plu » Thu Aug 29, 2013 1:21 pm

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?

User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Monitoring variable changes

Post by Ranguna259 » Thu Aug 29, 2013 2:26 pm

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
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
BlackBulletIV
Inner party member
Posts: 1260
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Monitoring variable changes

Post by BlackBulletIV » Thu Aug 29, 2013 9:45 pm

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.

User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Monitoring variable changes

Post by Ranguna259 » Fri Aug 30, 2013 8:40 pm

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
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
BlackBulletIV
Inner party member
Posts: 1260
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Monitoring variable changes

Post by BlackBulletIV » Fri Aug 30, 2013 9:48 pm

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.

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests