Display final score from windows 8 game c# - c#

I created a game in c# for windows 8 and everything works like it is supposed to but after the user plays the game the score is displayed in a text box and it is supposed to also display on the following screen displaying the score and text and thumbs up images depending on how high it is.
The problem is the score isn't being carried over. The code I use gets the constructor from the game page then the score, the code is below:
Var arcade = new arcadeMode();
ScoreNum.text = arcade.score.toString();
//then sample code to display images based on score
if(arcade.score < 100)
{
Usermessage.text = "try again.";
Thumbdown.visibility = visibility.visible;
}
I think my problem is actually getting the saved score from the game because it must only be getting the textbox value from before the game started. Not really sure how to save the score without a database though. Any suggestions on how to go about this?

It looks like ArcadeMode is essentially your data model, but gets the constructor from the game page doesn't really have meaning. Assuming you have an instance of ArcadeMode on your game page and it includes the actual score, with the code above, you've just created a brand new second instance of that same class with the score initialized to 0.
One quick way, since it appears it's just a simple piece of data you want to pass, is use the second parameter of Navigate. Wherever it is you navigate to the thumbs-up page, add the current value of your 'score' variable as the second parameter, for example:
Frame.Navigate(typeof(ThumbsUpPage), score)
Then in the OnNavigatedTo event of ThumbsUpPage you can access the value you passed via the Parameter property of the argument, for example:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
String score = e.Parameter;
...
Another option is to give the ArcadeMode class instance a larger scope (i.e., not make it a variable on the main page). You could make it a member of the App class, for instance, and then it would be available for all the pages in your app.

Related

Unity Keep Selected Color on after press even if other buttons are pressed

I know this is a very long question, but I tried my best to be as clear as possible with this weird problem. Any help or suggestions would be appreciated. Let me know if I can provide any more resources.
The Setup
I am trying to create a sort-of navigation system where the user can still see what path they are on. I have three sets of buttons:
Year Buttons - these are three static buttons that are always on the screen (year 1, 2 and 3).
Discipline Buttons - these are dynamically instantiated and destroyed based on which year button has been selected.
Module Buttons - these function the same as the discipline buttons, but take into account the year AND discipline button when instantiating and destroying.
How It Should Work
When the user clicks on a button, the button must change color and stay that color until a different button of THE SAME SET (mentioned above) is pressed.
Example
User presses "YEAR 1" button. Button changes from orange to green. Discipline instances are instantiated based on year = 1.
User then presses one of the instantiated DISCIPLINE buttons. This button changes from orange to green. The YEAR button should still stay green. Module instances are instantiated based on year=1 and discipline text.
User presses "YEAR 2" button. module and discipline instances are destroyed, and YEAR 1 button must change back to original color. YEAR 2 button must change to green and discipline instances are instantiated.
My Code
I have three lists: one for buttons, one for disciplines, one for modules. The button list is static, while the other two are destroyed and added to as needed (the list adding/clearing does work).
When I instantiate disciplines or modules, the previous ones are all destroyed (and the list is cleared) and then each instance is added to the list. Each instance has an onClick listener (made in the script, it does not work to make the listener Editor-side) which calls this general method:
public void ChangeAllWhite(List<GameObject> list)
{
foreach (var button in list)
{
button.GetComponent<Image>().color = orangeColor;
}
}
and then calls one of these three methods to change that instance color:
public void YearColorChange(GameObject buttonToChange)
{
ChangeAllWhite(yearButtons);
buttonToChange.GetComponent().color = selectedColor;
}
public void DisciplineColorChange(GameObject buttonToChange)
{
ChangeAllWhite(disciplineButtons);
buttonToChange.GetComponent<Image>().color = selectedColor;
}
public void ModuleColorChange(GameObject buttonToChange)
{
ChangeAllWhite(moduleButtons);
buttonToChange.GetComponent<Image>().color = selectedColor;
}
This is the code to instantiate the disciplines (same as module pretty much) after the previous ones have been destroyed:
foreach (string item in disciplines)
{
GameObject disciplineInstance = Instantiate(DisciplineItem);
disciplineInstance.transform.SetParent(DisciplineLayoutGroup.transform, false);
disciplineInstance.GetComponentInChildren<Text>().text = item;
disciplineButtons.Add(disciplineInstance);
disciplineInstance.GetComponent<Button>().onClick.AddListener(() =>
{
UnityEngine.Debug.Log("debug discipline");
AppManager.instance.classroomInfo.SetDiscipline(item);
StartCoroutine(AppManager.instance.web.GetModules(AppManager.instance.classroomInfo.year, item));
DisciplineColorChange(disciplineInstance);
//needs year, discipline
});
}
What currently happens
I don't get any errors, but the colors don't change. When I called the methods from an OnClick on the editor-side, it breaks and/or doesn't work. I think what is happening is that OnClick methods from previously instantiated (and now deleted) instances are trying to do something and then it just doesn't do anything. Any suggestions are welcome!
Your code seems incredibly over-complicated.
Why not use the various button-states-colors that are built in to Button ?
Alternately, you must build a class that has the colors (animations, images, fonts, whatever) you want as a property and add that component to your buttons and use that. (In an ECS system, "adding a component" like that is a bit like extending a class in normal OO programming; you're just adding more behavior to the Button.)
So you would have your own cool
`SugarfreeButton`
and then you can do stuff like
sugarfreeButton.Look = YourLooks.Possible;
sugarfreeButton.Look = YourLooks.CannotChoose;
sugarfreeButton.Look = YourLooks.Neon;
etc. You really HAVE to do this, you can't be dicking with setting colors etc in your higher-level code.
It's only a few lines of code to achieve this.
I've attached a completely random example of such "behaviors you might add to a button" at the bottom of this - DippyButton
When you do "panels of buttons" it is almost a certainty that you use a layout group (HorizontalLayoutGroup or the vertical one). Do everything flawlessly in storyboard, and then just drop them inside that in the code with absolutley no positioning etc. concerns in the code.
Unfortunately, u won't be able to do anything in Unity UI until extremely familiar with the two layout groups (and indeed the subtle issues of LayoutElement, the fitters, etc.).
You should almost certainly be using prefabs for the various button types. All of your code inside the "foreach" (where you add listeners and so on) is really not good / extremely not good. :) All of that should be "inside" that style of button. So you will have a prefab for your "discipline buttons" thing, with of course class(es) as part of that prefab which "do everything". Everything in Unity should be agent based - each "thing" should take care of itself and have its own intelligence. Here's a typical such prefab:
So these "lobby heads" (whatever the hell that is!) entirely take care of themselves.
In your example, when one is instantiated, on its own, it would find out what the currently selected "year" is. Does it make sense? They should look in the discipline database (or whatever) and decide on their own what their value should be (perhaps based on their position (easy - sibling index) in the layout group, or whatever is relevant.
You will never be able to engineer and debug your code the way you are doing it, since it is "upside down". In unity all game objects / components should "look after themselves".
You should surely be using a toggle group as part of the solution, as user #derHugo says. In general, anything #derHugo says, I do, and you should also :)
Random code examples from point (1) ... DippyButton
using UnityEngine;
using UnityEngine.UI;
// simply make a button go away for awhile after being clicked
public class DippyButton : MonoBehaviour
{
Button b;
CanvasGroup fader;
void Start()
{
b = GetComponent<Button>();
if (b == null)
{
Destroy(this);
return;
}
b.onClick.AddListener(Dip);
fader = GetComponent<CanvasGroup>();
// generally in Unity "if there's something you'll want to fade,
// naturally you add a CanvasGroup"
}
public void Dip()
{
b.interactable = false;
if (fader != null) { fader.alpha = 0.5f;
}
Invoke("_undip", 5f);
}
public void _undip()
{
b.interactable = true;
if (fader != null) { fader.alpha = 1f; }
}
}

Enum Value Set in Editor Not Changing in Script

I am working on a project that requires around 30 buttons in a UI menu. Each of these buttons has a "Loader" script attached to it, and the overall scene has a GameManager object (which persists through scenes).
The GameManager holds an enum with a number of named states equal to the number of buttons.
The idea is I need to populate the next scene with specific content depending on the button pressed, so I thought to assign each of these buttons an enum value, and when clicked the Loader script will change the GameManager's enum to equal the button's value. This way I can avoid making 30+ scenes and instead change the single scene based on the state. When the next scene is loaded, it should determine what the current state is, and does stuff based on that.
The problem is, even though I am able to manually assign the enum value of each individual button in the editor, that doesn't seem to actually do anything. Here are my scripts (abbreviated for only the relevant methods), and sample output when clicking a button.
Here is my GameManager:
public class GameManager : MonoBehaviour {
//Used so MenuContent scene knows what random content to populate with
public enum MenuChosen
{
Tabs, Formal, Casual, Headwear, Accessories, Speakers, ComputerParts,
GameConsoles, MobilePhones, Furniture, Lighting, OfficeSupplies, Gadgets, Toys,
Summer, SchoolSupplies, ArtsCrafts, Checking, Savings, PayPal, OtherAccount, Visa,
MasterCard, AirMiles, OtherCards, Food, Retail, Digital, OtherCoupons,
PayTransaction, ScheduledPayments, LoanPayment, MobileRecharge
};
public MenuChosen chosen;
}
And here is my Loader class. It is attached to every button. In the editor, I selected four buttons, and manually changed their states to "Formal", "Casual", "Headwear", and "Accessories", via the drop-down list in the inspector for each of them.
The Loader class creates a reference to the GameManager object, and attempts to change its "chosen" enum variable to be equal to the value that was set in the inspector window (which is called "MenuOption" in the loader).
public class Loader : MonoBehaviour {
public GameManager gm;
public GameManager.MenuChosen MenuOption;
private void Start()
{
gm = GameObject.FindGameObjectWithTag("GameManager").GetComponent<GameManager>();
Debug.Log("Current Loader State: " + MenuOption);
}
public void LoadMenuContent()
{
Debug.Log("Button's State is: " + MenuOption);
gm.chosen = MenuOption;
SceneManager.LoadScene("MenuContent");
Debug.Log("Current state is: " + gm.chosen);
}
}
When I first run the game, the debug messages show that the buttons' enum values are indeed changed:
Current Loader State: Accessories
Current Loader State: Headwear
Current Loader State: Formal
Current Loader State: Casual
However, the logs printed in the LoadMenuContent() method show something different:
Button's State is: Tabs
Current state is: Tabs
So even though it shows that the buttons' MenuOption is properly changed, once it reaches LoadMenuContent(), which is called when the button is pressed, then somehow the state has changed back to the default state, "Tabs".
Any thoughts on why the enum might be changing?
I discovered what my issue was.
Due to the large quantity of buttons, I had resorted to copying and pasting them.
This meant that each button was identical, and had its own copy of the Loader script as a component. By doing this, I was able to change the individual values of the enum of each individual button in the inspector.
However, this also meant that each and every button, in the OnClick() method, were all still holding a reference to the original button that I had copied and pasted. So these buttons were independent, but they all referenced the same exact script from the same exact button, who's state had never been changed from the default.
In order to fix this, I had to go in and manually drag each button into its own OnClick() method as a reference to itself, so they were properly calling the correct function from their own individual Loader script.

Loading more than one buttons in the same location or not

I am writing a UI interface with user input and buttons. I need to ask the user to input some number and then press the button labelled "solve", which needs to determine whether the number is correct. I try to use only one button to solve it in the beginning. Here is my code:
if (correct)
{
solveButton = new MenuButton(contentManager.Load<Texture2D>("solve"),
buttonCenter, GameState.AnswerNo);
}
else
{
solveButton = new MenuButton(contentManager.Load<Texture2D>("solve"),
buttonCenter, GameState.AnswerYes);
}
When loading it, under the draw method, I use the following code:
solveButton.Draw(spriteBatch);
Here, GameState.AnswerNo and GameState.AnswerYes lead to different pages standing for the correct and incorrect answers, respectively. However, it doesn't work as I thought it would - it always goes to GameState.AnswerNo page no matter the "correct" value is.
Therefore, I am thinking about using two buttons (button figures are the same but move position a little):
solveButton = new MenuButton(contentManager.Load<Texture2D>("solve"),
buttonCenter, GameState.AnswerNo);
buttonCenter.Y -= 40;
solveButton2 = new MenuButton(contentManager.Load<Texture2D>("solve"),
buttonCenter, GameState.AnswerYes);
And when loading them:
if (correct)
{
solveButton.Draw(spriteBatch);
}
if (!correct)
{
solveButton2.Draw(spriteBatch);
}
Which works fine, but the awkward thing is that when the user inputs the correct number, button1 will disappear and button2 will appear just under it. Any better ideas to complete this function?
I think you are looking at the problem wrong.
Instead of using two different buttons, why are you not just using one button, and then set the state via a function?
User inputs value, and presses button.
Button calls function, checks if value is correct.
Based on result, set the active state.

databinding in c# acting oddly

I have a question regarding my code:
I have a list of player "private List player = new List();" and Player is a class that I mad myself, I also created an Usercontrol and all variable of player are databinded.
And it's working perfectly fine in my main form, but I have a strange issue when I change form.
I give the player list as a parameter to another form and here is the code:
public Result(List<Player> player, string format)
{
InitializeComponent();
_player = player;
ExtentionHelpers.Shuffle<Player>(_player);
}
Shuffle as the name said, just shuffle the list, but it should shuffle _player which is a local variable of my second form, I never use player in this form.
But for some strange reason, my list player from my main form end up being shuffled too, and I don't want that, specially when that mess up my databinding.
What's happening here ??
Your player variable is a reference type, so it is the same data you are working on in both forms. You'll need to clone your player list if you want to manipulate it separately.
When calling your constructor, try this:
var frm = new Result(new List<Player>(player), "some format");
Creating a new list means that you can manipulate the new list independently from the first BUT if you change data within the Player type, it will affect data from the other form (you'll need to clone each item separately before adding to a new list if you want this).

Accessing dynamically created textboxes text

I have stumbled across a problem with my asp.net form.
Within my form the end user chooses a number of textboxes to be dynamically created, this all works fine with the following code:
protected void txtAmountSubmit_Click(object sender, EventArgs e)
{
int amountOfTasks;
int.TryParse(txtAmountOfTasks.Text, out amountOfTasks);
for (int i = 0; i < amountOfTasks; i++)
{
TextBox txtAddItem = new TextBox();
txtAddItem.ID = "txtAddItem" + i;
txtAddItem.TextMode = TextBoxMode.MultiLine;
questionNine.Controls.Add(txtAddItem);
txtList.Add(txtAddItem.ID);
}
}
However this has also caused a small problem for me, later on in my form on the submit button click, I send the results to the specified person it needs to go to (using smtp email). Again this part is fine, until I am trying to retrieve the text from these dynamically created textboxes.
What I have tried
I have tried using this msdn access server controls ID method however this was not working.
I tried to add these new textboxes to a list, however I was unsure on how to update these textboxes when they have text in them. Therefore my results were returning null because of this.
I have also looked at other questions on SO such as this however they are usually for WPF or winforms, rather than my problem with asp.net (this usually isn't an issue, but I don't need to get the text from every textbox control in my page, just the ones that were dynamically created).
I have also tried changing how I call the code that I hoped would have worked:
string textboxesText = string.Join("\n", txtList.Select(x => x).ToArray());
and then in my concatenated string (email body) I would call:
textboxesText
The problem
As they are dynamically created I am finding it difficult to call them by their id for example: txtExampleID.Text, also as I have to increment the ID's by one each time (so they don't override each other) it has made things a little bit more difficult for me.
I am not asking for a code solution, I would prefer pointers in the right direction as I am still learning.
So to sum it all up: I need to get the text from my dynamically created textboxes to add it to my email body.
The issue is these text boxes need recreated in the Load event of the page, every single time, so that both events and values can be hooked back up and retrieved.
I think the most straight forward approach, in your case, would be to extend idea #1 that you had already tried. Build a List of these controls with enough information to recreate them in Load, but you need to store that List in either ViewState or Session.
ViewState["DynamicControls"] = list;
or
Session["DynamicControls"] = list;
I would use ViewState if you can because it gets destroyed when the user leaves the page.

Categories

Resources