0

I am making an RPG, and I have a monster encounter function where I was adding weight and pattern to enemy attacks, and where I can edit it when calling the function:

Is it possible for arrays and variables to both be parameters in the same function? If not, are there any alternatives to achieve the same goal?

static void MonsterEncounter(float hp = 10, float speed = 3, float attack = 2, string monName = "Monster",  float exp = 200F, float gold = 30, bool boss = false, bool doubleBattle = false, bool twoEnemy = false, float hpTwo = 10, float speedTwo = 10, float attackTwo = 1, string monNameTwo = "Monster 2", int teamMate = 0, string status = "Fine", string powerAttack = "Super Hit", float powerAttackHit = 10, float powerAttackHeal = 2, bool isHealing = false, bool isDefending = false, int waitTime = 0, string statusAfflict = "null", bool hasWeight = true, bool hasPattern = false, string statusTwo = "Fine",string powerAttackTwo = "Super Hit", float powerAttackHitTwo = 10, float powerAttackHealTwo = 2, bool isHealingTwo = false, bool isDefendingTwo = false, int waitTimeTwo = 0,string statusAfflictTwo = "null", bool hasWeightTwo = true, bool hasPatternTwo = false, int[] patternTwo = {1, 1, 1, 1, 1,1 ,1, 1, 1, 1}, int[] weight = {0, 0, 0, 0},  int[] pattern = {1, 1, 1, 1, 1,1 ,1, 1, 1, 1}, int[] weightTwo = {0, 0, 0, 0}) 

but this is relevant:

int[] patternTwo = {1, 1, 1, 1, 1,1 ,1, 1, 1, 1}, int[] weight = {0, 0, 0, 0},  int[] pattern = {1, 1, 1, 1, 1,1 ,1, 1, 1, 1}, int[] weightTwo = {0, 0, 0, 0}

But I kept getting the same error here:

    main.cs(4265,254): error CS1525: Unexpected symbol '{'
main.cs(4265,255+): error CS1737: Optional parameter cannot precede required parameters
main.cs(4265,254): error CS1525: Unexpected symbol '1'

I've tried declaring the variables a different way:

 int[] patternTwo = new int[10] {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, int[] weight = new int[4] {0, 0, 0, 0},  int[] pattern = new int[10] {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, int[] weightTwo = new int[4] {0, 0, 0, 0}

But it produced errors still:

    main.cs(4265,255+): error CS1736: The expression being assigned to optional parameter 'patternTwo' must be a constant or default value
    main.cs(4265,255+): error CS1736: The expression being assigned to optional parameter 'weight' must be a constant or default value
    main.cs(4265,255+): error CS1736: The expression being assigned to optional parameter 'pattern' must be a constant or default value
    main.cs(4265,255+): error CS1736: The expression being assigned to optional parameter 'weightTwo' must be a constant or default value

I searched through google, but the pages I found were either in a different Programming Language or don't cover what I was asking.

Can I pass an array as arguments to a method with variable arguments in Java?

c# method with unlimited params or method with an array or list?

Passing arrays as arguments (C# Programming Guide)

Passing arrays as arguments in C#

Is it possible for arrays and variables to both be parameters in the same function? If not, are there any alternatives to achieve the same goal?

SilverAtom
  • 11
  • 1
  • 3
    Why don't you pass them in a class or struct, instead? – GrandMasterFlush Oct 26 '21 at 15:43
  • Short answer. yes. alternatively, you can create an object and call that object with out need to pass parameters. You can have as much parameters as you want, but as your program grows this might lead to problems and will become spaghetti codes. you should look at object oriented programming to prevent such problems. – jmag Oct 26 '21 at 15:44
  • 4
    You method declaration is ridiculously long. Don't do that. Just pass an object – Liam Oct 26 '21 at 15:48
  • How do you create an object to pass the parameters? – SilverAtom Oct 26 '21 at 15:51
  • The following may be helpful: https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/types/classes and https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/tutorials/classes – Tu deschizi eu inchid Oct 26 '21 at 16:07
  • 4
    Does this answer your question? [Method parameter array default value](https://stackoverflow.com/questions/12607146/method-parameter-array-default-value) You are actually encountering the problem that you can't give an array a ___default value___. Having array and non-array parameter types is not the issue. – Wyck Oct 26 '21 at 16:10
  • Consider an approach like `static void foo(int[] array = null) { array = array ?? new int[] { 1, 2, 3 }; }` – Wyck Oct 26 '21 at 16:12

1 Answers1

0

This is not a "real" solution but more a workaround. You can set the default array value as null and then use if statements to setup your desired arrays values

        static void MonsterEncounter(float hp = 10, /*[...]*/ int[] patternTwo = null, int[] weight = null,  int[] pattern = null, int[] weightTwo = null){
        if (patternTwo == null) { pattern = new int [] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; }
        //[...]
        if (weightTwo == null) { weightTwo = new int[] { 0, 0, 0, 0 }; }

    }

Otherwise, you can also use overloading.

pverza
  • 16
  • 1