[HELP] Variable monitoring from outside class

4 posts

Flag Post


I am attempting to make a simple class that I use to debug some of my

code at runtime. I use a function in my debug class to pass a variable
from another class that I want to monitor in realtime.

For simplicity sake I am only passing Stings at the moment. I can pass
the varible that I want successfully but I am stumpted as to how I to get my
DebugClass to monitor that variable in realtime.

Since the variable is being altered in another class, what would I need

to do to have the debugClass monitor that variable without having to call
a new function everytime the varible changes, which could be 300+ times a

This is the current method I use to add the variable to my displayList:

protected function addDebugData(stringToAdd:String):void{
	var debugText1:TextField = new TextField();
	debugText1.border = true;
	debugText1.width = 300;
	debugText1.text = stringToAdd;
	debugText1.y = 20;

The problem with this method is that it is a one time display method.
The current value passed is displayed, but that is it. My Class has
no knowledge of any changes made to the parent variable.

I have a method to update my panel that displays the variable(s) I have
Passed, but those variable are still whatever I initialy passed via my

I am likely missing something simple in scoping, constructing or in retuning
something. Fairly new to this all. So any thoughts, tips, alternatives and/or
solutions very welcome!

Flag Post

You cannot pass a data type variable (numbers or string) by reference in AS3, thus what you are trying is impossible.

I think you would have to approach this from a different angle entirely. What about having class A but also making a clone debug class A which has getters/setters for all properties and debug code at the start and end of every method. When you wanna debug your project you swap between A and the debug version of A.

At least, that’s the only way I can think of producing the sort of thing you want here.

Edit: Alternatively you can box every single data type with a wrapper class, thus allowing you to access by reference, but this would have a terrible performance impact and be bothersome to code. To do that for Int you’d basically make a class called WrapInt which only has a single “value” property to your Int value, and everytime you want to access the actual Int value you type myint.value instead of myint.

Flag Post

I was not aware that I could not pass data type variable by reference in AS3. I assume since you mentioned those specifically then I could pass object type variables by reference? Like TextField? If so that would suite my needs just as well.

If I were to use an object, the textField for example, is there then a way to have that variable monitored in realtime from another class if I pass a reference to the the class that is monitoring it?

P.S. I appreciate the help.

Flag Post

Yes, object typed variables work by reference, including TextField, but you don’t have to use the native classes in Flash, making your own for your own specific purposes would be better. (don’t use an entire TextField to store a string, make your own class that stores a string).

As for monitoring in real time, no, not really. These are the ways you could try to do it at best:
1. use a timer that checks the values periodically (but it’s entirely possible the values got changed several times before the timer had the chance to react)
2. use getters and setters on the original object so it informs the debug class when a change has happened (requires you to mess with the regular class in order to debug it)
3. Use inheritance to somehow melt the debug class and the target class into one, allowing for your code to debug itself in each class (might not suit what you are trying to do at all)