Creating a second runloop inside XNA - c#

I'm working on porting a game engine from Java to Windows Phone 7 XNA. One thing I'm struggling with is how to create modal dialogs. The dialog is rendered in XNA using SpriteBatch just like everything else, but what I basically want is something like this:
result = Dialog.Ask("Some Question?", DialogButtons.YesNo);
Where Dialog.Ask doesn't return until the user clicks one of the buttons. The only thing I've done to come close is a method to continuously call RunOneFrame() on the game:
private int runLoopCount;
public void BeginRunLoop() {
int runIndex = ++runLoopCount;
while (runLoopCount == runIndex) {
RunOneFrame();
Thread.Sleep(1);
}
}
public void EndRunLoop() {
--runLoopCount;
}
There are a few problems with this:
RunOneFrame is only supposedly for debugging purposes.
Input doesn't work! Calling TouchPanel.GetState() or GamePad.GetState(PlayerIndex.One) doesn't return new values.
Is there any way to initiate a run loop without throwing away the Game class and all it does for initialization? And I don't really know how to do without the Game class anyway, as there is no Main() method in Windows Phone 7 XNA applications. It just goes right into the Game constructor.

What you want to do is to add another 'state', you just have to show dialog until user do not select something... and _modalDialogIsUp bool..
protected override void Update(GameTime gameTime)
{
if (_modalDialogIsUp)
{
// handle only secesary mouse and button clicks
}
else
{
// normal mouse and button clicks
}
}
protected override void Draw(GameTime gameTime)
{
if (_modalDialogIsUp)
{
// draw only modal dialog
}
else
{
// draw game
}
}
I just can't undestang why you so desparatly want exactly modal behavior... is it some kind of new "sync" sect? :)

Related

Is this an alright method for buttons?

I'm practicing with buttons in Unity and I'm trying to figure out how I can assign different methods to a button without using the OnClick thing in the inspector so I came up with this.
public class UIButtons : MonoBehaviour
{
void Start()
{
var buttons = FindObjectsOfType<Button>(); //I should probably have this outside of this method
foreach (Button button in buttons)
{
button.onClick.AddListener(() => ButtonPressed(button));
}
}
void ButtonPressed(Button button)
{
switch (button.name)
{
case "MMPlayButton": //The main menu play button
Debug.Log("Play button pressed");
break;
case "PlayButton":
Debug.Log("Play button pressed");
break;
case "SettingsButton":
Debug.Log("Settings button pressed");
break;
case "QuitButton":
Debug.Log("Quit button pressed");
break;
default:
Debug.LogWarning("Button doesn't have a case: " + button.name);
//could do some kind of pop up message saying the button pressed isn't available or something
break;
}
}
}
I know this can work, however, I'd imagine this is a horrible way to do things because it's using the name so if I were to change the name or make a typo it breaks, or an issue I did encounter was if buttons have the same name, and if I add more buttons I'd have to add more cases and then it would probably turn out to be a giant mess.
I could be wrong though and maybe this is an alright idea, but I doubt it so looking for some help with this.
You can simplify this by using inheritance. Create a base class for all of your buttons to use, ie. UIButtonBase that has the OnButtonPressed virtual method. Then create specific buttons that inherit this base class and override the OnButtonPressed method.
Base Class
public class UIButtonBase: MonoBehaviour
{
protected virtual void Awake()
{
var button = GetComponent<Button>();
if (button)
{
button.onClick.AddListener(() => OnButtonPressed());
}
else
{
Debug.Log(name + " does not have a Button component!");
}
}
protected virtual void OnButtonPressed()
{
Debug.Log("UIButtonBase::OnButtonPressed()");
}
}
Specific implementation of a button
public class SettingsButton : UIButtonBase
{
protected override void OnButtonPressed()
{
Debug.Log("SettingsButton::OnButtonPressed()");
// If you want the base functionality as well, add..
//
base.OnButtonPressed();
}
}
For setup, create a button and add this script to it. You may need to change the GetComponent in Awake (UIButtonBase) to GetComponentInParent or GetComponentInChildren, depending on your gameobjects hierarchy. You could also expose the Button component in the UIButtonBase script and use that reference.
I know this won't be a good answer but if I were you, I would make different files for different buttons. Like SettingButton.cs, PlayButton.cs etc.
This approach will not add a huge amount of burden to your code but as the functionalities of your buttons increase, this approach will greatly help to keep your code organized.

Xamarin iOS app freezes: ViewDidAppear() never gets called

This seems to be a tricky one:
We're working on a Xamarin iOS app that has a UITableView control displaying a list of profiles. Once the user clicks on one of the profiles we'd like to switch to a separate "ProfileViewController" to display more detailed information. This works just fine for 99.9% of the time. In the remaining ~0.1% the item in the list gets highlighted (which happens OnClick) but the app just seemingly freezes and never switches to the other ViewController, leaving the user with an unresponsive list.
The interesting thing to note here is that the app doesn't really freeze, as the simple "swipe back" gesture brings the user back to the UITableView (to be clear the switch-to-profile-viewcontroller animation is never played so it basically switches from the unresponsive list back to the same list, but now it is responsive again).
The tricky thing is that we can't reliably reproduce this bug. It just seems to happen at random, sometimes while stuff is running in the background, other times while the app was previously in an idle state. We are pretty sure that it isn't related to multithreading (we triple-checked everything, using locking and semaphores where necessary), it rather seems to be some rendering issue (as you can still swipe back to the previous screen or at least it isn't your common dead lock).
Using a bunch of Console.WriteLine() tracers and hours of trial and error reproducing this bug we could isolate the problem and discovered the following:
Upon clicking on the list code in ViewDidLoad() and ViewWillAppear() is successfully executed. However ViewDidAppear() never gets invoked. There is nothing else happening in our code or on different threads between ViewWillAppear() and ViewDidAppear() and we don't have any funky / unusual code that gets executed previously (just the usual UI initialization like this in ViewDidLoad()):
...
LblProfileName.TextAlignment = UITextAlignment.Center;
LblProfileName.Text = string.Empty;
LblProfileName.Font = FontAgent.ForSize(30);
LblProfileSlogan.TextAlignment = UITextAlignment.Center;
LblProfileSlogan.Text = string.Empty;
LblProfileSlogan.Font = FontAgent.ForSize(20);
LblProfileRelationInfo.TextAlignment = UITextAlignment.Center;
LblProfileRelationInfo.Text = string.Empty;
LblProfileRelationInfo.Font = FontAgent.ForSize(15);
...
At this point we are kinda out of ideas as to what could be going wrong here and we have independently reviewed any of our code that could remotely be involved in this bug but we found nothing.
We didn't find anything related online but maybe someone else has encountered a similar issue like this in Xamarin / Xamarin iOS before?
Are there any steps we could take that we don't know of? When breaking the app in the Visual Studio for Mac debugger during the freezes the call stacks only contain native code and are not of much use.
Any help or ideas as to what else we could try are hugely appreciated :)
Edit: Adding some code
This is our BaseViewController defining some initialization methods to be used by it's children (again in 99.9% of all cases this works just fine):
public abstract class BaseViewController : UIViewController
{
public BaseViewController(string nibName, NSBundle nSBundle) : base(nibName, nSBundle)
{
ControllerDidInitialize();
}
public ClubmappViewController(IntPtr handle) : base(handle)
{
ControllerDidInitialize();
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
CustomOrientationService.ScreenRotationPortrait();
InitializeStaticContent();
Console.WriteLine("ViewDidLoad() exits just fine...");
}
public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);
Console.WriteLine("When the bug occurs this never gets executed :C");
RefreshUI();
OnViewDidAppear?.Invoke(this, new EventArgs());
LoadData();
}
private protected abstract void InitializeStaticContent();
private protected abstract void RefreshUI();
private protected virtual void LoadData()
{
}
private protected virtual void ControllerDidInitialize()
{
}
}
This is the ProfileViewController that inherits from BaseViewController and that just stops being rendered or inititialized or whatever the actual problem might be (it just never shows up as described above) but most of the time it works just fine:
public partial class ProfileViewController : BaseViewController
{
public const string STORYBOARD_ID = "ProfileViewController";
public ProfileViewController(IntPtr handle) : base(handle)
{
}
private protected override void ControllerDidInitialize()
{
// do initialization things like initializing variables, etc
// no real logic here
}
private protected override void InitializeStaticContent()
{
// layouting loading
LblProfileTitle.TextAlignment = UITextAlignment.Center;
LblProfileTitle.Text = string.Empty;
LblProfileTitle.Font = FontAgent.ForSize(20);
LblProfileTitle.Font = UIFont.BoldSystemFontOfSize(20);
LblProfileName.TextAlignment = UITextAlignment.Center;
LblProfileName.Text = string.Empty;
LblProfileName.Font = FontAgent.ForSize(30);
LblProfileSlogan.TextAlignment = UITextAlignment.Center;
LblProfileSlogan.Text = string.Empty;
LblProfileSlogan.Font = FontAgent.ForSize(20);
// ... and so on ..
}
private protected override void RefreshUI()
{
// ... theme related stuff ...
ViewProfileActive.BackgroundColor = ThemeAgent.CurrentTheme.OnlineIndicatorColor;
LblProfileName.TextColor = ThemeAgent.CurrentTheme.PrimaryTextColor;
LblProfileSlogan.TextColor = ThemeAgent.CurrentTheme.SecondaryTextColor;
// ...
}
private protected async override void LoadData()
{
// load user data ...
ProfileData data = await ...
}
}
And this is the "OnClick" event that is triggered when a profile is clicked on, that's supposed to initialize and show the ProfileViewController (it's unlikely that something's wrong with this but including it nonetheless):
// ...
(sender, e) =>
{
ProfileViewController profileController = (ProfileViewController)UIStoryboard.FromName("Main", null).InstantiateViewController(ProfileViewController.STORYBOARD_ID);
profileController.ModalPresentationStyle = UIModalPresentationStyle.FullScreen;
currentViewController.NavigationController.PushViewController(profileController, true);
}
// ...

Perform operation when a player enters the room

I want to create a multiplayer application using Photon (Pun2). After a player joins the room, I want to hide some buttons and display others for all the players in that room. At the moment I have this function:
public override void OnPlayerEnteredRoom(Player newPlayer)
{
print(newPlayer); // #02
if (PhotonNetwork.CurrentRoom.PlayerCount == 2 && PhotonNetwork.IsMasterClient)
{
print(PhotonNetwork.CurrentRoom.PlayerCount + "/2 Starting game...");
searchingBtn.SetActive(false); // hide this button
findMatchBtn.SetActive(false); // hide this button
scanningBtn.SetActive(true); // display this button
}
}
But this won't work, because only for 1 of the 2 players the buttons will change, for the other one they will stay the same.
I saw some examples on the Internet, where in this function OnPlayerEnteredRoom it's called PhotonNetwork.LoadLevel(scene_index), but at that moment, I don't want to load another scene, I want to stay in the current scene and just hide/display some buttons. Is this even possible?
EDIT: Here is the final version
public override void OnJoinedRoom()
{
Debug.Log("You joined the room.");
Debug.Log(PhotonNetwork.CurrentRoom.PlayerCount);
Debug.Log(PhotonNetwork.IsMasterClient);
UpdateButtons();
}
public override void OnPlayerEnteredRoom(Player newPlayer)
{
Debug.Log("Other players joined the room.");
if (PhotonNetwork.CurrentRoom.PlayerCount > 1 && PhotonNetwork.IsMasterClient)
{
Debug.Log(PhotonNetwork.CurrentRoom.PlayerCount + "/2 Starting game...");
UpdateButtons();
}
}
private void UpdateButtons()
{
searchingBtn.SetActive(false); // hide this button
findMatchBtn.SetActive(false); // hide this button
scanningBtn.SetActive(true); // display this button
}
From Photon Documentation:
virtual void OnJoinedRoom ()
//Called when the LoadBalancingClient entered a room, no matter if this client
//created it or simply joined.
virtual void OnPlayerEnteredRoom (Player newPlayer)
//Called when a remote player entered the room. This Player is already added to
//the playerlist.
With this knowledge, OnPlayerEnteredRoom is only called when a remote player joins the room. So in order to solve your problem, you'll need to call your code in the OnJoinedRoom callback for the local client and OnPlayerEnteredRoom when a remote player joins.
In summary, your new code should look like this:
public override void OnPlayerEnteredRoom(Player newPlayer)
{
UpdateButtons(newPlayer);
}
public override void OnJoinedRoom(Player player)
{
UpdateButtons(player);
}
private void UpdateButtons(Player player)
{
if (PhotonNetwork.CurrentRoom.PlayerCount > 1 && PhotonNetwork.IsMasterClient)
{
print(PhotonNetwork.CurrentRoom.PlayerCount + "/2 Starting game...");
searchingBtn.SetActive(false); // hide this button
findMatchBtn.SetActive(false); // hide this button
scanningBtn.SetActive(true); // display this button
}
}

Unity - C# - NullReferenceException: Object reference not set to an instance of an object [duplicate]

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 6 years ago.
EDIT
My question is different (I think..) because the last image allows me to set a GameObject to the Communication Message and Communication Message. BUT, on play, it immediately resets to None (Text) and None (Button). I don't know why this happens or how to solve this!
I am aware this question is widely covered but still I cannot solve this. I hope someone has a solution for me.
What I have
I am working with Behavior Designer, an asset for Unity. I'm making Behavior Trees and the problem occurred when using a behavior tree reference.
First, I had my Behavior Tree like this:
It searches the front door. If it is found, it will move towards it and communicate with the human playing the game.
This worked before, but now I put this behavior tree in a Behavior Tree Reference, like this:
When I double click the reference, the first image shows. BUT, where it goes wrong, is this:
Problem
This is the human instructions node in the first picture. It doesn't load the communication message and communication button. When I load the corresponding button, it immediately resets on playing the scenario. This happened when I loaded this behavior in the Behavior Tree Reference, and the problem didn't occur when I played this from the original tree self.
NullReferenceException: Object reference not set to an instance of an object
HumanInstructions.CommunicationElements (Boolean activeOrNot) (at Assets/HumanInstructions.cs:54)
HumanInstructions.OnAwake () (at Assets/HumanInstructions.cs:19)
BehaviorDesigner.Runtime.BehaviorManager.EnableBehavior (BehaviorDesigner.Runtime.Behavior behavior)
BehaviorDesigner.Runtime.Behavior.EnableBehavior ()
BehaviorDesigner.Runtime.Behavior.Start ()
Any ideas what could cause this and how to solve this?
The code from HumanInstructions
using UnityEngine;
using UnityEngine.UI;
using BehaviorDesigner.Runtime;
using BehaviorDesigner.Runtime.Tasks;
using UnityStandardAssets.Characters.FirstPerson;
public class HumanInstructions : Action
{
public string humanInstructionsText; // The string in inspector that shows up on-screen for the human operator to communicate with UGV.
public string messageToConsole; // Print this message to console when the action is performed SUCCESSFUL.
public string textOnButton; // See inspector in Behavior Designer. Is used as text on button.
public Text CommunicationMessage;
public Button CommunicationButton;
bool boolClicked; // Is false; when the button is clicked, it will turn TRUE, calling the IF function, returning Taskstatus SUCCESS.
public override void OnAwake ()
{
CommunicationElements (false);
}
public override void OnStart ()
{
boolClicked = false;
CommunicationElements (false);
CommunicationButton.onClick.AddListener (ButtonClicked);
}
public override TaskStatus OnUpdate ()
{
CommunicationMessage.text = humanInstructionsText;
CommunicationButton.GetComponentInChildren<Text> ().text = textOnButton;
CommunicationElements (true); // The communication elements are VISIBLE on screen.
TriggerStatusCameraView (false);
if (boolClicked == true) { // this IF function is active when the button is clicked. See ButtonClicked () function.
CommunicationElements (false); // Removes the communication elements from screen.
TriggerStatusCameraView (true);
return TaskStatus.Success;
} else {
return TaskStatus.Running;
}
}
// The following function is called when the CommunicationButton is clicked on screen.
void ButtonClicked ()
{
boolClicked = true; // Changes the value to true, so it will trigger the IF function in OnUpdate ();
Debug.Log (messageToConsole); // Sends this message to the console.
}
// The following function can be called upon and turn all UI elements (such as text, buttons or images) ACTIVE or not.
void CommunicationElements (bool activeOrNot)
{
CommunicationButton.gameObject.SetActive (activeOrNot);
CommunicationMessage.gameObject.SetActive (activeOrNot);
}
// The following code will DISABLE camera control if the communication screen is active for the human operator
void TriggerStatusCameraView(bool activeOrNot)
{
GameObject.Find ("FPSController").GetComponent<RigidbodyFirstPersonController_custom> ().enabled = activeOrNot;
}
}
The solution was simple. Thanks for looking into this.
Text CommunicationMessage = GameObject.Find("CrazyMsg").GetComponent<Text>();
Button CommunicationButton = GameObject.Find("MissionButton").GetComponent<Button>();

How to exit from from game loop without closing the application?

How I create and run the instance of the game:
using (Game1 game = new Game1())
{
game.Run();
MessageBox.Show("Game finished");
// A lot of other things that I don't want to do inside the game"
}
If inside the game, at a certain point, I write:
this.Exit();
The Application closes entirely, and the MessageBox is never shown.
How can I just let .Run() return?
Edit: I noticed that clicking onto the "x" of the window while the game is running gives the effect I want
https://msdn.microsoft.com/en-us/library/microsoft.xna.framework.game.onexiting.aspx might help, something like this should do your thing (put that in your Game class):
protected override void OnExiting (Object sender, EventArgs args)
{
MessageBox.Show("Game finished");
}

Categories

Resources