In my script attached to my controllers I want to be able to reference the child object that the controller is holding at the time, but I’m not sure how.
Any idea how to do this, would I need to tag the objects or something along those lines?
One way to do it, which is something I've done before at least, is to use System events.
You make two events in your controllers:
event EventHandler OnPickedUp;
event EventHandler OnLetGo;
If you manage to get something within range of picking it up, you fire off the event OnPickekUp
public class MyVRController
{
public event EventHandler OnPickedup;
public event EventHandler OnLetGo;
private bool HasObject = false;
...
private void SuccessfullyPickedUp(GameObject pickedUpGO)
{
if(OnPickedUp != null)
{
HasObject = true;
OnPickedUp(pickedUpGO, null);
}
}
...
private void OnLetGo()
{
if(OnLetGo != null)
{
HasObject = false;
OnLetGo(this, null);
}
}
...
}
Then whatever needs to care about the fact that you picked something up or you dropped something, can do this:
public class MyGameManager
{
public void Start()
{
// However you reference the controllers, do it here.
myRightVRController.OnPickedUp += SomeFunc1;
myRightVRcontroller.OnLetGo += SomeFunc2;
myLeftVRController.OnPickedUp += SomeFunc3;
myLeftVRController.OnLetGo += SomeFunc4;
// The rest of your initialization...
}
}
If you want you can specify what controller the event came from in the EventArgs that can be passed (currently passing null).
Hope this helps!
Related
Issue
I have created an event in the class LoginVM which looks like the following:
public class LoginVM : INotifyPropertyChanged
{
public event EventHandler<string> PasswordSet;
}
Also in this class I have a piece of code which fires this event:
public class LoginVM : INotifyPropertyChanged
{
public event EventHandler<string> PasswordSet;
private void PopulateLatestServer()
{
try
{
string SERVER_ID = Registry.GetValue(#"HKEY_CURRENT_USER\SOFTWARE\PODIA", "LATESTSERVER", null).ToString();
BDO_SERVERS latestserver = SavedServers.Where(a => a.Server_ID == SERVER_ID).FirstOrDefault();
setServerURL(latestserver.ServerURL, false);
Username = latestserver.Username;
PasswordSet(this, latestserver.Password);
}
catch (Exception)
{
Global.WriteLog("Could not find last logged in server.", EventLogEntryType.Warning);
}
}
}
I have another class which is called LoginV and in there I create an instance of the class and subscribe to the event:
public partial class LoginV : MetroWindow
{
public LoginV()
{
InitializeComponent();
LoginVM _loginVM = new LoginVM();
this.DataContext = _loginVM;
_loginVM.PasswordSet += new EventHandler<string> (_loginVM_PasswordSet);
}
private void _loginVM_PasswordSet(object sender, string e)
{
passwordBox.Password = e;
}
As you can probably tell I am trying to trigger an event from the ViewModel to the View but every time I trigger the event from the ViewModel, PasswordSet is null and errors.
An event is null when there's no listener to the event.
private void RaisePasswordSet(String pass) {
YourEventArgs args = new YourEventArgs(pass);
if(PasswordSet != null) PasswordSet(this, args);
}
Your issue is that when you try to raise the event no one listen to it yet.
It's a good idea to initialize the password in the constructor for LoginVM as you did. That's when initialization ought to happen. Ordinarily, you'd set a property and the binding in the XAML would take care of updating the control. No need for an event on the VM. But this is a password box, so you can't bind it, and the event you wrote is The Right Thing.
But in your implementation, that leaves you with this sequence of events:
Create VM
VM raises PasswordSet in its constructor -- without checking to see if there are any handlers.
View assigns VM to DataContext
View adds handler to PasswordSet event
And you get an exception at step 2, because you didn't check for handlers.
Here's what you do.
In the VM or anywhere, always use this pattern for raising events:
C# <= 5:
protected void OnPasswordSet(String e)
{
var handler = PasswordSet;
if (handler != null)
{
handler(this, e);
}
}
C#6
protected void OnPasswordSet(String e) => PasswordSet?.Invoke(this, e);
Either:
private void PopulateLatestServer()
{
try
{
string SERVER_ID = Registry.GetValue(#"HKEY_CURRENT_USER\SOFTWARE\PODIA", "LATESTSERVER", null).ToString();
BDO_SERVERS latestserver = SavedServers.Where(a => a.Server_ID == SERVER_ID).FirstOrDefault();
setServerURL(latestserver.ServerURL, false);
Username = latestserver.Username;
OnPasswordSet(latestserver.Password);
}
catch (Exception)
{
Global.WriteLog("Could not find last logged in server.", EventLogEntryType.Warning);
}
}
Can't crash now. Or at least not the same way as last time.
Problem number two: How do you update the view initially?
Easy: Take whatever's in the view's PasswordSet handler, move it into a protected method, and call that in both places. This looks a little verbose since it's only a one-liner, but it's nice to have things rolled into neatly labeled units. If that code were more complicated, you'd absolutely want not to be copying and pasting it. If it gets more complicated a year from now, you won't have to waste any time re-parsing your the old code.
public partial class LoginV : MetroWindow
{
public LoginV()
{
InitializeComponent();
LoginVM _loginVM = new LoginVM();
this.DataContext = _loginVM;
_loginVM.PasswordSet += new EventHandler<string> (_loginVM_PasswordSet);
UpdatePassword();
}
protected void UpdatePassword()
{
passwordBox.Password = e;
}
private void _loginVM_PasswordSet(object sender, string e)
{
UpdatePassword();
}
Option number two: Keep OnPasswordSet() as shown above, but instead of having the view manually update the password in the constructor, have the LoginVM require a PasswordSet handler as a parameter. This isn't the way I would do it; constructor parameters like this get on my nerves. But that may just be an irrational prejudice on my part. This way makes more clear the fact that the owner needs to handle that event to use the class, and "provide a suitable event handler" becomes the only thing the consumer needs to do in order to use the thing. The less a consumer needs to know about your class's internals, the better, for obvious reasons. Platonically ideal design would be when programmers who don't think at all can make casual glib assumptions about your class, and not end up on Stack Overflow begging somebody to read the documentation to them out loud. We'll never get there, though.
public class LoginVM : INotifyPropertyChanged
{
public LoginVM(EventHandler<string> passwordSetHandler)
{
if (passwordSetHandler != null)
{
PasswordSet += passwordSetHandler;
}
PopulateLatestServer();
}
// If the consumer doesn't want to handle it right way, don't force the issue.
public LoginVM()
{
PopulateLatestServer();
}
A third option is to set up explicit add/removes for the event, and raise the event when the handler comes in:
public class LoginVM : INotifyPropertyChanged
{
private event EventHandler<string> _passwordSet;
public event EventHandler<string> PasswordSet
{
add
{
_passwordSet += value;
// ...or else save latestServer in a private field, so here you can call
// OnPasswordSet(_latestServer.Password) -- but since it's a password,
// best not to keep it hanging around.
PopulateLatestServer();
}
remove { _passwordSet -= value; }
}
Ok guys I'm totally new to stackoverflow, let me know if I stuff something up.
I am making a class library for sockets and then using the class library to make a server. The issue I've run into is this:
In the class library I have a class called sockets that has event handlers (you know like connection made, closed so on) and in the server I'm making, I have a class named player which inherits from sockets.
The problem pretty much comes down to this.
I use the OnConnectionMade event handler to create my player, but because I cant do instanceOfInheritedClass = intanceOfBaseClass, even when I make use of use of the event handlers, the event handlers wont trigger because instanceOfInheritedClass isn't pointing to intanceOfBaseClass event, if I pass through the parameters.
void hostManager_OnConnectionMade(object source, ConnectionArgs e)
{
Player socket = new Player(e.GetSocket());
socket.OnDataRecivedPostConvert += Socket_OnDataRecivedPostConvertLogin;
}
public Player(DDSocket socket)
{
this.Host = socket.Host;
this.Socket = socket.Socket;
//this.OnConnectionClosed += socket.OnConnectionClosed;
//this.OnDataRecivedPostConvert += socket.OnDataRecivedPostConvert;
//this.OnDataRecivedPostConvertHost += socket.OnDataRecivedPostConvertHost;
}
One of the solutions I came up with was that instead of inheriting, I can just make it a intanceOfBaseClass parameter in the player class. But that will prevent me from making proper use of object source from my event handlers which will mean I'll need to use linq or something to find the player from the socket or something like that.
The other thing I thought about doing was somehow passing the event handlers over, which you can see I tried, but don't know how to do.
Now after hours of looking it up I'm stuck. Any help is greatly appreciated
and any answer that solves this issue is fine. I'm not picky with how its solved.
Despite that your Player class is inherited from DDSocket, but in this scenario, it acts as the wrapper class of DBSocket, so there is one hack to achieve that, I think you have to do some further steps:
class DDSocket
{
public event Action OnConnectionClosed;
public void Raise()
{
if (OnConnectionClosed != null)
{
OnConnectionClosed();
}
}
}
class Player :DDSocket
{
// make new event look as the same base class
public new event Action OnConnectionClosed;
public Player(DDSocket socket)
{
socket.OnConnectionClosed += Socket_OnConnectionClosed;
}
private void Socket_OnConnectionClosed()
{
if (OnConnectionClosed != null)
{
OnConnectionClosed();
}
}
}
// test those 2 classes
static void Main()
{
DDSocket d = new DDSocket();
Player pl = new Player(d);
pl.OnConnectionClosed += () => MessageBox.Show("test");
d.Raise();
}
So I have a class that is basically a manager for 20+ copies of another class. What is the best way of handling the same event fired from each one of them? And is this the best way of unregistering the events? Or should I be using a single EventHandler somehow?
I put together a simple example that basically does what I'm doing in my actual project.
class Manager
{
List<Child> children = new List<Child>();
public Manager()
{
for (int i = 0; i < 10; i++)
{
Childchild = new Child();
child.Done += child_Done;
items.Add(child);
child.DoStuff();
}
}
public void RemoveAll()
{
foreach (Child child in items)
{
child.Done -= child_Done;
}
items.Clear();
}
void child_Done(string sometext)
{
Console.WriteLine("child done: " + sometext);
}
}
class Child
{
public delegate void _Done(string sometext);
public event _Done Done;
public Child()
{
}
public void DoStuff()
{
if (Done != null) { Done("finally!"); }
}
}
The unregistration should be fine - as long as the target instance and method match it will work. I will, however, strongly advise you use a more regular event pattern, with a sender - then you will know which child is talking. For example:
public class MessageEventArgs : EventArgs {
public MessageEventArgs(string message) {
this.message = message;
}
private readonly string message;
public string Message { get { return message; } }
}
and an:
public event EventHandler<MessageEventArgs> Done;
protected virtual void OnDone(string message) {
var handler = Done;
if(handler != null) handler(this, new MessageEventArgs(message));
}
Assuming you want the It might be better to add the Manager object to respond to each child when the child object raises the event:
It might be better to register for the event when adding a child and unregister when removing a child.
A quick way to do that would be to switch from List to ObservableCollection. This collection will raise an event as soon as the collection changes.
So in the constructor of the Manager, instantiate the ObservableCollection and register for the CollectionChanged event. In the handler check the event argument to see what children have been added, and removed so the manager can register for (or unregister) their event(s).
This is more or less the way.
A few suggestions to make it work a little better:
Create a function that adds a Child, and at the same way registers the event.
The same can be done with removing child + unregistering the event.
Also, you can make the event get a "sender" parameter, and make the Child pass "this" to it.
This way the event handler will be able to know which child is done, and remove it from the list, and/or whatever else needs to be done.
This seems to be basics of the language, but I do not understand how is this accomplished in .Net. I have a member variable in a class, say a bool _isCommitted. I want something to happen whenever _isCommitted is true. Something like this:
//Whenever _isCommitted == true()
{
Foo()
}
Basically like an event, but here it is my variable. How to? Many thanks..
This is normally done through properties and a backing private field. You need to ensure you only ever access through the property.
private bool _isCommitted;
public bool IsCommitted
{
get { return _isCommitted; }
set
{
if(value)
{
//do something
}
_isCommitted = value;
}
}
At the most basic level, you can create an event in your class:
public delegate void MyHandler(bool b);
public event MyHandler CommittedChanged;
Now people can subscribe to your event like so:
public void SomeHandlerMethod(bool b) { ... }
...
someInstance.CommittedChanged += SomeHandlerMethod;
someInstance.CommittedChanged += ASecondHandlerMethod;
someInstance.CommittedChanged += x => { /* inline handler using lambda */ };
A user can unregister his event handler this way:
someInstance.CommittedChanged -= SomeHandlerMethod;
And wherever you decide to change your variable, you will follow it up with:
if (CommittedChanged != null) CommittedChanged(_isCommitted);
This will call everyone who has registered a function with your event.
Having said this, there are plenty of improvements that you can do. First, make _isCommitted into a property, and do the event callback in its setter. This way, you won't forget to call the handlers.
public IsCommitted {
get { return _isCommitted; }
set {
_isCommitted = value;
if (CommittedChanged != null) CommittedChanged(_isCommitted);
}
}
Read more about events here.
This is enough to get you going. However, if you delve further into the C# framework, you will find a standardized way of using this event framework inside of the System.ComponentModel namespace. Sepcifically, the interface INotifyPropertyChanged, which ties neatly into a more generic event system that also plays well with some of Microsoft's own technologies, such as WPF, allowing GUI elements to pick up on changes to your class automatically. Read more about INotifyPropertyChanged here.
You basically need PropertyChangedEvent PropertyChangedEventHandler Delegate
I think C# properties is what you need.
private bool _isCommitted;
public bool IsCommitted
{
get { return _isCommitted; }
set { if(value){/*DO SOMETHING HERE*/}
_isCommitted = value; }
}
I have some C# code that updates some properties of an object. I have an event handler defined to help me respond when the update process is done. Unfortunately, I've learned that this event is getting fired multiple times. I suspect this is happening because the event handler is being set at the wrong time. Currently, it is being set like the following:
myObject.Update_Succeeded += new EventHandler(myObject_Update_Succeeded);
Due to the complexity of the code, I'm having a difficult time of tracking down where it should be set. I would like to only set the event handler it hasn't been previously set. Because of this, I want to do something like this:
ClearEventHandlers(myObject);
or
myObject.Update_Succeeded = null;
myObject.Update_Succeeded += new EventHandler(myObject_Update_Succeeded);
Is there a way to accomplish what I'm trying?
Thank you!
Yes, you can customize the add/remove accessors of your event. This article describes these accessors. But you can do something like:
class MyClass
{
private EventHandler _myEvent;
public event EventHandler MyEvent
{
[MethodImpl(MethodImplOptions.Synchronized)]
add
{
_myEvent = (EventHandler)Delegate.Combine(_myEvent, value);
}
[MethodImpl(MethodImplOptions.Synchronized)]
remove
{
_myEvent = (EventHandler)Delegate.Remove(_myEvent, value);
}
}
public void ClearMyEvent() {
_myEvent = null;
}
...
}
You should be able to remove a handler using the subtract operator like below
myObject.Update_Succeeded -= new EventHandler(myObject_Update_Succeeded);
Or check this out for a way to remove all event handler if you are in doubt
How to remove all event handlers from a control
Proper way should be to detach the handler from each event after you no longer use it:
public class MyObjectListener
{
private readonly MyObject _object;
public class MyObjectListener(MyObject obj)
{
_object = obj;
Attach();
}
// adds event handlers
private void Attach()
{
obj.UpdateSucceeded += UpdateSuceededHandler;
obj.UpdateFailed += UpdateFailedHandler;
}
// removes event handlers
private void Detach()
{
obj.UpdateSucceeded -= UpdateSuceededHandler;
obj.UpdateFailed -= UpdateFailedHandler;
}
...
}
The only thing you need to decide is where to call the Detach method. For example, you can call it in the handler itself:
private void UpdateSuceededHandler(object sender, Data args)
{
Detach();
// do something when it succeeds
}
private void UpdateFailedHandler(object sender, Data args)
{
Detach();
// do something when it fails
}
Or, you could allow users of MyObjectListener to tell it that it no longer needs to listen to the attached object:
public void StopListening()
{
Detach();
}
An object which raises an event should not allow its listeners to modify the event invocation list. Each event listener should subscribe or unsubscribe its own event handlers only.
You better set event handler on the initialization of your object i.e. in your Constructor.