Converting from 3.1 to 4.0... gamestates - xna-4.0

I'm restarting development of a game I was working on, and have successfully converted it over to XNA 4.0 using the cheat sheet. Most of it works, some of it doesn't, and I'm in the process of fixing what doesn't work.
One thing that I had was a state system that was heavily based on the state system used as an example of XNA Unleashed, the ebook. I didn't have much in it.... basically just the ability to pause the game.
When I paused the game, the action of the game would stop, and the word "PAUSED" would appear in block letters across the screen, and you'd see the paused action. However, now it seems that the sprite batch automatically clears out everything between frames, so when I pause the game now, the screen clears, leaving PAUSED over a purple background. Back then, I believe that adding "SaveStateMode.SaveState" would prevent that from happening, but that functionality was removed. When I did some research, I found out that it was removed because it was essentially useless, that's all handled in the game state manager.
However, I can't find any documentation on this. Where should I start? Right now, my code looks like this:
In the Playing Game State update method:
if (input.WasPressed(0, Buttons.Start, Keys.Enter))
{
GameManager.PushState((GameState)ThisGame.PausedGameState.Value);
}
public void PushState(GameState newState)
{
AddState(newState);
//Let everyone know we just changed states
if (OnStateChange != null)
{
OnStateChange(this, null);
}
}
private void AddState(GameState state)
{
states.Push(state);
Game.Components.Add(state);
//Register the event for this state
OnStateChange += state.StateChanged;
}
//PausedGameState Draw method:
public override void Draw(GameTime gameTime)
{
ThisGame.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null, null);
DrawPaused(); //draws the Paused Text
ThisGame.SpriteBatch.End();
}
The goal is to make "PAUSED" appear over the screen, with a snapshot of where the gameplay left off... and not "PAUSED" over a cleared purple screen.

Do you suppose that GraphicsDevice.Clear() somehow got called after the state changed to paused? What does the DrawPaused() method do in your code?

Related

Unity UI not working properly ONLY on Windows

I have been working on figuring out what is going on with my game's UI for at least two days now, and no progress.
Note that this is a mobile game, but I was asked to build for Windows for visualization and presentation purpose.
So the problem is that when I run my game on the Unity Editor, Android, iOS and Mac platforms the UI works just perfect, but then when I run the game on Windows the UI still works fine UNTIL I load a specific scene.
This specific scene is a loading screen (between main menu and a level) when the level finished async loading, a method called MoveObjects is called in a script in the loading screen, to move some objects that where spawned in the loading screen scene into the level scene (this is not the issue though, since I already try without this method and the problem on the UI persist).
Once the logic of this MoveObjects method is done, a start button is enabled in the loading screen, for the player to click and start playing (I did try moving the start button to the level scene, since maybe it not been a child of the currently active scene could be the issue, but the problem still persist). Is at this point that the UI is partially broken, what I mean with this is, that I can see buttons (and some other UI elements like a scrollbar) changing color/state when the mouse moves over them, but I cannot click on them anymore (the button wont even change to the pressed state).
Also note that I tried creating a development build to see if there was any errors in the console, and I notice that this problem is also affecting the old UI system, so I was not able to interact with the development console anymore.
Also also, note that if I grab and drag the scrollbar before this issue appear, and I keep holding down on the scrollbar until this happens, the mouse gets stuck on the scrollbar, meaning that I cannot interact with the UI anymore, but the scrollbar will still move with the mouse.
I already check that this things are not the source of the problem:
Missing EventSystem, GraphicRaycaster or InputModule.
Another UI element blocking the rest of the UI.
Canvas is Screen Space - Overlay so there is no need for a camera reference.
I only have one EventSystem.
Time.timeScale is 1.
I am not sure what else I could try, so if anyone has any suggestions, I would appreciate it. Thanks.
P.S: I am sorry to say that I cannot share any code or visual material or examples due to the confidentiality.
A major source for a non-working UI for me has always been another (invisible) UI object blocking the raycast (a transparent Image, or a large Text object with raycast on).
Here's a snippet I put together based on info found elsewhere, I often use it to track objects that are masking the raycast in complex UI situations. Place the component on a text object, make sure it's at least few lines tall, as the results will be displayed one under another.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
[RequireComponent(typeof(Text))]
public class DebugShowUnderCursor : MonoBehaviour
{
Text text;
EventSystem eventSystem;
List<RaycastResult> list;
void Start()
{
eventSystem = EventSystem.current;
text = GetComponent<Text>();
text.raycastTarget=false;
}
public List<RaycastResult> RaycastMouse(){
PointerEventData pointerData = new PointerEventData (EventSystem.current) { pointerId = -1, };
pointerData.position = Input.mousePosition;
List<RaycastResult> results = new List<RaycastResult>();
EventSystem.current.RaycastAll(pointerData, results);
return results;
}
void Update()
{
list= RaycastMouse();
string objects="";
foreach ( RaycastResult result in list)
objects+=result.gameObject.name+"\n";
text.text = objects;
}
}

deal with Unity 5 UI in augmented reality app

I'm trying to make an augmented reality application with vuforia and unity.
whenever it recognize the image target, it must tell a story by showing text , and it should enable the user to press next and back to go on reading the different parts of this story, I'm totally new to unity and don't know how to handle with UI throughout scripting, I need some help on how to accomplish the part of "going forward and backward on showing the story by hitting Next and Back buttons", and all these parts of story should be related to the same image target in the same scene.
I appreciate it if you help me with an example code.
You should create some script that attach on trackable object, maybe something like this.
public class DataBook {
string[] dataBook;
string idText;
bool isActive;
}
Then you must create another script to set that trackable object is active or not, this link can help you to get that.
https://developer.vuforia.com/forum/faq/unity-how-do-i-get-list-active-trackables
Then after you get the active trackable object, you can set the dialog from the book by create another controller script for button, example
public void Next() {
DataBook[] books = FindObjectsOfType<DataBook>(); // if the object more than one, it will be more easy if it only the one
foreach (var book in books)
{
if (book.isActive) {
book.idText += 1;
textUI.text = book.dataBook[idText]; //textUI assign to object text on canvas
}
}
}
you can learn about unity UI Button on this :
https://unity3d.com/learn/tutorials/modules/beginner/ui/ui-button
Good luck

Unity2D Set Time Trial That Kills Player When Over

I can't find any answers to this. What I want is to set a 5 min time limit that begins when the level begins and ends when level ends. I also want it to kill the player if it runs out. This is for Unity2D and I want the timer to be seen at top left or right of screen. So basically I need a time limit like that of Super Mario. In C# code please.
Break up your problem into easier tasks. You will need some type of timer, and you will want to know how to 'kill' the player.
To create a timer, you can take advantage of how Update runs once per frame. Unity also comes with a Time class. In Time, there is a variable deltaTime which keeps track of the time in seconds to complete the last frame. Some other things you might want to add on to your class that acts as a timer is when to start and stop the timer.
To output the value from a timer to the game, you might want to use Unity's UI system and canvas. You might create a text object that is part of a canvas and anchor that text object to the top left corner.
To 'kill' a player, you can have some method that runs once the timer reaches the time in question and run the kill logic you want.
Helpful links:
MonoBehavior Update: https://docs.unity3d.com/ScriptReference/MonoBehaviour.Update.html
Time: https://docs.unity3d.com/ScriptReference/Time.html
UI: https://unity3d.com/learn/tutorials/topics/user-interface-ui
You can create a class that keeps track of the timer and to check if the requirements to fail are still there.
In this case you can have a float for the timer, and a bool for the condition whenever you completed the level or not.
It the timer reaches zero, check if the bool is still false, if it is, then you call something like KillPlayer(). If you complete the level by hitting a trigger or anything that will know if the level should be considered finished, you set the bool to true.
So your final check could look like this:
public float levelTimer = 300f;
public bool levelComplete = false;
void Start()
{
levelTimer = 300f;
levelComplete = false;
}
void Update()
{
levelTimer -= Time.deltaTime;
if(levelTimer <= 0 && !levelComplete)
{
KillPlayer();
//Something like a Game Over screen maybe
}
}
I hope this help you in the right direction.

How to create a delay based on an animation in HaxeFlixel?

I'm developing my first game and I have a player class (FlxSprite) that has a death animation.
I want to remove the player from the stage as soon as the animation ends, but if I use:
player.animation.play('death');
remove(player);
The animation doesn't finish and the player just disappears.
The way I handle this in most of my projects is something like this:
override public function update(elapsed:Float):Void
{
if (!alive)
{
if (animation.finished)
{
exists = false;
}
super.update(elapsed);
}
// other update stuff...
super.update(elapsed);
}
override public function kill():Void
{
if (!alive || !exists)
{
return;
}
alive = false;
animation.play("death", true);
// Note: I DO NOT call super.kill() because I want to leave exists = true
}
And, in your PlayState or wherever you want to remove the object, just check if (!player.exists) remove(player);, I guess? I don't usually use remove, I just wait until the state is destroyed and then clean everything up with FlxDestroyUtil.
Your question isn't super clear, but I'll try help anyway.
Because of the way the game loop is looping, the remove function kills off the sprite before the animation plays. You can check to see when the animation is finished by putting an if statment in your update function, like so.
if (animation.finished) kill()
If you need an actual timed delay, you can see my previous answer where you increase a variable by FlxG.elapsed every update on a variable until it exceeds your timer length.
I hope this answers your question. You may want too look at methods such as kill() in the HaxeFlixel docs, as I think you might be confused with remove().
EDIT: very sorry, forgot to include one important detail. It is likely that you are calling animation.play every single frame - stop once your death animation plays. In my code I saw I have an isDying variable that stops any new animations starting, and checks to see if the current animation is finished. Without this, the finished variable might not flip.

Minimize/restore windows programmatically skipping the animation effect

I need to perform several operations on a list of windows (minimize some of them, restore others) in order to switch between two or more set of windows at once.
The problem with this are those animations you can see when minimizing and restoring a window. The whole process look terrible with all those animations going in and out, up and down.
I cannot, however, disable those animations because this is for other computers and i dont want to change other people's settings, plus those animations are actually useful when you minimize/restore one window only (i.e. when YOU do it manually) because you can see what is happening, but for doing it programmatically on several windows at a time, it's not nice.
I'm currenlty using the SendMessage function to send the WM_SYSCOMMAND message with params SC_MINIMIZE/SC_RESTORE. I dont know whether there is another way.
So, the question:
How can I minimize/restore a window programatically without the animation effect??
PS: The programming language is not important. I can use any language that's nessesary for accomplishing this.
SetWindowPlacement with SW_SHOWMINIMIZED or SW_RESTORE as appropriate for showCmd in WINDOWPLACEMENT seems to bypass window animation. I'd keep an eye on the functionality for future versions of the OS though since documentation does not mention anything about animation.
How about Hide > Minimize > Show ?
You could temporarily disable the animations and then restore the user's original setting.
class WindowsAnimationSuppressor {
public:
WindowsAnimationSuppressor() : m_suppressed(false) {
m_original_settings.cbSize = sizeof(m_original_settings);
if (::SystemParametersInfo(SPI_GETANIMATION,
sizeof(m_original_settings),
&m_original_settings, 0)) {
ANIMATIONINFO no_animation = { sizeof(no_animation), 0 };
::SystemParametersInfo(SPI_SETANIMATION,
sizeof(no_animation), &no_animation,
SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);
m_suppressed = true;
}
}
~WindowsAnimationSuppressor() {
if (m_suppressed) {
::SystemParametersInfo(SPI_SETANIMATION,
sizeof(m_original_settings),
&m_original_settings,
SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);
}
}
private:
bool m_suppressed;
ANIMATIONINFO m_original_settings;
};
void RearrangeWindows() {
WindowsAnimationSuppressor suppressor;
// Rearrange the windows here ...
}
When the suppressor is constructed, it remembers the user's original setting and turns off the animation. The destructor restores the original settings. By using a c'tor/d'tor, you ensure that the user's settings are restored if your rearranging code throws an exception.
There is a small window of vulnerability here. In theory, the user could change the setting during the operation, and then you'll slam the original setting back. That's extremely rare and not really that bad.

Resources