Skip to content

Function default non-primitive values #397

Open

Description

@AzuStar

Hello @yanghuan

There are troubles with exported metadata with default values mentioned here.

However, further tests showed another major issue with the whole default value transpilation.
There is another major problem with the transpiled default value according to the compiled source code.

I have attached examples of C# source and target Lua code that is completely incorrect.
Valid C#

protected Paladin(unit u) : base(u, new HeroStats()
        {
            baseDMG = 1,
            baseARM = 0,
            baseMR = 123,
            baseHP = 0,
            baseMP = 0,
            baseMS = 0
        }, new HeroStats()
        {
            baseDMG = 23,
            baseARM = 13,
            baseMR = 22,
            baseAttackCooldown = 2.15f,
            baseHP = 500,
            baseMP = 120,
            baseMS = 300f,
            regenHP = 1,
            incomingHealing = 0.05f,
        })

Invalid Lua.

__ctor__ = function (this, u)
      local default = NoxRavenUnitAgents.HeroStats()
      default:setbaseDMG(1)
      default:setbaseARM(0)
      default:setbaseMR(123)
      default:setbaseHP(0)
      default:setbaseMP(0)
      default:setbaseMS(0)
      local default = NoxRavenUnitAgents.HeroStats()
      default:setbaseDMG(23)
      default:setbaseARM(13)
      default:setbaseMR(22)
      default:setbaseAttackCooldown(2.15)
      default:setbaseHP(500)
      default:setbaseMP(120)
      default:setbaseMS(300)
      default:setregenHP(1)
      default:setincomingHealing(0.05)
      System.base(this).__ctor__(this, u, default, default)
    end

Not only the default is parsed twice, which is already incorrect behaviour, default variable name is instantly reused by the compiler when transpiling multiple objects in the function. The behaviour occurs not only for ctor constructor.
Functions with predefined primitive values behave properly.
Valid C#

public Status ApplyStatus(NoxUnit source, NoxUnit target, int level, float duration, int initialStacks = 1, int bonusLevel = 0, float bonusDuration = 0, int bonusStacks = 1)
///.......
GhoulEnrage.ApplyStatus(evt.caller, evt.caller, 1, 2, 1, 0, 0, 0);

Valid Lua

class.GhoulEnrage:ApplyStatus1(evt.caller, evt.caller, 1, 2, 1, 0, 0, 0)

If I remove manually defined values in the function ApplyStatus to the following:

GhoulEnrage.ApplyStatus(evt.caller, evt.caller, 1, 2);

Produces valid Lua using predefined primitive values in the ApplyStatus definition.

class.GhoulEnrage:ApplyStatus1(evt.caller, evt.caller, 1, 2, 1, 0, 0, 1)

This is the expected behaviour which only occurs for primitive values.

Current workaround is to use no more than one non-primitive default type in function definition.

Activity

  1. yanghuan commented on Aug 1, 2022

    @yanghuan
    Owner

    thanks, but i can't reproduce this issue

    class A {
      public int baseDMG { 
        get {
          return 0;
        }
        set {
    
        }
      }
    
      public int baseARM  {
        get {
          return 0;
        }
        set {
    
        }
      }
    }
    
    class TestA {
      public TestA(int c, A a, A b) {
      }
    }
    
    class TestB : TestA {
      public TestB(int a) : base(a, new A {
        baseDMG  = 1,
        baseARM  = 2,
      }, new A {
        baseARM = 3,
        baseDMG  = 4,
      }) {
    
      }
    }
    

    will got

    -- Generated by CSharp.lua Compiler
    local System = System
    System.namespace("", function (namespace)
      namespace.class("A", function (namespace)
        local getbaseDMG, setbaseDMG, getbaseARM, setbaseARM
        getbaseDMG = function (this)
          return 0
        end
        setbaseDMG = function (this, value)
        end
        getbaseARM = function (this)
          return 0
        end
        setbaseARM = function (this, value)
        end
        return {
          getbaseDMG = getbaseDMG,
          setbaseDMG = setbaseDMG,
          getbaseARM = getbaseARM,
          setbaseARM = setbaseARM
        }
      end)
    end)
    System.namespace("", function (namespace)
      namespace.class("TestA", function (namespace)
        return {}
      end)
    end)
    System.namespace("", function (namespace)
      namespace.class("TestB", function (namespace)
        local __ctor__
        __ctor__ = function (this, a)
          local default = A()
          default:setbaseDMG(1)
          default:setbaseARM(2)
          local extern = A()
          extern:setbaseARM(3)
          extern:setbaseDMG(4)
          System.base(this).__ctor__(this, a, default, extern)
        end
        return {
          base = function (out)
            return {
              out.TestA
            }
          end,
          __ctor__ = __ctor__
        }
      end)
    end)
    

    it is no problem, Whether I missed anything ?

  2. Drake53 commented on Aug 20, 2022

    @Drake53
    Contributor

    @AzuStar Can you check if you still have this issue with v1.7.17? Maybe it was already fixed but my fork wasn't up-to-date.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions