Dispatcher Timer Problem

Posted by will on Stack Overflow See other posts from Stack Overflow or by will
Published on 2010-05-24T05:18:07Z Indexed on 2010/05/24 5:21 UTC
Read the original article Hit count: 313

I am trying to make a game in silverlight that also has widgets in it. To do this I am using a dispatcher timer running a game loop that updates graphics etc. In this I have a variable that has to be accessed by both by the constantly running game loop and UI event code. At first look it seemed that the gameloop had its own local copy of currentUnit (the variable), despite the variable being declared globally. I am trying to update currentUnit with an event by the widget part of the app, but the timer's version of the variable is not being updated. What can I do get the currentUnit in the gameloop loop to be updated whenever I update currentUnit via a click event?

Here is the code for setting currentUnit as part of a click event

DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Unit));


                currentUnit = serializer.ReadObject(e.Result) as Unit;


                txtName.Text = currentUnit.name;
                Canvas.SetLeft(txtName, 100 - (int)Math.Ceiling(txtName.ActualWidth) / 2);
                txtX.Text = "" + currentUnit.x;
                txtY.Text = "" + currentUnit.y;

                txtX.Text = "" + currentUnit.owner;
                txtY.Text = "" + currentUnit.moved;
                txtName.Text = "" + currentUnit.GetHashCode();

And here is a snippet from the gameLoop loop

//deal with phase changes and showing stuff
            if (txtPhase.Text == "Move" && movementPanel.Visibility == Visibility.Collapsed)
            {
                if (currentUnit != null)
                {
                    if (currentUnit.owner)
                    {
                        if (currentUnit.moved)
                        {
                            txtMoved.Text = "This Unit has Already Moved!";
                            movementPanel.Visibility = Visibility.Collapsed;
                        }
                        else
                        {
                            txtMoved.Text = "" + currentUnit.GetHashCode();
                            movementPanel.Visibility = Visibility.Visible;
                        }
                    }
                    else
                    {
                        txtMoved.Text = "bam";
                        movementPanel.Visibility = Visibility.Collapsed;
                    }
                }
                else
                {
                    txtMoved.Text = "slam";
                    movementPanel.Visibility = Visibility.Collapsed;
                }
                //loadUnitList();
            }

Here is the code for my unit class.

using System;

public class Unit
{
    public int id { get; set; }
    public string name { get; set; }
    public string image { get; set; }
    public int x { get; set; }
    public int y { get; set; }
    public bool owner { get; set; }
    public int rotation { get; set; }
    public double movement { get; set; }
    public string type { get; set; }
    public bool moved { get; set; }
    public bool fired { get; set; }
}

Overall, any simple types, like a double is being 'updated' correctly, yet a complex of my own type (Unit) seems to be holding a local copy.

Please help, I've asked other places and no one has had an answer for me!

© Stack Overflow or respective owner

Related posts about Silverlight

Related posts about variables