[英]Unity c# need help understanding switch statement
I'm trying to understand switch statements and conditionals with them.我试图用它们来理解 switch 语句和条件。
Here is the script currently.这是当前的脚本。
Unit thisUnit; <- my reference to the unit stats it has.
int captureStartingHealth;
int captureStartingMana
void Start()
{
captureStartingHealth = thisUnit.currentHP;
// In start it's the same as thisUnit.maxHP;
captureStartingMana = thisUnit.currentMP;
// In start it's the same as thisUnit.maxMP;
}
void AssignmentSwitchStatement()
{
switch (captureStartingHealth)
{
case int captureStartingHP when captureStartingHealth < (float)captureStartingHealth:
behaviour = Behaviour.SUPPORT;
roll = Random.Range(0, 101);
IncrementSupportRollChances();
break;
default:
roll = Random.Range(0, 101);
DefaultRollChances();
break;
}
}
What is the correct syntax for constructing a way to reference when my captureStartingHealth variable is less than 40 for example.例如,当我的 captureStartingHealth 变量小于 40 时,构建引用方式的正确语法是什么。 When I made the syntax it doesn't allow me to simply use the integer 'captureStartingHealth' instead.. it seems to only work if I make a new integer variable for the case.
当我创建语法时,它不允许我简单地使用整数“captureStartingHealth”代替..它似乎只有在我为案例创建一个新的整数变量时才有效。
So, why is it I can't use my captureStartingHealth integer like this?那么,为什么我不能像这样使用我的 captureStartingHealth 整数?
switch (captureStartingHealth) // example switch
{
case captureStartingHealth when captureStartingHealth < (float)thisUnit.maxHP:
//execute code
break;
}
"
Instead it requires me to make a new integer相反,它需要我创建一个新整数
case int a when a < conditional
but how do I know when this new int has reached the conditional as true?但是我怎么知道这个新的 int 什么时候达到了条件为真呢? Like I don't understand how this int "a" works for the switch since it's a local integer that isn't assigned anything right?
就像我不明白这个 int "a" 是如何用于 switch 的,因为它是一个没有分配任何东西的本地整数吗? So for me it's like saying..
所以对我来说就像说..
case int a (0) when a (0) < (float)thisUnit.maxHP: // execute code case int a (0) 当 a (0) < (float)thisUnit.maxHP: // 执行代码
What am I doing wrong here?我在这里做错了什么? Probably many things but I honestly don't know why the case needs a new integer instead of using the value I want to compare for the conditional statement.
可能有很多事情,但老实说我不知道为什么这个案例需要一个新的整数,而不是使用我想为条件语句比较的值。
That first switch
code makes absolutely no sense at all!第一个
switch
代码完全没有意义!
in在
switch(captureStartingHealth)
{
case int captureStartingHP:
...
break;
default:
...
break;
}
you make a type check via Pattern Matching and check if captureStartingHealth
is an int
.您通过模式匹配进行类型检查并检查
captureStartingHealth
是否为int
。 Basically like doing基本上喜欢做
if(captureStartingHealth is int captureStartingHP)
{
...
}
else
{
...
}
which kind of is equal to doing哪种等于做
if(captureStartingHealth.GetType().IsAssignableTo(typeof(int)))
{
int captureStartingHP = (int)captureStartingHealth;
...
}
else
{
...
}
But since captureStartingHealth
is an int
anyway this is always true
and just unnecessary overhead.但是由于
captureStartingHealth
是一个int
无论如何这总是true
并且只是不必要的开销。
Then you anyway throw away the type casted captureStartingHP
and compare然后你无论如何扔掉类型转换
captureStartingHP
并比较
captureStartingHealth < (float)captureStartingHealth
Here are two weird things:这里有两件奇怪的事情:
float
good for when comparing two int
values?int
值时,什么是float
好?a < a
will never be true ... you have both times the same variable! a < a
永远不会是真的......你有两次相同的变量! As was mentioned before if the only check you actually want to do is comparing the two values you rather want to use a simple if - else
如前所述,如果您真正想要做的唯一检查是比较两个值,您宁愿使用简单的
if - else
// you do this in any case so rather pull it out of the checks in order to write/maintain it only once
roll = Random.Range(0, 101);
if(captureStartingHealth < thisUnit.maxHP)
{
behaviour = Behaviour.SUPPORT;
IncrementSupportRollChances();
}
else
{
DefaultRollChances();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.