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# 枚举器接口的问题

尽管在本地环境中具有相同的 launchSettings.json,但使用了不同的 URL

客户端无法反序列化字符串日期时间

是否可以在 C# 中为自动 json 序列化设置自定义合同解析器?

为什么我的解密方法会抛出“要解密的数据长度无效”加密异常

ScrollToAsync .Net Maui 仅在我第二次按下按钮时有效

忽略有关生成查询表达式 EF Core 的 ThenInclude 语句

使用 SQL Server FOR XML 创建可扩展的 XML 文档

ModelState.IsValid 在 ASP.NET Core 6.0 MVC 中总是返回 false

从c#中的给定字符串中删除url

在 C# 中引发 EventArg 具有通用属性的事件

如何根据情况在 OnTriggerExit/Enter 逻辑之间切换?

播放动画 5 次

C# 根据另一个数组对数组的一部分进行排序

Web API 核心 JWT 身份验证不起作用

BsonSerializationException:类的创建者映射有 2 个参数,但没有配置

如何在不指定确切键的情况下读取用户按下的键?

无法向命名元组列表添加值

BitConverter 如何将字节重构为短字节

使用 Plinq AsParallel 时的内存泄漏