Monday, 31 August 2015

Using a raspberry pi to stream/download iplayer files

Necessity is the mother of invention, and when I was talking with my father about the trials of recording radio programs on the PC, then converting them to MP3 files for listening to later, we discussed and said that the best solution would be to have a Raspberry Pi quietly sitting around and downloading the files for you.  After spending an unfruitful afternoon with a Pi and a USB-DAB device, I wondered if I could get the radio shows off the iplayer instead.  I quick google later said I could, and a couple of afternoons later I had it working well.  So this is what I did.

Setup of the Raspberry Pi

I took a new Raspberry Pi (a B+, but I’m certain a B will work), installed Raspbian (using NOOBS) and setup the Wifi, which was a simple task.  The setup of the get_iplayer software was not so easy, but I eventually found https://github.com/get-iplayer/get_iplayer/wiki/raspbian which explained it excellently.  Once I had got get_iplayer active, I turned my attention to the web-pvr interface.

As I envisaged not having to login to the Pi, I set the web interface to accept connections from anywhere:
sudo nano /etc/default/get_iplayer_web_pvr
set
LISTEN=0.0.0.0
Warning: the web-pvr has a note saying its not very secure, so should only be used on internal (safe) environments and never facing the internet.

By default get_iplayer downloads tv as mp4, but radio as aac, so I needed to change that option.  For neatness I wanted to put all the files in one place (/home/pi/iplayer_media), and tidy the naming, so I used the following commands:
get_iplayer --add-prefs --aactomp3
get_iplayer --add-prefs --output "/home/pi/iplayer_media"
get_iplayer --add-prefs --file-prefix "<nameshort><-senum><-episodeshort>"
At this point, I could just run the get_iplayer_web_pvr and leave it, but rather than do that I wanted it started whenever the Pi booted up. To do this I found that editing rc.local was simple and worked.
sudo nano /etc/rc.local

just before exit 0 add:
su pi -c 'get_iplayer_web_pvr &'
All I needed to do was reboot the pi and I was ready to go.

Using the PI

Using my windows machine I opened up my browser and went to http://ip_address_of_the_pi:1935 I was greeted by the web UI




I enabled the BBC radio checkbox & click the refresh cache button. It opened a new window which updated the cache.


I then entered my search terms & hit the search button.

Once I got the results, I could have listened to the shows directly.  Clicking on the ‘play’ action gives me a m3u file which I can open with VLC or MPC-HQ (windows media player didn’t work for me though).  If you do, it seems to stream via the Raspberry Pi.


But what I wanted was offline playing, so I selected all the items and hit the record button.


THIS WAS SLOOOW. It downloaded the files (in some format) and then had to run ffmpeg to convert them to mp3 files which was rather slow.  I estimate it must have taken 10-15 minutes to perform the conversion.  But again, it was in a separate window, so I could still carry on.

A look about on the BBC TV side & I found an episode of the Clangers (how can you not like the Clangers?), so clicking on the Record link popped up another window to record that.  The TV recordings did not have to be converted it appears, as the time taken was very fast.


Once all the downloads were finished, I clicked on the recordings tab and saw all my downloads:


Clicking on the play/play direct gave me the m3u files, but a ‘right click->save as’ will allow you to get the mp3/mp4 file, though you will need to name it yourself, as it gives it a garbage name.



As an alternative, you can use WinSCP or a similar SFTP client to copy files directly from the Pi.

Conclusion

I could have installed the get_iplayer directly on my PC, but it would have needed to install lots of dependencies on my PC too.  At $50, this is an idea task for a Raspberry Pi.  Now, all I have to do, is keep an eye on the iplayer for when Doctor Who get posted up.

Happy Watching/Listening,
Mark

Monday, 3 November 2014

How to fail the most important roll of the adventure and make the adventure better

An incredible thing happened to us in our last RPG session.  “The paladin is on his knees before the undead king, bolstered by the bard who had been singing praises, and makes his case to the long dead king.”
I picked up my 2 six sided dice and rolled them.

Snake eyes!

Even with all the bonuses, this was a failure, big time.

But as a result of that roll, the adventure became so much better.

Introducing Dungeon World

For those who have never heard of it, Dungeon world is a rules-light narrative based fantasy adventure game.  The core rules are simple, every time you want to do an action, roll 2 dice and add your attribute bonus (-3 to +3).

  • If you roll a 10+ it works
  • If you roll a 7-9 it works, but there is a complication (you put yourself in danger, you get hurt, you lose something, and so on)
  • If you roll a 6 or less, the action fails and the GM gets to add a new event to the game (which usually ups the stakes or makes a misfortune happen).

As you might have noticed, there is a lot of story telling, and it puts the GM under a lot of stress to come up with new ideas on the fly.  But I am enjoying playing it and it is giving me such good ideas for my own games.

The Story So Far

Sir Grifford of Imos (Paladin, my character) was tasked by the Church of the Silverflame to verify that the body of the long dead king Boltar the brave, was still resting in its hidden tomb.  With Nanoc the Barbarian, Galadiir the Elvish Wizard and Bertam the Bard, the adventures discovered that the body had been taken and had tracked it to an abandoned elven temple high on the mountains.
Having hacked and slashed their way through the orcs and bandits defending the place our brave adventurers reached the high temple and saw the body of Boltar lying on the alter with cultists around it performing a ritual.  One black knight stood between us and our goal, with right on our side what could go wrong? (Famous last words)

What Did Go Wrong

Remember earlier when I mentioned that every time the player rolls 6 or less the action fails and the GM introduced a new event.  Well, I seem to remember that just about every one of the first ten rolls we made failed.  So here are some of the complications that happened:

  • The magical spell casting skull which we beat earlier reappeared
  • The female drow elf which had fallen off the side of the mountain reappeared in the combat seeking to murder Bertam who tricked her
  • One of the PC’s stands on one of many long dead corpses which turned out to be undead rather than dead (big oops)
  • The ritual which was to reincarnate Boltar hits the point of no return and we realise we cannot stop his resurrect, only stop the cult from completing the ritual to control the resurrect d king
  • The ghost of the Boltars wife (a very possessive woman), who has been hidden inside Galadiir’s staff, decides that she must protect her husband at all costs and allow him to come back to life, so she possessed the drow and takes to defending the body.
  • Nanoc fighting the black knight with her great sword manage to together smash into the wall of the temple and the wall and then sections of the floor begin to crumble

As you can see the stakes are getting pretty high, but the worst is yet to come.  For one of the failures, the GM asked me a question:
“What would be the worst thing if Boltar were to come back to life?”
Put on the spot I came up with the first thing on my mind:
“Boltar was an Elf hater (In our story, the Great Elven Empire has just fallen apart), if he finds out what the elves have done well…”
With hindsight, it was a great answer from the perspective of the story, but not from the perspective of common sense.
I also love the way that our GM often asks us to give facts for the story and weaves it all together.  Some of that is Dungeon world, and some of that if just good storytelling.

The Final Roll

Having defeated the cultists, watched several of the major NPC’s fall/escape through the crumbling floor, and having nearly lost the body of the king to the chasm, we try to explain to the undead king what has happened.  The king believes that we resurrected him and demands to know what has happened.  So we explain that the Church of the Silverflame sent us to find him (we don’t mention that we tried to stop his resurrection), and he sort of believes it until he spots the elf wizard (oops).  At which point we try to explain about the need to a local guide to navigate around the elven temple and place of power which he was brought back to life in.
But that’s when I rolled the snake eyes.
The king decides that he has had enough of us, binds the part in a web of lightning and flies off to the distance.  By the time the lightning has worn off, he is long gone.

Conclusion

Well, it wasn’t just my bad roll which messed the party up.  It was a lot of bad rolls all around.  But what a story we made out of those bad rolls.
So here is a thought to all you GM’s out there:
Instead of just using a bad roll as ‘you miss’ or ‘it doesn’t work’, think about using those bad rolls to introduce new story elements or new problems for the party to encounter.  Increase the stakes, add to the tension, or make it much tougher for the party.
Make those failures more meaningful.

Tuesday, 2 September 2014

Compiling and Deploying Windows Store Applications using MonoGame or Unity 3D

This time, I'm addressing an annoying concern that I found. Trying to understand how to build Windows Apps.

There are three flavors of Windows 8:
  • Windows 8 Desktop (the normal operating system we know and … use)
  • Windows 8 RT (which runs on the surface tablets)
  • Windows Phone 8 (yet another OS)
Windows 8 Desktop is simple to build applications for. Its backwards compatible with all the previous versions, so any techniques you used in the past work. But the Windows RT is a different beast. The main thing is it only runs Store Applications, it will not run any of the ‘normal’ windows applications.

The worst bit about all this is the lack/fragmented collection of documentation on the topic. So this is me, trying to put all the documentation all together in one place. To give a step by step process of compiling the application as a Windows Store Application and deploying it on a Windows Surface Device.

Most of this document will be on using MonoGame to make applications, with an extra section at the bottom on using Unity3D.

Setup:

You will need
  • A windows 8 Desktop machine with the following installed
  • A Windows RT device for deployment testing
  • A network
    • Because both devices MUST be in the same subnet
  • A Windows Live account to enable your developer access

Network Test:

First things, make sure that both sides can ping the other. Use ipconfig to get the IP addresses and ping to test connectivity. When I was testing, I found that my surface device did not respond to pings, so the following steps had to be done.
  • Go to control panel: system & security: Advanced settings: firewall
  • In the incoming settings: find ‘File and Printer Sharing (Echo Request –ICMP IPv4) and enable it
That allowed incoming pings and I was sure that the network was operational


The Blue Screen of Success:

Simplest task is to create a new project, compile & deploy to the device.
In VS2012, create a new project of type ‘Windows Store App’
There may be a requirement for you to get a windows developer license:
To get this you will need your live.com account. It will give you a licence for ~30 days.

Compile & test that you get a nice sky blue window on you development machine.

Running on Remote:

Now in the toolbar, change the location from ‘Local Machine’ to ‘Remote Machine’
You will get a dialog popping up asking for the IP address of the device, which you can enter.
If you ever need to change it again, just open the projects properties and select the Debug Settings:
On Tablet run the ‘Remote Debugger’ application:
It may be necessary to also setup the firewall exceptions and request a developer license for the tablet too. But at least Microsoft have made it fairly painless.
Once you have the Remote Debugger running, you can hit F5 on the development machine and watch the PC deploy and run the application on the tablet.
<Sarcasm> It’s a blue screen!!! Wow!!! </Sarcasm>
You can change the application name by altering the ‘Package.AppManifest’, and change the icons using your favourite paint program. And then start to write some real code.
All easy so far.

Content Pipeline Woes:

One of the create joys of working in XNA was the drag-drop nature and ‘it just works™’ of the content pipeline. However, MonoGame has yet to ace this one currently, so this will get a little bit messy.

Now add in a new project of the type ‘MonoGame content project’. I called mine ‘MyContent’.

Note:
I’m not sure it this is available in VS2012 by default. Most of the MonoGame documents say that you use VS2010 to use this. I believe that because I installed the XNA tools for VS2012 (https://msxna.codeplex.com/) it worked ok.

You add in XNA content as usual. But for each content you add, you must select it and change its content processor to the MonoGame equivalent:
Once all the content is in, right click on the content project and select the configuration manager:
For the content project, set it to Windows 8| any CPU
Compile the ‘MyContent’ project, then open up explorer & look into the “GameName1/MyContent/MyContent/bin/Windows8” directory.
Ignoring the dll (obviously), and drag drop the whole content folder into Visual Studio project
Say yes-to-all for any overwriting questions.

Now select ALL the XNB items and change build action to ‘content’. Some people say that you should change the ‘Copy to Output Directory’ to ‘Copy If Newer’ must also be set. However I didn’t and it seemed to work for me.
You can now use the Content as you would use it in a normal XNA project.


Debugging the Errors:

If you followed the above steps EXACTLY, it should have worked. Notice I say ‘should’. When I was developing, I spent a lot of time watching my application near silently fail with no idea what was wrong.

If you are one of those, look at the output window (if you don’t see it, Menu: Debug: Windows: Output)
There are a lot of first chance exceptions: File not found & Content Load Exception. But Visual Studio didn’t do its usual stopping as soon as it hit an error. I don’t know why it doesn’t instantly stop in MonoGame when it does in XNA. But here is how to re-enable this:

In the menu, select Debug: Exceptions. When the dialog box appears, press the Add button:
Add in: Managed Debugging Assistants, Microsoft.Xna.Framework.Content.ContentLoadException
You must then make sure the check box is ticked.
Now close the dialog box and in the menu select: Tools: Options.

In the dialog box, find Debugging: General. Then set ‘Enable the exception assistant’ and ‘Enable Just My Code’
Now if there is a problem with the content, it will now stop instantly (as it should). You can now double check the spelling, make sure the file is in the content folder, check that the properties are set correctly etc.

Extra: Compiling Windows Store Applications with Unity3D

To compile a Windows Store Application with Unity 3D is very simple once you have got the development machine with VS2012 and the hardware with the remote debugging tools working together.

Create your Unity3D project as usual (I’m using angry bots for this).

Open Unity’s Build settings and select Windows Store Apps. You are given the choice of XAML C# Solution or XAML C++ Solution. There seems to be little difference between the two (though C# gives the option ‘Debugging Unity C# Projects’).
Once you have done the settings, build the project. You will be prompted for a directory to put the files into. Once the building has finished, you will discover that Unity has creates a Visual Studio solution which you must now open.

Deploying the project:

This is basically the same as for MonoGame: Open the project, select remote device, compile & deploy.

The only item to note is that when compiling in Debug mode, the deployment size is greater than 100MB! That takes a very long time to deploy on your device unless you have very fast WiFi.

Therefore it’s a better idea to try compiling and testing locally or in the simulator before going for deployment to hardware.


Conclusion:


You can see my Window RT tablet with the games installed. It’s been an interesting challenge to get all the facts together and put it all into one place. Hopefully this will be useful to others working on getting Unity3D/MonoGame making Windows Store Applications.

Happy Coding:
Mark

Sunday, 10 November 2013

Teaching the kids to retrocode

I have an O level in computer Science (GCSE actually).
To get this I learned to program the BBC micro computer.  For those who don't know here's the summary:

http://en.wikipedia.org/wiki/BBC_Micro Its 1980's home computer. It has 8 bit CPU with 32KB of memory and runs about 2MHz. Compare this to modern PCs which have 4GB of Memory (x125000 as much) and run at 4GHz (x2000 times as fast). Did I mention that you have to plug them into a TV to make them work and load and save their programs to a audio cassette?

Anyway I was down in Singapore Science Centre, today & what did I find but:

Instantly, I grabbed the kids & pulled them in.

After they had finished playing pacman using pieces of fruit (I know it sounds weird, it was a demo of using conductivity). I managed to spot a spare computer and had them sitting down and play in grannies garden (those who know it love this game).
I then handed them a handy programming sheet and a got them onto it.  It was a little funny with the locals asking me for help, (they assumed I was one of the organisers), but I didn't mind I was back into my environment and remembering all the strange quirks of how a BBC was to program.  The tutorials were very good stuff, only 15 minutes or so to type in (depending upon your keyboard skills).

Here were the results:


Did they enjoy it, YES. Did I enjoy it? EVEN MORE!

My next thoughts are, can I build some simple graphical tutorials (like the ones the kids did), which can be typed in so quickly and easily? It will probably by Python on a Raspberry Pi. But I need to put my thinking cap on a bit to look at this.

TTFN,
Mark

Thursday, 26 September 2013

The Programmers RPG

If you have not played it, please go and play the Programmers RPG.
Kongregate Games logo
Its a silly 5-10 minute game which I built as final assignment for a class I was taking on Unity 3D. After I finished it, I decided, ‘let try to publish it on Kongregate, the worst they can do is laugh at it and reject it’, so I did.
Well, I put it up, it was accepted instantly & is still on the main page (4 days after being put up).


That’s kind of cool, but I’m not really impressed with this game.  What I am impressed with was the way I got a reasonable RPG system to work & that’s what I’m going to be talking about in these posts.

The Basics:

What things are the “must have” for RPG’s? Here are the main bits I added.

  • Dialog system (PC’s talking to NPC’s)
  • Message display (for any other stuff)
  • Quests/Missions
  • Combat (but my combat system wasn’t very good, so I won’t talk on it much)

There are also some other “must have’s” for RPG’s which I didn’t add:

  • Levels & XP
  • Items & Inventory systems
  • Saving games

There were also a lot of over standard game conventions dropped in this game, which I might cover later:

  • Health bars (2D & 3D)
  • Arrow’s showing you where to go
  • Knockback
  • Treasure springing from the chest & being attracted to the player
  • Respawns
  • Mouse look with the zoom in & out
  • Things flashing for a bit & vanishing

But lets get back to the main bit, the quests.

Dialog system:

For those not sure, go over to google & type ‘RPG dialog box’.  The hoards of pictures show a very clear idea of what we want: the text appearing letter by letter, faster if you hold the button, often with pictures of the character(s) talking.

So here is roughly what the code looked like:

public class RpgDialog : MonoBehaviour 
{
    public GUISkin skin;
    public float textRate = 2; // how fast the text appears
    public AudioClip tickSound; // audio
    public int border=5, height=100; // size of dialog 

    float textCounter = 0;
    Texture2D leftImage, rightImage; // images
    string theText = null; // the text (null means hidden)
    Rect mainRect; // rectangle for box

    public bool Finished { get { return theText==null; } }
    public void Show(string txt, Texture2D left, Texture2D right)
    {
        theText = txt;
        leftImage = left;
        rightImage = right;
        textCounter = 0;
    }

    public void Hide()
    {
        theText=null;
    }

    void Start()
    {
        // if sound add the audio
 if (tickSound!=null)
 {
     AudioSource aud=gameObject.AddComponent<AudioSource>();
     aud.clip=tickSound;
 }

        // put the box at bottom of screen
        mainRect = new Rect(border, Screen.width-border-height, Screen.width - border*2, height);
    }

    // Update is called once per frame
    void Update () 
    {
        if (Finished) return;

 int oldCounter=Mathf.FloorToInt(textCounter); // for use later in audio
        if (Input.anyKey)   // any key makes it faster
            textCounter += Time.deltaTime * textRate*10;
 else
     textCounter += Time.deltaTime * textRate;
 // tick sound when displaying
 if (tickSound!=null && textCounter < theText.Length)
 {
     if (oldCounter!=Mathf.FloorToInt(textCounter)) // if new text
     {
  audio.clip=tickSound; // make sure it stays as a tick
  audio.Play();
     }
 }
        // if finished & space bar
        if (textCounter >= theText.Length)
        {         
            if (Input.GetKeyDown(KeyCode.Space))
                Hide();
        }
    }

    void OnGUI()
    {
        if (Finished) return;
 // set the skin
        GUISkin oldskin = GUI.skin;
        GUI.skin = skin;        
        GUI.Box(mainRect, ""); // draw the box
 // inner rect is where we must display the text
        Rect innerRect = GuiUtils.InflateRect(mainRect, -border, -border);
        // draw images as needed
        if (leftImage != null)
        {
            GUI.DrawTexture(new Rect(innerRect.x + border, innerRect.y + (innerRect.height - leftImage.height) / 2, leftImage.width, leftImage.height), leftImage);
            innerRect.x += leftImage.width + border * 2;
            innerRect.width -= leftImage.width + border * 2;
        }
        if (rightImage != null)
        {
            GUI.DrawTexture(new Rect(innerRect.xMax - rightImage.height - border, innerRect.y + (innerRect.height - rightImage.height) / 2, rightImage.width, rightImage.height), rightImage);
            innerRect.width -= rightImage.width + border * 2;
        }
        // draw the text
        if (theText != null)
        {
            string s = theText;
            if ((int)textCounter < theText.Length)
                s = theText.Substring(0, (int)textCounter);
            GUI.Label(innerRect, s);
        }
        // restore old skin
        GUI.skin = oldskin;
    }
}

The actual code had a little more than this, it also had a fade out routine to make it look a little nicer.
Notice the use of the GUISkin.  This allows me to customise the appearance of the dialog box, setting the font, text colour & background.

To make the dialog work I had a single game object, tagged as game controller and with this behaviour added.
Then the code to make this dialog appear looks like this:

// get the dialog
RpgDialog  dialog= GameObject.FindGameObjectWithTag("GameController") . GetComponent< RpgDialog>();
dialog.Show(“hello world”,null,null);

I will get on to how to stream all the dialogs together later.


Status message system:

This is not always found in RPG’s, its also very common on multiplayer games.  Its the one which appears and says general information, without stopping the gameplay.

In essence its just a simple GUI routine with a list of strings to hold.  The only clever bits, were just a timer to automatically move the text up once in a while & finding out how much text to hold. Here is the code:

public class MessageDisplay : MonoBehaviour {

    public float height = 100;
    public float border = 5;
    public float advanceDelay = 5;

    Rect mainRect;
    List<string> messages = new List<string>();
    float nextAdvance = float.MaxValue;

    public void Clear()
    {
        for(int i=0;i<messages.Count;i++)
     messages[i]="";
    }
    public void ShowMessage(string msg)
    {
        // remove one, add one
        messages.RemoveAt(0);
        messages.Add(msg);
        nextAdvance=Time.time+advanceDelay;   // in X seconds remove a message
    }
    
    // Use this for initialization
    void Start () 
    {
        // put the box at bottom of screen
        mainRect = new Rect(border, Screen.width-border-height, Screen.width - border*2, height);
        float lineHeight = skin.label.CalcSize(new GUIContent("W")).y;
        int maxLines = Mathf.FloorToInt(mainRect.height / lineHeight);
        // setup it with empties
        for (int i = 0; i < maxLines; i++)
            messages.Add("");        
    }
 
    // Update is called once per frame
    void OnGUI() 
    {
        // don’t show if the dialog is active
        if (!GetComponent<RpgDialog>().Finished()) return;
        // clear the screen after a while
        if (Time.time >= nextAdvance)
        {
            ShowMessage("");    // adds a blank
            // which will reset the timer too
        }
        string s = "";
        for (int i = 0; i < messages.Count; i++)
        {
            s += messages[i];
            s += "\n";
        }
        GUI.Label(mainRect, s, skin.label); 
    }
}
To use this, just put the component on the same game controller object. And access it the same way.  To stop the message display interfering with the rpg dialog, there is a simple check to spot if its in use & then don’t display the messages.

Again to use it is simple, just get a reference to the script & call a method or two.

// get the message
MessageDisplay message= GameObject.FindGameObjectWithTag("GameController").GetComponent<MessageDisplay>();
message.ShowMessage(“its a message”);

The quest system:

Ok, now its time to get technical.
You will need to self study a couple of topics:

Here is the basic idea of how it all works:
When the player walks up to the NPC they touch a trigger, the script on the old man reacts to this and checks the flags.
The flags are a collection of static variables which store which quests/missions have been performed or not.
If the correct flags are set, the NPC’s script will attach the quest script to the game controller.
The quest script is basically a big coroutine which will trigger the various other scripts we just wrote (the dialog and message) and activate them.

Lets look at some code, first the trigger:

public class QuestTrigger : MonoBehaviour {

    public string questName;
    void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Player")
        {
     // add quest to the main RPG & this destroy myself to stop muliple triggers
     GameObject rpg=GameObject.FindGameObjectWithTag("GameController");
     rpg.AddComponent(questName);
     Destroy(this);
        }
    }
}

Lets look at a silly quest. These can be written in C# or Javascript. I found Javascript to be simpler to work with because of the auto wrapping of coroutines, but I give them both as examples.

A C# quest:

public class qst_start : MonoBehaviour {

    // Use this for initialization
    void Start () 
    {
        StartCoroutine(DoQuest()); // start a coroutine
    }
    // all C# coroutines must return a IEnumerator
    IEnumerator DoQuest() 
    {
        // load the resource
        Texture2D portrait=(Texture2D)Resources.Load("portrait");
         
        // get the dialog box
        RpgDialog dialog= GameObject.FindGameObjectWithTag("GameController").GetComponent< RpgDialog>();

        // show the dialog and wait for it to close
        dialog.Show("<Yawn>\nThat was a good nap", portrait, null);
        while (!dialog.Finished()) yield 0;
        dialog.Show("<Looks Around>\nErr", portrait, null);
        while (!dialog.Finished()) yield 0;
        dialog.Show("Where am I?", portrait, portrait, null);
        while (!dialog.Finished()) yield 0;
        dialog.Show("I don't remember drinking *that* much beer", portrait, null);
        while (!dialog.Finished()) yield 0;

        // PS if any of the functions called from here yield anything, 
        // you cannot call them directly, but must StartCoroutine(), them

        // and so our adventure continues:

        // done: now exit
        Destroy(this);  // remove myself
        yield return 0;
    }
}

A Javascript quest:

function Start () 
{
    // in JS: you don't need the StartCoroutine, only the yield
    var portrait:Texture2D=Resources.Load("portrait") as Texture2D;
    var oldport:Texture2D=Resources.Load("port_oldman") as Texture2D;

    Dialog("Greetings mighty warrior",null,oldport);
    Dialog("Do you have the key?",null,oldport);
    if (RPG.has_key==true) // check the static variables
    {
        Dialog("Yes here is is",portrait,null);
  // etc,etc,etc
    }
    else
    {
        Dialog("Err, not yet ",portrait,null);
  // etc,etc,etc
    }
}

// don’t need to do anything complex for a coroutine in JS
function Dialog(txt:string, left:Texture2D, right:Texture2D)
{
    var dialog: RpgDialog=GameObject.FindGameObjectWithTag("GameController").GetComponent("RpgDialog");    
    dialog.Show(txt,left,right);
    while (!dialog.Finished()) yield 0;
}

Conclusion:

Thats the heart of the quests systems, the coroutines turned out to seriously simplify the work.  My original version had its own mini scripting engine, but was just so much work to do.
To really get it working well I would need to design how to manage the saving on game (probably at save points) and how to manage the coroutines between loading & saving.

Later I will write a bit more on the other features, but this is the most complex bit.

I hope that it helps some of you think on this.

Happy coding,
Mark

Sunday, 15 September 2013

Visual Basic and Serial Ports

BASIC was the first language I ever learned (BBC Micro BASIC to be precise).  And though I have moved on to many other languages, BASIC holds a special place in my heart. Last week, a friend of mine was complaining about having issues getting BASIC working on the latest version of Windows.
After asking him I realised that he was still using VB 6 (which was a 1990's application).  So I decided to put a morning aside to testing Visual Basic on Windows 7, and in particular getting the serial port code working.

So lets get to it!

1. Install VB.net 

I'm using the 2010 express version, but other versions should be quite similar.
A quick trip to the MS website

With the usual setup:
Its a 60MB download (not too bad)
And we are ready to go.

2. Start new project



3. Design the form

Make sure you add a serial port component from the toolbox

My form looked like this (see the serial port down at the bottom):


4. Add the code

The connection & sending was dead easy.

Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
    SerialPort1.Close() ' just in case
    ' setup serial port
    SerialPort1.PortName = txtPort.Text
    SerialPort1.BaudRate = 9600
    SerialPort1.Open()
End Sub

Private Sub BtnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStop.Click
    SerialPort1.Write("0")
End Sub

Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
    SerialPort1.Write("1")
End Sub

If you get any blue underlining, it just means its missing a library, right click on the offending item & import the library.

5. Add the receiving code

First task, select the serial port & get up its properties.  Then you need to give the name of a function to be called when there is data waiting to be received.

You then double click on the item & it brings you to the code editor & you can just type in your code:

Private Sub OnDataIn(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
    Dim sp As SerialPort = CType(sender, SerialPort)
    Dim indata As String = sp.ReadExisting()
    System.Diagnostics.Debug.WriteLine(indata)
End Sub

This ALMOST works, but often I found that VB would be reading too fast & not get the entire line of data, just part of it
So a small change to the code & we are good
Private Sub OnDataIn(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
    Dim sp As SerialPort = CType(sender, SerialPort)
    ' note: ReadExisting() will only read what is in the buffer right now
    '   which means you might get only half the message & have to reassemble it
    ' Its far easier to use ReadLine() which will block until the whole line is in
    'Dim indata As String = sp.ReadExisting()
     Dim indata As String = sp.ReadLine()
     System.Diagnostics.Debug.WriteLine(indata)
End Sub

6. Deal with the dragons

So last task is just take this text and update the UI. Simple, Right? Wrong!

Errr....
Lets try and address this one

To keep an eye on the serial port, VB runs a thread. But it’s not the same thread that is running the rest of the UI and all our other code.
Thread B is not allowed to access the UI stuff which is owned by thread A, because it’s basically very dangerous to let two threads play around with the same things at the same time.

7. Solving the problem with an invoke

I tracked down a fairly simple piece of code to get around this issue from http://www.dreamincode.net/forums/blog/143/entry-2337-handling-the-dreaded-cross-thread-exception/
It looked like this:

' this delegate will help us later
' for those from a C/C++ background, think 'function pointer'
Delegate Sub ThreadSafeDelegate(ByVal ctrl As Control, ByVal str As String)

'The method with the delegate signature
Private Sub ChangeText(ByVal ctrl As Control, ByVal str As String)
    If ctrl.InvokeRequired Then
        ctrl.Invoke(New ThreadSafeDelegate(AddressOf ChangeText), New Object() {ctrl, str})
    Else
        ctrl.Text = str
    End If
End Sub

What its doing, is if the function if called from thread B, the thread invokes the function (it puts the function & all the arguments in a safe place). Then later when thread A comes by, it notices the function needs to be called & calls it.
I then added my version to do the job I really wanted
Private Sub AppendText(ByVal ctrl As Control, ByVal str As String)
    ' if not thread safe, recall this function
    If ctrl.InvokeRequired Then
        ctrl.Invoke(New ThreadSafeDelegate(AddressOf AppendText), New Object() {ctrl, str})
        Return
    End If
    ' do the real work
    Dim lb As ListBox = CType(ctrl, ListBox) 'get the list box
    If lb.Items.Count >= 10 Then ' if its too ful remove the old stuff
        lb.Items.RemoveAt(0)
    End If
    lb.Items.Add(str) ' add the new stuff
End Sub

And my receive code works just fine now

Private Sub OnDataIn(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
    Dim sp As SerialPort = CType(sender, SerialPort)
    Dim indata As String = sp.ReadLine()
    System.Diagnostics.Debug.WriteLine(indata)
    'lstDataIn.Items.Add(indata)
    AppendText(lstDataIn, indata) ' thread save version
End Sub

Conclusion:

Apart from the thread safe issue, this was so easy.  But then VB was always designed for a quick UI hackup.  All this work took less than 2 hours (including the time to overcome the dragon).  Having to add this extra thread code might make it a little harder, but VB.Net seems to be able to still deliver the quick prototypes that VB 6 did 20 years ago.
This is not the only way to some this problem.  I could have done all this with C# as well, it would probably have been the same thing, with the same cross thread issue as well.  But today was VB’s day.

Happy coding,
Mark