Advertisement

help with inserting a KeyCode[] inside an if(input.GetKeyDown, and using a switch statement to scroll between them

Started by October 02, 2020 03:40 PM
6 comments, last by Juliean 4 years, 1 month ago

ok im starting to get the hang of many things in C#/unity and i'm trying to pull off something fairly complicated (for me at least):

i want to create a combat on/off toggle method that activates either when pressing “1” , or “mouse 0”.

i've done that with a switch statement that switches between a bool called inCombat:

switch(inCombat)
	{
	default:
		//call combat script
		inCombat = true;
			break;
		
	case(inCombat):
		//stop calling combat script
		inCombat = false;
			break;
	}	

the thing is, when i'm in combat, i don't want to disable combat by pressing Mouse0, cause that should attack.

so i wanted to make a switch statement inside the if(Input.GetKeyDown), that checks an array of KeyCodes, and then calculates the outcome based on what key was pressed.

now obviously there are much less convoluted ways of doing this (like dividing it into 2 different methods/ scripts), but since i'm fairly new to C# i wanted to figure out if my original idea is even possible, and if so - how.

adding the InitiateCombat() method's code in it's current state.

private void InitiateCombat()
    {
        KeyCode[] combatInitiator =  new KeyCode[]{KeyCode.Alpha1, KeyCode.Mouse0};
        
		if (Input.GetKeyDown(combatInitiator[]))
		{
            switch (KeyCode[])
            {
                case (KeyCode.Alpha1):
                    switch (inCombat)
                    {
                        default:
                            print("Initiating combat. getting me sword out");
                            inCombat = true;
                            print(inCombat);
                            break;

                        case (true):
                            print("already in Combat, getting me sword back");
                            inCombat = false;
                            print(inCombat);
                            break;

                    }
                    break;

                case (KeyCode.Mouse0):
                    if (!inCombat)
                    {
                        print("Initiating combat. getting me sword out");
                        inCombat = true;
                        print(inCombat);
                    }
                    else
                    {
                        return;
                    }
                    break;

            }
        }
    }

thanks guys!

None

You can't use arrays directly like that. If you really wanted to do it that way, you'd need a foreach-loop to go over the elements and process each one by one:

KeyCode[] combatInitiator =  new KeyCode[]{KeyCode.Alpha1, KeyCode.Mouse0};

foreach (var keyCode in combatInitiator)
{        
	if (Input.GetKeyDown(keyCode))
	{
        	switch (keyCode)
        	{
        	}
        }
}

But the way you presented it, that doesn't make any sense. Why create the array just to later switch on the values? Just write the checks one after another and you'll be fine.

Advertisement

that if statements run over each of the array values?

None

SassyPantsy said:
that if statements run over each of the array values?

In my code yes, the foreach-loop runs over all the array-elements, putting each into “keyCode”-variable which can then be checked with the if-statement.

It looks like you are overcomplicating things here.
If I understand correctly, you want to toggle that you are in combat mode by pressing “1” or the left mouse button.
But you want a combination to get out of combat mode.

So the code would be:

if (!inCombat && (Input.GetKeyDown(KeyCode.Alpha1) || Input.GetKeyDown(KeyCode.Keypad1) || Input.GetMouseButtonDown(0)))
	inCombat = true;
else if (inCombat && (Input.GetKeyDown(KeyCode.LeftShift) && Input.GetMouseButtonDown(0)))
	inCombat = false;

Here you have the key combination, and a simple way to toggle inCombat on and off.

Oh, I have assumed here you would be able to use either the “1” on the top of the keyboard or the numeric pad, and that you would use the LeftShift and Left Mouse Button as a combo to get out.

Obviously, code for handling the combat would go into a separate place after this check has been made.

Andy Pett

Juliean said:

You can't use arrays directly like that. If you really wanted to do it that way, you'd need a foreach-loop to go over the elements and process each one by one:

KeyCode[] combatInitiator =  new KeyCode[]{KeyCode.Alpha1, KeyCode.Mouse0};

foreach (var keyCode in combatInitiator)
{        
	if (Input.GetKeyDown(keyCode))
	{
        	switch (keyCode)
        	{
        	}
        }
}

But the way you presented it, that doesn't make any sense. Why create the array just to later switch on the values? Just write the checks one after another and you'll be fine.

AndyPett said:

It looks like you are overcomplicating things here.
If I understand correctly, you want to toggle that you are in combat mode by pressing “1” or the left mouse button.
But you want a combination to get out of combat mode.

So the code would be:

if (!inCombat && (Input.GetKeyDown(KeyCode.Alpha1) || Input.GetKeyDown(KeyCode.Keypad1) || Input.GetMouseButtonDown(0)))
	inCombat = true;
else if (inCombat && (Input.GetKeyDown(KeyCode.LeftShift) && Input.GetMouseButtonDown(0)))
	inCombat = false;

Here you have the key combination, and a simple way to toggle inCombat on and off.

Oh, I have assumed here you would be able to use either the “1” on the top of the keyboard or the numeric pad, and that you would use the LeftShift and Left Mouse Button as a combo to get out.

Obviously, code for handling the combat would go into a separate place after this check has been made.

yeah these still didn't work :/ but i'll just get back to the drawing board i guess. i'll just do something less silly.

None

Advertisement

It is strange if this did not work.

If you want to send me the code, you can send it as a private message (if you don't want to post in the forum), and I can see what's going wrong or how you could do it.

Andy Pett

@AndyPett weird. i see my reply wasn't posted. anyway, it did! i did some code cleaning and wrote it again and it worked. guess the whole switch/if conundrum was kinda pointless.

None

SassyPantsy said:
@AndyPett weird. i see my reply wasn't posted. anyway, it did! i did some code cleaning and wrote it again and it worked. guess the whole switch/if conundrum was kinda pointless.

Glad to hear you got it to work. In case you are still wondering, I'll give you two pointers about what was wrong with your original code, so you maybe don't repeat the same mistakes:

SassyPantsy said:
KeyCode[] combatInitiator = new KeyCode[]{KeyCode.Alpha1, KeyCode.Mouse0};

That is declaring an array. An array is a collection of values. You cannot use an array in place where you would normally use a value (like calling a function Input.IsKeyDown() inside a switch), but you eigther need to access an element (combatInitiator[0]), or use a loop to go over all the values.

SassyPantsy said:
switch (KeyCode[])

Furthemore, in this case you try to switch on the type KeyCode[] (read: Array of KeyCodes). You cannot switch on a type, but you need a single value, in this case you need a variable of type KeyCode ((and again, not an array).

This topic is closed to new replies.

Advertisement