I'm rather new to coding in C# and am trying to teach myself more. I've been trying to work on a simple rpg with stats from levels in RPG and have gotten stuck with trying to apply the damage to an enemy based on my characters stats.

When I thought I had solved the issue by splitting my stats script for the player into a second one for the enemy units, I unfortunately ran into the issue where the left-hand side of the assignment needing to be a variable property or indexer, and no matter how much I search for solutions I'm stumped. Would anyone be able to take a look at my scripts and point out any glaring errors I've made?

Please and thank you!

   public void TakePhysicalDamage()
    {
    defaultStats.GetPhysicalDamage()-= armor; //This is the offending line
    physicalDamage = Mathf.Clamp(physicalDamage, 0, int.MaxValue);
    health -= (int)Math.Round(physicalDamage);   
    
    if(health <= 0)
        {
        health = 0;
        Die();
    }
}
void Die()
{
    {
        playerLevel.AddExperience(experience_reward);
    }
    Destroy(gameObject);
}

}

Here is the playerstats (defaultstats) script just for reference where I'm trying to get the physical damage from

[SerializeField] float strength = 5f; [SerializeField] float physicalDamage = 5f;

  public float GetPhysicalDamage()
  {
    return physicalDamage += strength;
  }

Sorry if this seems super basic, but please take a look if you're bored!

推荐答案

You are trying to modify a function:

defaultStats.GetPhysicalDamage()-= armor;

but you can't, because GetPhysicalDamage only returns damage, it isn't setup as a property that would allow you to modify it (and also don't do that!)

public float GetPhysicalDamage()
{
  return physicalDamage += strength;
}

Instead, it looks like you have a variable physicalDamage that you should be using instead, like:

public void TakePhysicalDamage()
{
    physicalDamage = defaultStats.GetPhysicalDamage() - armor; //This is the offending line
    physicalDamage = Mathf.Clamp(physicalDamage, 0, int.MaxValue);
    health -= (int)Math.Round(physicalDamage);   
    
    if(health <= 0)
    {
        health = 0;
        Die();
    }
}

Actually, on closer review, I think you're probably not doing what you think you're doing. It looks like physicalDamage is supposed to be the base damage you're doing, but when you have a line like the following from GetPhysicalDamage():

return physicalDamage += strength;

if physicalDamage is 5 and strength is 5, then the first time you call GetPhysicalDamage() you get 10. BUT what you're doing is adding the strength to physical damage and storing that value as the new physical damage with the += operator, such that the next time you call GetPhysicalDamage() the physicalDamage variable is now 10 (from the previous call) and now it returns 15. Then 20, 25, etc.

I think what you want instead is just the sum of physicalDamage and strength, like:

return physicalDamage + strength;

But if this is the case then I think the variable name physicalDamage is misleading. I would personally prefer something like basePhysicalDamage and then you can have a property like:

public int PhysicalDamage => basePhysicalDamage + strength;

I would especially recommend doing this because later in your code, where you're having the issues now, you are modifying the physicalDamage variable on lines like:

physicalDamage = Mathf.Clamp(physicalDamage, 0, int.MaxValue);

This is also confused because it looks like you're trying to GetPhysicalDamage and modify that with armor, but when you call GetPhysicalDamage and armor you're getting them from the same (local) source, so it would be either the player's physical damage dealt to themselves with the player's armor, or it would be the mob's physical damage dealt to themselves with their armor.

I would pass the damage as an argument so that you can send damage from one thing to another, like:

public void TakePhysicalDamage(int damage)
{
    damage -= armor;
    damage = Mathf.Clamp(damage, 0, int.MaxValue);
    health -= (int)Math.Round(damage);
    if(health <= 0)
    {
        health = 0;
        Die();
    }
}

Csharp相关问答推荐

C#EF Core WHERE IN LINQ FROM LIST WITH.CONTAINS不返回任何内容

注册所有IMediatR类

为基本审计设置Audit.EntityFramework.Core

当我没有此令牌时,为什么语法报告EOF错误?

C#动态设置ServerReport报表参数

我可以查看我们向应用程序洞察发送了多少数据吗?

WPF DataGrid文件名列,允许直接输入文本或通过对话框按键浏览

如何从另一个类的列表中按ID取值

使用ASP.NET MVC for Lemon Squeezy X-Signature创建散列

用于请求用户返回列表的C#Google API

将C#类导入到PowerShell

如何使用类似于[SELECT*FROM&Q;&Q;WHERE&Q;]SQL查询的System.Data.Entity创建查询?

C#如何获取字符串中引号之间的文本?

当`JToken?`为空时?

SignalR跨域

如何在C#中反序列化Java持续时间?

如何根据分割文本的块数来计算文本的大小?

在c#中,使用Okta和Blazor时,LocalReDirect()陷入循环,出现错误&请求太多.

异步等待,如何在Windows窗体中使用它们?

奇怪的字符串.StartsWith和.包含从.NET Framework 4.8升级到.NET 6后的行为