Thursday, January 29, 2015

Having trouble calling variables

Right now I am trying to create an rpg c# word game with windows forms to get familiar with the language. I have been stuck on this issue for a couple days now and cannot seem to get it working. I have a character creation form, which gets all the information (name, gender, skill points etc.). I have a reference class called "Player" which gets and sets all of the player variables and sets them to 0. I also have a second form which is going to be my main form that is bigger, will hold equipment, stats and other features, but when I have a test on form2, trying to set a label to a variable we took from form1, it returns 0. I have tried everything that I know and cannot seem to find the answer. I will attach all of the code that I have now.


Form 1 is KylesRpgGame


Form 2 is HomeScreen


Player entity is Player



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Engine;

namespace KylesRpgGame
{

public partial class KylesRpgGame : Form
{
Int32 remainingPoints { get; set; }
public Player player1;


public KylesRpgGame()
{
InitializeComponent();
//Variables
remainingPoints = 15;
player1 = new Player();
//
//Attributes -> Lables
lblStrengthPoints.Text = player1.Strength.ToString();
lblAgilityPoints.Text = player1.Agility.ToString();
lblIntelligencePoints.Text = player1.Intelligence.ToString();
lblStaminaPoints.Text = player1.Stamina.ToString();
lblPointsRemaining.Text = remainingPoints.ToString();
//
}
private void btnStrInc_Click(object sender, EventArgs e)
{
if (remainingPoints > 0)
{
player1.Strength++;
remainingPoints--;
lblStrengthPoints.Text = player1.Strength.ToString();
lblPointsRemaining.Text = remainingPoints.ToString();
}
}

private void btnStrDec_Click(object sender, EventArgs e)
{
if (player1.Strength > 0)
{
player1.Strength--;
remainingPoints++;
lblStrengthPoints.Text = player1.Strength.ToString();
lblPointsRemaining.Text = remainingPoints.ToString();
}
}

private void btnAgiInc_Click(object sender, EventArgs e)
{
if (remainingPoints > 0)
{
player1.Agility++;
remainingPoints--;
lblAgilityPoints.Text = player1.Agility.ToString();
lblPointsRemaining.Text = remainingPoints.ToString();
}
}

private void btnAgiDec_Click(object sender, EventArgs e)
{
if (player1.Agility > 0)
{
player1.Agility--;
remainingPoints++;
lblAgilityPoints.Text = player1.Agility.ToString();
lblPointsRemaining.Text = remainingPoints.ToString();
}
}

private void btnIntInc_Click(object sender, EventArgs e)
{
if (remainingPoints > 0)
{
player1.Intelligence++;
remainingPoints--;
lblIntelligencePoints.Text = player1.Intelligence.ToString();
lblPointsRemaining.Text = remainingPoints.ToString();
}
}

private void btnIntDec_Click(object sender, EventArgs e)
{
if (player1.Intelligence > 0)
{
player1.Intelligence--;
remainingPoints++;
lblIntelligencePoints.Text = player1.Intelligence.ToString();
lblPointsRemaining.Text = remainingPoints.ToString();
}
}

private void btnStaInc_Click(object sender, EventArgs e)
{
if (remainingPoints > 0)
{
player1.Stamina++;
remainingPoints--;
lblStaminaPoints.Text = player1.Stamina.ToString();
lblPointsRemaining.Text = remainingPoints.ToString();
}

}

private void btnStaDec_Click(object sender, EventArgs e)
{
if (player1.Stamina > 0)
{
player1.Stamina--;
remainingPoints++;
lblStaminaPoints.Text = player1.Stamina.ToString();
lblPointsRemaining.Text = remainingPoints.ToString();
}
}
private void btnTest_Click(object sender, EventArgs e)
{
//player.var = textbox
player1.CharacterName = txtCharacterName.Text;
//Get Gender
if (rdoMale.Checked)
player1.Gender = "Male";
else
player1.Gender = "Female";
//Class
player1.Class = cboClass.Text;

//If name is blank or begin with spaces, return
if(String.IsNullOrEmpty(txtCharacterName.Text) || txtCharacterName.Text[0] ==' ')
{
MessageBox.Show("You must insert a correct name. Names cannot begin with spaces.");
return;
}
//if male or female is checked, return. If not
if(rdoMale.Checked == false && rdoFemale.Checked == false)
{
MessageBox.Show("You must select a gender.");
return;
}

//Get class
if (cboClass.Text != "Warrior")
{
if (cboClass.Text != "Hunter")
{
if (cboClass.Text != "Priest")
{
MessageBox.Show("Choose a class.");
return;
}
}
}
//Make them use all points
if (remainingPoints > 0)
{
MessageBox.Show("You must use all your points.");
return;
}
//Comfirmation Message Box
MessageBox.Show("Your name is " + player1.CharacterName + "\n" + "Your Gender is "
+ player1.Gender + "." + "\n" + "Your Strength is " + player1.Strength + "\n"
+ "Your Agility is " + player1.Agility + "\n" + "Your Intelligence is "
+ player1.Intelligence + "\n" + "Your Stamina is " + player1.Stamina);




HomeScreen next = new HomeScreen();
next.Show();
this.Hide();
}
}
}


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Engine;

namespace KylesRpgGame
{
public partial class HomeScreen : KylesRpgGame
{
public HomeScreen()
:base()
{
InitializeComponent();
MessageBox.Show(player1.Strength.ToString());
}
}
}




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Engine
{
public class Player
{
public string CharacterName { get; set; }
public Int32 Strength { get; set; }
public Int32 Agility { get; set; }
public Int32 Intelligence { get; set; }
public Int32 Stamina { get; set; }
public Int32 StrengthModifier { get; set; }
public Int32 AgilityModifier { get; set; }
public Int32 IntelligenceModifier { get; set; }
public Int32 StaminaModifier { get; set; }
public string Gender { get; set; }
public string Class { get; set; }


public Player()
{
Strength = 0;
Agility = 0;
Intelligence = 0;
Stamina = 0;
StrengthModifier = 0;
AgilityModifier = 0;
IntelligenceModifier = 0;
StaminaModifier = 0;
CharacterName = "";
Gender = "";
Class = "";

Strength = Strength + StrengthModifier;
Agility = Agility + AgilityModifier;
Intelligence = Intelligence + IntelligenceModifier;
Stamina = Stamina + StaminaModifier;

}
}
}




No comments:

Post a Comment