Help understanding Error messages/sorting an array

Hello, I am in the process of going through the Udemy course “Learn to Code by Making Games - Complete C# Unity” I am at the tail end of the Block Breaker section.

Full disclosure, one could describe my level of competence as “novice” only if one were being very generous.

This is a question unrelated to Block Breaker. I am in the midst of fiddling around using Unity to make a character generator and eventually Virtual Tabletop (VTT) assistant of sorts to aid in online RPG play. One of the key things I will need to do over and over again is roll dice and get the results.

I have the basics of a script for rolling [numberofdice]d[sizeofdie]. In the particular game I will be playing there is are also types of rolls “boons” and “banes” with boons you roll a number of d6 and take the highest one and add that to the 1d20 roll. Banes work the same but you subtract the highest die from the d20 roll.

I figured rather than rewriting essentially the same code for the boons and banes rolls I could point the boon and bane methods to the RollThem Method and use that putting the results of the rolls into an array, sorting them, then pulling the highest number only. The problems I am running into is twofold. Attempting to call the RollThem Methods for the Bane or Boon give the following error…

Error CS0029: Cannot implicitly convert type ‘void’ to ‘int’ (CS0029) (Assembly-CSharp)

The second problem is getting the array to sort. When I ran into trouble with the rolling I tried seeding the array manually just to test out that part of the code. The documentation in the Unity - Scripting API is completely unhelpful in this regard.Diltering for unity 4.6 and C# it has the following entry…

Array.Sort()
Description

Sorts all Array elements.

Var hello = new Array (“c”, “a”, “b”); hello.Sort(); print(hello); // prints a, b, c.

Using this example results in the following error…

Error CS1501: No overload for method ‘Sort’ takes ‘0’ arguments (CS1501) (Assembly-CSharp)

If you could help be find my way back to the proper path I would be eternally grateful.

Ultimately, I may scrap this entire approach as I (hopefully) learn more and understand more but I am thus far pretty confused by OOP in general and can only infrequently understand what error messages are trying to convey. I hope this was not too rambling and someone can make sense of what my issues are and can provide enlightenment.

I did attempt to put this on github as suggested but after ~2 hour trip down that rabbit hole I gave up and I’m just including a ZIP file of the project.

Thanks in advance,
Hans

DiceRollerPoC.zip (147.5 KB)

It seems you need to take a closer look at some of the API documentation as it seems you types don’t always match. That’s what the errors are saying.

Hi @Hansmo;

For the first problem, I’ve not looked at your project but the error you list;

Error CS0029: Cannot implicitly convert type 'void' to 'int' (CS0029) (Assembly-CSharp)

May suggest that you are calling a method which returns void and you are expecting an int. Assuming this is the case, change the return type of your method to int, e.g.

public int RollThem()
{
    int result = 0;

    // do clever stuff to establish the actual value of "result" here

    return result;
}

When the above method is called it will now return a value of type int rather than of type void.

You could of course break this method down a bit further, for example, the commonality between what you have described is the rolling of a D6 and the rolling of D20. For all intents and purposes, that’s two calls to the same “get me a random number” method where you provide the min/max values. You then calculate slightly different based on whether your rolling as “boons” or “banes”. I don’t know whether you may have any other factors to consider such as modifiers to the dice rolls for any other part of the game, but you could break it down a bit like this;

using UnityEngine;

public class DiceRoller
{
    // handles multiple dice and adding 1D20
    public int RollBoon(int numberOfDice)
    {
        int result = 0;

       result = GetHighestValueFromMultipleD6Rolls(numberOfDice) + RollD20();       

        return result;
    }

    // handles multiple dice and deducting 1D20
    public int RollBane(int numberOfDice)
    {
        int result = 0;

        result = GetHighestValueFromMultipleD6Rolls(numberOfDice) - RollD20(); 

        // are negative values are allowed?  If not, handle that here

        return result;        
    }
    
    // returns the highest scoring di from a number of D6s
    private int GetHighestValueFromMultipleD6Rolls(int numberOfDice)
    {
        int highestResult = 0;
        int result = 0;

        for (int i=0; highestResult < 6 && i < numberOfDice; i++)    // multi-condition which will save rolling if you already have a 6
        {
            result = RollD6();

            if(result > highestResult)
            {
                highestResult = result;
            }
        }

        return highestResult;
    }

    // returns 1D6
    public int RollD6()
    {
        return GetRandomValue(1, 6);
    }

	// example of extended D6 roll to support modifiers
	// public int RollD6(int modifier)
	// {
	//     int result = 0;
	//
	//     result = GetRandomValue(1, 6);
	//     result += modifier;
	//
	//     return result;
	// }

        // returns 1D20
	public int RollD20()
	{
		return GetRandomValue(1, 20);
	}

	// example of extended D20 roll to support modifiers
	// public int RollD20(int modifier)
	// {
	//     int result = 0;
	//
	//     result = GetRandomValue(1, 20);
	//     result += modifier;
	//
	//      return result;
	// }

    // returns a random value int between min and max
    private int GetRandomValue(int min, int max)
    {
        return Random.Range(min, max+1)    // note: +1 as using ints maximum is exclusive not inclusive
    }
}

Again, if it were me, I’d probably them look at the above and break out what is actual dice behaviour into a separate class, and have what’s left in a class which handles these types (boon / bane etc)

For you second problem give this a try;

using UnityEngine;
using System.Collections;

public class ArrayTest
{
    public void Start()
    {
       Array hello = new Array("c", "a", "b");

       Array.Sort(hello);    // this is the main difference in the usage

        // output
        for (int i = 0; i < hello.Length; i++)
        {
            Debug.Log("Index " + i.ToString() + " : " + hello[i].ToString());
        }
    }
}

Note there are quite a few ways you can sort arrays, the link below takes you to the above usage, but the navigation on the left of that page will provide you with many others. You may want to filter though as some will be for more recent versions of .Net which Unity 4.6 will not be supporting.


See also;

Thank you for your assistance. I will give this a try when I have a chance
:slight_smile:

Hans

15 posts were merged into an existing topic: Character Generator / Virtual Tabletop Assistant

Privacy & Terms