I've looked around a lot.. and I haven't found anything to help me out.
This is the outcome I am looking for:
I am using XNA Game Studio 4.0, and I want to create my own library to use when making my game, so I don't have to rewrite code that I have previously used over and over every time I want to make a new game. So, I want to call in a library as a reference. My Library:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace iRaaptorLibraryXNA
{
public class XNALibrary
{
public static void consolePrint(string text)
{
var timeStamp = System.DateTime.Now.ToString(#"hh:mm:ss tt");
Console.WriteLine("[" + timeStamp + "] CONSOLE: " + text);
}
}
}
I use the consolePrint function a lot while debugging.
For example, if I wanted to print a line to show success, I'd like to be able to type
consolePrint("SUCCESS!")
and that prints it to console. I DONT want to have to repeatedly type in:
XNALibraryXNA.consolePrint("SUCCESS!");
I want to shorten it to just
consolePrint("SUCCESS!");
Here is my game code:
using iRaaptorLibraryXNA;
namespace xnaFun
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize()
XNALibrary.consolePrint("Initialized!");
}
protected override void LoadContent()
{
iRaaptorLibraryXNA.consolePrint("Loading content!"); // DOES PRINT TO CONSOLE
spriteBatch = new SpriteBatch(GraphicsDevice);
}
etc......
I hope I included enough information. ~iRaaptor
TL;DR I want to shorten
XMALibrary.consolePrint("text");
to
consolePrint("text");
Add this class to your library:
public class BaseGame : Microsoft.Xna.Framework.Game
{
protected void consolePrint(string message)
{
XNALibrary.consolePrint("Initialized!");
}
}
Your game class can then inherit from the basegame and use the consolePrint method:
public class MyGame : BaseGame
{
protected override void Initialize()
{
base.Initialize()
consolePrint("Initialized!");
}
}
Related
I'm trying to test a very simple script to learn to use the input-action system in MRTK. I use a standard function to read the input, from a tutorial and also found on the MRTK documentation here: https://microsoft.github.io/MixedRealityToolkit-Unity/api/Microsoft.MixedReality.Toolkit.Input.IMixedRealityPointerHandler.html
However, the code below gives me a error that I cannot figure out....
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Microsoft.MixedReality.Toolkit.Input;
public class GameManager : BaseInputHandler, IMixedRealityPointerHandler
{
public MixedRealityInputAction Showmenu;
public Text ButtonText;
int clicktimes = 0;
// Start is called before the first frame update
public void ButtonClicker()
{
clicktimes++;
ButtonText.text = "You clicked: " + clicktimes.ToString();
}
public void OnPointerUp(MixedRealityPointerEventData pd)
{
}
public void OnPointerDown(MixedRealityPointerEventData pd)
{
}
public void OnPointerClicked(MixedRealityPointerEventData pd)
{
}
public void OnPointerDragged(MixedRealityPointerEventData pd)
{
}
}
The error is: CS0534 'GameManager' does not implement inherited abstract member 'InputSystemGlobalHandlerListener.UnregisterHandlers()'
Can't figure it out... any help appreciated!!!
Stupid me.... here is the answer in case it's useful to someone:
protected override void UnregisterHandlers()
{
}
protected override void RegisterHandlers()
{
}
I'm working on a game engine. Now assume we have a Game class:
using System;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using System.Drawing;
using static OpenTK.Graphics.OpenGL.GL;
namespace Test2
{
class Game : GameWindow
{
public Game(int width, int height)
:base(width, height)
{
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
}
protected override void OnUpdateFrame(FrameEventArgs e)
{
base.OnUpdateFrame(e);
}
protected override void OnRenderFrame(FrameEventArgs e)
{
base.OnRenderFrame(e);
}
}
}
And we have a medium in which to call a method for every frame.
Now assume the person creating a game, creates a file called ball_behaviour script:
using System;
using System.Drawing;
namespace Test2.Project.Behaviour
{
class Ball
{
public void Update()
{
}
public void init()
{
A2D.Background = Color.CornflowerBlue;
}
public void preInit()
{
}
}
}
And assume there are other files like it, which have the methods Update, and init. How can I call these methods from protected override void OnRenderFrame for example, in every class that's registered under Test2.Project.Behaiviour namespace? Thank you so much in advanced.
EDIT:The GameWindow class comes from OpenTK, which is the API I'm using to interface c# with OpenGL.
EDIT #2: Kind of like the way Unity does it.
you could use a virtual void that you than override which would be easier and faster to implement,have done this thing yesterday
I created a new MonoGame iOS solution in Xamarin Studio. After that, I added to following code to Game1.cs:
bool IsiOS = false;
if (Device.RuntimePlatform == Device.iOS)
IsiOS = true;
But I get an error message when I build the project.
if (Device.RuntimePlatform == Device.iOS)
System.InvalidOperationException You MUST call Xamarin.Forms.Init();
prior to using it.
What is wrong with my code? I don't know what I should add or change.
code(Game1.cs):
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Xamarin.Forms;
namespace NewTest.iOS
{
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
bool IsiOS = false;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.IsFullScreen = true;
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
if (Device.RuntimePlatform == Device.iOS)
IsiOS = true;
}
protected override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.CornflowerBlue);
base.Draw(gameTime);
}
}
}
code(Program.cs):
using System;
using Foundation;
using UIKit;
namespace NewTest.iOS
{
[Register("AppDelegate")]
class Program : UIApplicationDelegate
{
private static Game1 game;
internal static void RunGame()
{
game = new Game1();
game.Run();
}
static void Main(string[] args)
{
UIApplication.Main(args, null, "AppDelegate");
}
public override void FinishedLaunching(UIApplication app)
{
RunGame();
}
}
}
The Device class is part of Xamarin Forms. If you are using Xamarin Forms, then you must initialize it first. That is exactly what the error message is telling you.
I just started using a Hololens and have been messing around with the voice recognition scripts from the tutorials on the Microsoft website.I have been using unity to create 3D texts, however, if I try to change the text using voice commands it does not work!
This is my following code :
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Windows.Speech;
using UnityEngine.UI;
public class VoiceRecognized : MonoBehaviour {
KeywordRecognizer kr = null;
Dictionary<string, System.Action> keywords = new Dictionary<string, System.Action>();
// Use this for initialization
public Text name_text;
private string testName = "Drop";
void begin () {
keywords.Add("Change", () =>
{
this.BroadcastMessage("OnReset");
});
kr = new KeywordRecognizer(keywords.Keys.ToArray());
kr.OnPhraseRecognized += KeywordRecognizer_OnPhraseRecognized;
kr.Start();
}
public void KeywordRecognizer_OnPhraseRecognized(PhraseRecognizedEventArgs args)
{
System.Action keywordAction;
if (keywords.TryGetValue(args.text, out keywordAction))
{
keywordAction.Invoke();
}
}
// Update is called once per frame
void Update () {
}
}
The other program that changes the text upon hearing "Change".
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class ChangeText : MonoBehaviour {
public Text name_text;
void Start()
{
}
void OnReset()
{
name_text.text = "Change";
}
}
In this code, I am using voice recognition script to change the initial text "Hello World" to "Change". I am not sure if there a problem with my Unity, however I am currently following everything Microsoft is doing with their tutorials.
Try invoking same method on button click, see if that works. You can use Debug.Log to see if you method gets called with voice command.
I am looking to get platform specific location details using Xamarin's dependency injection but running into issues. More than likely from doing it wrong.
Here is my current setup:
nuMaps/Interfaces/ILocationService.cs
using Xamarin.Forms.Maps;
namespace nuMaps
{
public interface ILocationService
{
void initLocationService();
Position getCurrentPosition();
}
}
nuMaps/nuMaps.Droid/Interfaces/LocationService.cs
using System;
using nuMaps;
using nuMaps.Droid;
using Xamarin.Forms.Maps;
using Android.App;
using Android.Gms.Common;
using Android.Gms.Common.Apis;
using Android.Gms.Location;
using Android.Locations;
using Android.Widget;
[assembly: Xamarin.Forms.Dependency (typeof (LocationService))]
namespace nuMaps.Droid
{
public class LocationService : Java.Lang.Object, ILocationService, IGoogleApiClientConnectionCallbacks, IGoogleApiClientOnConnectionFailedListener, Android.Gms.Location.ILocationListener
{
readonly IGoogleApiClient _googleApiClient;
readonly LocationRequest _locRequest;
Position _currentPosition;
Location _currentLocation;
public LocationService()
{
Console.WriteLine ("LocationService constructor");
_currentPosition = new Position (0, 0);
_googleApiClient = new GoogleApiClientBuilder (Xamarin.Forms.Forms.Context)
.AddApi (LocationServices.Api)
.AddConnectionCallbacks (this)
.AddOnConnectionFailedListener (this)
.Build ();
_locRequest = new LocationRequest ();
}
#region ILocationService implementation
public void initLocationService()
{
_googleApiClient.Connect ();
}
public Position getCurrentPosition ()
{
if (_googleApiClient.IsConnected) {
_currentLocation = LocationServices.FusedLocationApi.GetLastLocation (_googleApiClient);
_currentPosition = new Position (_currentLocation.Latitude, _currentLocation.Longitude);
}
_googleApiClient.Disconnect ();
return new Position (_currentLocation.Latitude, _currentLocation.Longitude);
}
#endregion
public void OnLocationChanged(Location l)
{
Console.WriteLine ("OnLocationChanged");
_currentLocation = l;
}
public void OnConnectionFailed (ConnectionResult result)
{
Console.WriteLine ("ConnectionFailed");
}
#region IGoogleApiClientConnectionCallbacks implementation
public void OnConnected (Android.OS.Bundle connectionHint)
{
Console.WriteLine ("OnConnected");
if (!_googleApiClient.IsConnected)
LocationServices.FusedLocationApi.RequestLocationUpdates (_googleApiClient, _locRequest, this);
_currentLocation = LocationServices.FusedLocationApi.GetLastLocation (_googleApiClient);
}
public void OnConnectionSuspended (int cause)
{
Console.WriteLine ("OnConnectionSuspended");
}
#endregion
}
}
Usage in nuMaps/Views/MapPage.xaml.cs
using Xamarin.Forms;
using Xamarin.Forms.Maps;
using System;
using System.Diagnostics;
namespace nuMaps
{
public partial class MapPage : ContentPage
{
public MapPage ()
{
InitializeComponent ();
Position p = DependencyService.Get<ILocationService>().getCurrentPosition();
MyMap.MoveToRegion (
MapSpan.FromCenterAndRadius (
p, Distance.FromMiles (1)
)
);
}
}
}
nuMaps/Views/Loginpage.xaml.cs
using System;
using Xamarin.Forms;
using nuMaps;
namespace nuMaps
{
public partial class LoginPage : ContentPage
{
public LoginPage ()
{
InitializeComponent ();
/*
* Init platform specific location service.
* TODO: This *shouldn't* need to happen, but we don't get location information until
* the OnConnected callback happens which is too slow to put in getCurrentLocation method.
*/
DependencyService.Get<ILocationService>().initLocationService();
}
}
}
I believe the problem is in your implementation of ILocationService.
I would remove implementing activity (why do you want to use OnCreate?) and take a look at http://developer.xamarin.com/guides/android/platform_features/maps_and_location/location/.
I'd recommend on Android getting the GPS location via the google play apis, which will require implementing ILocationListener, IGoogleApiClientConnectionCallbacks, and IGoogleApiClientOnConnectionFailedListener. Hope that helps!
Edit for comments:
If the LocationService in the question is up to date, I don't see that you're implementing IGoogleApiClientConnectionCallbacks or ILocationListener. It may be that because the mappage is using gps, GetLastKnownLocation works, because a location has recently been obtained.
GPS location requesting is an async operation - one of the methods with IGoogleApiClientConnectionCallbacks is OnConnected, where you should call something like:
LocationServices.FusedLocationApi.RequestLocationUpdates(googleApiClient, locationRequest, this);
This will actively request location updates, and then fire OnLocationChanged(Location location) when an location update is returned. This is all async, so you'll likely need to expose these events in your LocationService and subscribe to them.
You could give TinyIoC a try instead of the Xamarin Dependency Service. It works well for platform-specific implementations, like location services.
Maybe the Xamarin Dependency Service can be used for these types of things, but I've only used it for custom Renderers so far. For more service-based stuff, I use TinyIoC.