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
So do you guys know of any way to monitor variables ? (That doesn't require me to recompile löve)
Monitoring variable changes
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- Ranguna259
- Party member
- Posts: 911
- Joined: Tue Jun 18, 2013 10:58 pm
- Location: I'm right next to you
- ejmr
- Party member
- Posts: 302
- Joined: Fri Jun 01, 2012 7:45 am
- Location: South Carolina, U.S.A.
- Contact:
Re: Monitoring variable changes
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.
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.
- Ranguna259
- Party member
- Posts: 911
- Joined: Tue Jun 18, 2013 10:58 pm
- Location: I'm right next to you
Re: Monitoring variable changes
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
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
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?
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?
- Ranguna259
- Party member
- Posts: 911
- Joined: Tue Jun 18, 2013 10:58 pm
- Location: I'm right next to you
Re: Monitoring variable changes
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
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
Thanks for all the replys everyone
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
Thanks for all the replys everyone
- BlackBulletIV
- Inner party member
- Posts: 1261
- Joined: Wed Dec 29, 2010 8:19 pm
- Location: Queensland, Australia
- Contact:
Re: Monitoring variable changes
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.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
- Ranguna259
- Party member
- Posts: 911
- Joined: Tue Jun 18, 2013 10:58 pm
- Location: I'm right next to you
Re: Monitoring variable changes
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 wasBlackBulletIV wrote: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.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
- BlackBulletIV
- Inner party member
- Posts: 1261
- Joined: Wed Dec 29, 2010 8:19 pm
- Location: Queensland, Australia
- Contact:
Re: Monitoring variable changes
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.
But I understand that sometimes print statements don't provide all the info you need. It can't hurt though.
Who is online
Users browsing this forum: Google [Bot] and 74 guests