Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: I have a minor issue with passing variables... SOS...  (Read 1153 times)

0 Members and 1 Guest are viewing this topic.

TerribleAtProgramming

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
I have a minor issue with passing variables... SOS...
« on: November 23, 2014, 11:43:05 pm »
Sorry If my code is a bit of a mess, I tried to cut out all the unimportant stuff, so there may be some bits missing... my issue is that I am trying to pass the Boolean "CorrectLetter1" into the Game class, if this Boolean is true then the Integer "GatesCleared" Should increase, there are no errors, until I play my game at which point it crashes and gives the error message "Object reference not set to an instance of an object" how do I fix this...

(I am using SFML in C# so my code will be a little different, but I can try and translate from C++)

here is some of my code...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Resources;
using SFML.Window;
using SFML.Graphics;
using System.Drawing;

namespace SFML_Template
{
    public class Game
    {
        Player player;
        public int GatesCleared = 0;
        public void Update(RenderWindow window, float delta)
        {
            this.delta = delta;
            player.Update(delta);
            {

                    if (GatesCleared == 0)
                    {
                        if (player.Position.X >= background.Position.X + 1045)
                        {
                            player.Position = new Vector2f(player.Position.X - 2, player.Position.Y);
                            View view = window.GetView();
                            view.Move(new Vector2f(-2, 0));
                            window.SetView(view);
                        }
                    }

                    if (GatesCleared == 1)
                    {
                        if (player.Position.X >= background.Position.X + 1045 & player.Position.Y >= player.Position.Y + 440)
                        {
                            player.Position = new Vector2f(player.Position.X - 2, player.Position.Y);
                            View view = window.GetView();
                            view.Move(new Vector2f(-2, 0));
                            window.SetView(view);
                        }
                    }

                    if (player.Position.X >= background.Position.X + 900 & player.Position.Y >= background.Position.Y + 100 & player.Position.Y <= background.Position.Y + 150 & GatesCleared == 0)
                    {
                        EncryptionActive = true;
                        if (Encryption.correctLetter1 = true)       //Error occurs here
                        {
                            GatesCleared += 1;
                        }
                    }
                    else EncryptionActive = false;
                    }
           }
}





using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Resources;
using SFML.Window;
using SFML.Graphics;
using System.Drawing;
using System.Timers;

namespace SFML_Template
{
    public class Encryption : Sprite
    {
        int letterCount = 1;
        Game game;
        public Boolean correctLetter1 = false;


        public void Update(RenderWindow windowEncypt, float delta)     
        {
            this.delta = delta;
                if (letterCount == 1)
                {
                    keyPress = false;
                    Letter1.Update(delta);
                    if (Keyboard.IsKeyPressed(Keyboard.Key.Q) || Keyboard.IsKeyPressed(Keyboard.Key.W) || Keyboard.IsKeyPressed(Keyboard.Key.E) || Keyboard.IsKeyPressed(Keyboard.Key.R) || Keyboard.IsKeyPressed(Keyboard.Key.T) || Keyboard.IsKeyPressed(Keyboard.Key.Y) || Keyboard.IsKeyPressed(Keyboard.Key.U) || Keyboard.IsKeyPressed(Keyboard.Key.I) || Keyboard.IsKeyPressed(Keyboard.Key.O) || Keyboard.IsKeyPressed(Keyboard.Key.A) || Keyboard.IsKeyPressed(Keyboard.Key.S) || Keyboard.IsKeyPressed(Keyboard.Key.D) || Keyboard.IsKeyPressed(Keyboard.Key.F) || Keyboard.IsKeyPressed(Keyboard.Key.G) || Keyboard.IsKeyPressed(Keyboard.Key.H) || Keyboard.IsKeyPressed(Keyboard.Key.J) || Keyboard.IsKeyPressed(Keyboard.Key.K) || Keyboard.IsKeyPressed(Keyboard.Key.L) || Keyboard.IsKeyPressed(Keyboard.Key.Z) || Keyboard.IsKeyPressed(Keyboard.Key.X) || Keyboard.IsKeyPressed(Keyboard.Key.C) || Keyboard.IsKeyPressed(Keyboard.Key.V) || Keyboard.IsKeyPressed(Keyboard.Key.B) || Keyboard.IsKeyPressed(Keyboard.Key.N) || Keyboard.IsKeyPressed(Keyboard.Key.M))
                    {
                        if (Keyboard.IsKeyPressed(Keyboard.Key.S))
                        {
                            correctLetter1 = true;
                        }
                     }
}
« Last Edit: November 24, 2014, 02:09:45 am by TerribleAtProgramming »

Ztormi

  • Jr. Member
  • **
  • Posts: 71
  • Web developer by day. Game developer by night.
    • View Profile
Re: I have a minor issue with passing variables... SOS...
« Reply #1 on: November 24, 2014, 09:58:53 am »
You really need to read some tuts or books about C#. This is pretty basic stuff.

 public class Game
    {
        public Game()
        {
            SomeClass s = new SomeClass();
            s.DoSomething();
            Console.WriteLine(s.Stuff);
        }
    }
    public class SomeClass
    {
        public int Stuff { get; set; }
        public SomeClass() { }

        public void DoSomething()
        {
            Stuff = 123;
        }
    }
Other way around
  public class Game
    {
        public int Stuff { get; set; }
        public Game()
        {
            SomeClass s = new SomeClass(this);
            s.DoSomething();
        }
    }
    public class SomeClass
    {
        private Game _game;
        public SomeClass(Game game)
        {
            _game = game;
        }
        public void DoSomething()
        {
            _game.Stuff = 123;
        }
    }
 
With static class (not recommended)
   
 public static class Game
    {
        public static int Stuff { get; set; }
    }
    public class SomeClass
    {
        public SomeClass() { }
        public void DoSomething()
        {
            Game.Stuff = 123;
        }
    }
With an event:
 
 public class ChangeStuffEventArgs : EventArgs
    {
        public int Stuff { get; set; }
        public ChangeStuffEventArgs() { }
        public ChangeStuffEventArgs(int stuff)
        {
            Stuff = stuff;
        }
    }
    public class Game
    {
        public Game()
        {
            SomeClass s = new SomeClass();
            s.OnStuffChanged += s_OnStuffChanged;
            s.DoSomething();
        }
        void s_OnStuffChanged(object sender, ChangeStuffEventArgs e)
        {
            Console.WriteLine(e.Stuff);
        }
    }
    public class SomeClass
    {
        public event EventHandler<ChangeStuffEventArgs> OnStuffChanged;
        private int _stuff;
        public int Stuff
        {
            get { return _stuff; }
            set
            {
                _stuff = value;

                if (OnStuffChanged != null)
                    OnStuffChanged(this, new ChangeStuffEventArgs(_stuff));
            }
        }

        public SomeClass() { }
        public void DoSomething()
        {
            Stuff = 123;
        }
    }

Choose your weapon.
PS. Making a new thread and new account after first one gets closed is not really a polite thing to do  :)
« Last Edit: November 24, 2014, 10:55:53 am by Ztormi »

 

anything