How can I create a sub that runs when a variable changes value(without having to check the variable all the time)? Kind of like Form_Load or something.
Printable View
How can I create a sub that runs when a variable changes value(without having to check the variable all the time)? Kind of like Form_Load or something.
You can't. You can though emulate it by using property get/let/set statements.
Although, if you are making a game, you should have a continuous loop where you can check it every time.
Z.
Put this in a class module called clsPlayer (it's just an example) :
To use it, just declare it like this where you would declare the variable:Code:
Private pHealth As String
Public Property Let Health(ByVal sData As String)
pHealth = sData
'Put here anything else you want to happen when
'you modify the player's health
End Property
Public Property Get Health() As String
Health = Health
'Put here anything else you want to happen when
'you get the player's health
End Property
Then, to access it, do it like this:Code:
Dim Player As New clsPlayer
Hope that clears everything ;)Code:
Player.Health = 100
Temp = Player.Health
Or you could go overboard, and have every Get/Let create an event... 0.o
Z.
Another approach would be to make a function to return the value of the var, and another to set it.
There, it should be simplier and faster than Property Get/Set, but you would have to use these functions instead of Something = MyVar or MyVar = Something...Code:
Dim MyVar As Long
Public Sub SetMyVar(ToWhat As Long)
MyVar = ToWhat
End Sub
Public Function GetMyVar() As Long
GetMyVar = MyVar
End Sub
Oh, and thanks for replying. I like people that say "Thanks dude that works you're great" :p