• 11
• 9
• 10
• 9
• 11

# Seperate GUi from GameLogic

This topic is 527 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello guys,

im in trouble right now, because i stuck on a ( maybe ) a simple problem?!

So i have a GuiClass.cs with in this class i create my Button/Textbox, all the GUI stuff.

So i trigger the button/textbox with a event like in WinForms. But i need to exchange Data tgrough different Classes without a reference.

For case2 i create Singleton Class with a Dictionary<string, Action>, now in the OhterClass i can register a event like:

EventManager.Instance.AddAction("test", () => a = 3);


and in the GuiClass i can trigger this event like:

button.OnClick += () => { EventManager.Instance.TriggerEvent("test"); };


so i solved case2.

But now when i change  the variable in the OtherClass, how can i  transfer the new value back to the GUI-Class? I can create a second event in the EventManager, but is there a better way to do this? - case 1

And in general, is my approach a goad Idea? How do you solve to seperate GUI/Gamescreen,...

I dont use WPF/Winform i wrote my own Textbox/Button rendered with Direct3D.

hope you can help me :(

Edited by montify

##### Share on other sites

Wrap the variable in a struct or class and just pass the reference around - simple as that.

##### Share on other sites

You simply run in the spaghetti trap. GUI should never hold nor change data on its own rather than simply trigger stuff and fire events that is what MVC is on so you should change your general design

##### Share on other sites

Usually there will be a single 'UpdateGUI' event - whenever you change a value, fire the UpdateGUI event, and the GUI receives that and knows it needs to read the latest values from the game and update them in the display. It's typically okay for the whole GUI to update in those circumstances, even though only 1 thing has changed, because the update is a very simple operation. (This ceases to be true with complex listviews and the like - you might want something a bit more targeted for that. You could add hints into the event regarding what needs to update, or have UI elements 'subscribe' to specific data sources. But worry about that later...)

##### Share on other sites

I created my own Databinding class (similar to WPF) and now i can bind two variables with reflection, OneWay and TwoWay.

##### Share on other sites

I think that is a good approach, if you're happy with your game data being somewhat coupled to the UI code. I don't like the data binding approach because it means you start needing to implement things like INotifyPropertyChanged or using ObservableCollection<T> containers, etc. But it can be more efficient that way.

##### Share on other sites

I don't know if this is the right way to do Binding, but:

i have 1 Interface IBinding.cs:

  public interface  IBinding
{
event Action OnChanged;
}


Here is my DataBinding.cs

    public enum BindinType
{
OneWay,
TwoWay
}

public class DataBinding
{

public DataBinding(TextBox source, IBinding desination, string property, BindinType bindinType)
{

var myType = desination.GetType();
var p = myType.GetProperty(property);

if (bindinType == BindinType.OneWay)
{
source.OnChanged += () =>
{
string tmp = source.text;
p.SetValue(desination, float.Parse(tmp), null);
};
}

if (bindinType == BindinType.TwoWay)
{
source.OnChanged += () =>
{
var tmp = float.Parse(source.text);
p.SetValue(desination, tmp, null);
};

desination.OnChanged += () =>
{
source.text = p.GetValue(desination, null).ToString();
};
}
}

}


now i can do like this:

new DataBinding(textBox, gameScreen, "a property value", BindinType.TwoWay);


As a said, i dont know if this is common or not, i tried it by myself to "recreate" Microsofts Binding in a easy way...