• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[vJASS] Overridng functions/methods?

Status
Not open for further replies.
Level 15
Joined
Nov 30, 2007
Messages
1,202
I have this function in one of my libraries and I want to basically override in another library that works as an extension to the first, that is, adds extra features. The difference between the two is that the first one does now save any data, whilst the other one would.

function SetUnitFullName takes unit u, string firstname, string lastname, string unitname returns nothing

How would I go about it?

JASS:
library RandomNaming uses Table

    globals
        /* Configurables */
        public boolean keepNormalNames     = true        
        public boolean colorHeroes         = true        
        public string color                = "|cffffcc00"
    endglobals
 
    function SetUnitFullName takes unit u, string firstname, string lastname, string unitname returns nothing
        if IsUnitType(u, UNIT_TYPE_HERO) then
            call BlzSetUnitName(u, unitname)
            if colorHeroes then
                call BlzSetHeroProperName(u, color + firstname + " " + lastname + "|r")
            else
                call BlzSetHeroProperName(u, firstname + " " + lastname)
            endif
        else
            if StringLength(firstname) > 0 or StringLength(lastname) > 0 then
                call BlzSetUnitName(u, color + firstname + " " + lastname + "|r|n" + unitname)
            else
                call BlzSetUnitName(u, unitname)
            endif
        endif
    endfunction
 
    function GetUnitNamePool takes unit u returns TypeNamePool
        return TypeNamePool.typesNameTable[GetUnitTypeId(u)]
    endfunction
 
    function GiveUnitRandomName takes unit u returns boolean
        local TypeNamePool namepool = TypeNamePool.typesNameTable[GetUnitTypeId(u)]
        if namepool != 0 then
            call SetUnitFullName(u, namepool.getRandomFirstName(), namepool.getRandomLastName(), GetUnitName(u))
            return true
        endif
        return false
    endfunction
 
    struct TypeNamePool
        private Table types
        private integer size
        private NamePool firstNamePool
        private NamePool lastNamePool
    
        static Table typesNameTable
    
        static method onInit takes nothing returns nothing
            set typesNameTable = Table.create()
        endmethod
    
        method getRandomFirstName takes nothing returns string
            return firstNamePool.getRandom()
        endmethod
    
        method getRandomLastName takes nothing returns string
            return lastNamePool.getRandom()
        endmethod
    
        static method create takes NamePool firstPool, NamePool lastPool returns thistype
            local thistype this = .allocate()
            set this.types = Table.create()
            set this.firstNamePool = firstPool
            set this.lastNamePool = lastPool
            set this.size = 0
            return this
        endmethod
    
        private method indexOf takes integer unitTypeId returns integer
            local integer i = 0
            loop
                exitwhen i == .size
                if .types[i] == unitTypeId then
                    return i
                endif
                set i = i + 1
            endloop
            return -1
        endmethod
    
        method remove takes integer unitTypeId returns boolean
            if thistype.typesNameTable[unitTypeId] == this then
                call thistype.typesNameTable.remove(unitTypeId)
                set .size = .size - 1
                set .types[.indexOf(unitTypeId)] = .types[.size]
                call .types.remove(.size)
                return true
            endif
            debug call BJDebugMsg("error - TypeNames.remove: Unit Type does not belong to this name pool.")
            return false
        endmethod
    
        method add takes integer unitTypeId returns thistype
            local thistype prev = thistype.typesNameTable[unitTypeId]
            if prev != 0 then
                call prev.remove(unitTypeId)
            endif
            set thistype.typesNameTable[unitTypeId] = this
            set .types[.size] = unitTypeId
            set .size = .size + 1
            return this
        endmethod
    
        method destroy takes nothing returns nothing
            call types.destroy()
            call .deallocate()
        endmethod
    endstruct
 
    struct NamePool
        private Table names
        private integer size
    
        static method create takes nothing returns thistype
            local thistype this = .allocate()
            set this.names = Table.create()
            set this.size = 0
            return this
        endmethod
    
        method getRandom takes nothing returns string
            debug if .size <= 0 then
                debug call BJDebugMsg("error - NamePool.getRandom: No names to draw from.")
                debug return ""
            debug endif
            return .names.string[GetRandomInt(0, .size - 1)]
        endmethod
    
        method add takes string name returns thistype
            set .names.string[.size] = name
            set .size = .size + 1
            return this
        endmethod
    
        method destroy takes nothing returns nothing
            local integer i = 0
            loop
                exitwhen i == .size
                set .names.string[i] = null
                set i = i + 1
            endloop
            call names.destroy()
            call .deallocate()
        endmethod
    endstruct
endlibrary


Here is the extended library
JASS:
library UnitNameUtil uses RandomNaming

    struct UnitName
        string firstName
        string lastName
        string uname
    
        static method create takes unit u, string firstname, string lastname returns thistype
            local thistype this = .allocate()
            set this.firstName = firstname
            set this.lastName = lastname
            set
            return this
        endmethod
    endstruct
 
    /* This function is the one that i want to have override the previous one.*/
    function SetUnitFullName takes unit u, string firstname, string lastname, string unitname returns nothing
 
    endfunction
 
    function SetUnitRandomNameFromPools takes unit u, NamePool firstNames, NamePool lastNames return nothing
 
    endfunction

    function SetUnitNormalName takes unit u, string name returns nothing
 
    endfunction
 
    function SetUnitProperName takes unit u, string firstname, string lastname returns nothing
 
    endfunction

    function SetUnitFirstName takes unit u, string firstname returns nothing
 
    endfunction
 
    function SetUnitLastName takes unit u, string lastname returns nothing
 
    endfunction
 
    function GetUnitProperName takes unit u returns string
        return ""
    endfunction
 
    function GetUnitFirstName takes unit u returns string
 
    endfunction
 
    function GetUnitNormalName takes unit u returns string
 
    endfuncton
 
    function DestroyUnitName takes unit u returns nothing
 
    endfunction
 
endlibrary

I'm thinking that you either write a textmacro in the parent that checks if a library exists and then you write the optional code there or something else...

Could someone please show me how it's done?

An alternative would also be to have everything in the same library and have a static constant to determine if the data should be saved to the unit or not. Which option should I go with?
 
Status
Not open for further replies.
Top