Roman Numeral Generator Part 1
Hi guys. I have had an idea for a roman numeral generator method for a while now and thought I would share it with you. The idea is to write a method so that you can pass it an int and it will output the roman numerals which represent that number. This tutorial series will probably only be 2-3 parts long but it might work well with the 4X Space Game tutorial for ship/planet/leader names etc… It could also be useful in other projects.
Firstly what are the roman numerals? Well 1 to 10 are:
These are the ones we will begin with in this part. We can add the higher numbers later.
Note: There is no 0 (zero) in roman numerals.
In Unity create a new script called RomanNumerals. Make it a pure class by removing the “: Monobehavior” next to the class name and deleting the Start and Update methods.
Next create a new public static method called RomanNumeralGenerator. Make it return a string and pass it an int.
Declare a string called roman and return it.
We are going to be using switch/case to set the value of roman. This is essentially a more efficient if/else statement.
Between declaring roman and returning it, create a Switch which is passed number then cases for each of the above numbers. Don’t forget to add a default case at the end.
Now for each case we need to set roman to the appropriate roman numeral.
But what about our default case? Well for now we will set roman equal to the number passed to the switch (converted to a string). That way if a number is passed to the method which isn’t between 1 and 10, it will just return that number as a string.
Great we can now convert the numbers 1 – 10 into roman numerals. But how can we tell this is working?
- If you have been following the 4X Space Game tutorial and have got to the part where we name the planets see the example immediately below (titled Example 1). Make sure the RomanNumerals is added somewhere in the assets folder (I added it to folder path Assets/Scripts/Data Handlers for those who have reached part 17).
- If you have not been following the 4X Space Game tutorial create a new Unity project, add the RomanNumerals script to the assets folder and then skip to the example below the 4X Space Game example (titled Example 2)
Open up the Galaxy script and go to the CreatePlanetData method. Where we set the name of the planet, we currently get the Star name and then concatenate that with a number. We want to keep the Star name bit but change it so the number displays as a roman numeral instead.
Call the RomanNumeralGenerator method and pass it the planetList.Count plus 1.
Hitting play now produces something like this:
Create a new script called Test and delete the Update method. In the Start method add a for loop where int i starts at 1 and finishes at 10.
In the loop add a Debug.Log that outputs the roman numeral for i.
Now in Unity create a new empty object and attach the Test script to it. Press play and you should see this in the console:
That concludes this part. In the next part we will add the higher roman numerals to our Generator.