• 🏆 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] Table + StringHash problem

Status
Not open for further replies.
Level 15
Joined
Nov 30, 2007
Messages
1,202
I'm using a string hash as an Id to save commands that are to be executed later. But because that can generate negative numbers or very large numbers it's not gonna work. Should i use ordinary hashtables here or another solution?

JASS:
library CommandUtils initializer Init uses Table 
   
    globals 
        private constant string     PREFIX              = "-"
        private constant integer    MAX_WORDS           = 120
       
        public PlayerChat array lastPlayerChat[24]
       
        private Table table 
    endglobals 
   
    struct PlayerChat
        public string chatStr
        public string subStr
        public string array word[MAX_WORDS]
        public integer wordCount 
        public integer length
       
        method update takes string chat returns nothing 
            local string char = ""
            local integer i = 0
            loop
                exitwhen i == .wordCount
                set .word[i] = null
                set i = i + 1
            endloop
            set .length = StringLength(chat) 
            set .chatStr = chat
            set .wordCount = 0
            set i = 0
            loop
                exitwhen i > .length
                set char = SubString(chat, i, i + 1)
                if char != " "  then 
                    set .word[.wordCount] = .word[.wordCount] + char
                else
                    set .wordCount = .wordCount + 1
                endif
                set i = i + 1
            endloop
            set .wordCount = .wordCount + 1
        endmethod 
       
        method getWord takes integer index returns string 
            if (index < 0 or index >= .wordCount) then 
                return null
            endif
            return .word[index]
        endmethod 
    endstruct
   
    function RegisterCommand takes trigger trg, string cmd returns nothing 
        set table.trigger[StringHash(cmd)] = trg
    endfunction 
   
    function DeregisterCommand takes string cmd returns nothing 
        call table.trigger.remove(StringHash(cmd))
    endfunction 
   
    function GetLastPlayerChat takes integer playerId returns PlayerChat
        return lastPlayerChat[playerId]
    endfunction
   
    private function Main takes nothing returns nothing 
        local trigger t
        local integer i
        local integer cid
        local integer pid = GetPlayerId(GetTriggerPlayer())
        call lastPlayerChat[pid].update(GetEventPlayerChatString())
        set cid = StringHash(lastPlayerChat[pid].word[0])
        if (not(table.trigger.has(cid))) then 
            return
        endif 
        set lastPlayerChat[pid].subStr = SubString(lastPlayerChat[pid].chatStr, StringLength(lastPlayerChat[pid].word[0]), lastPlayerChat[pid].length)
        set t = table.trigger[cid]
        if TriggerEvaluate(t) then
            call TriggerExecute(t)
        endif
        set t = null
    endfunction
    private function Init takes nothing returns nothing 
        local trigger t = CreateTrigger()
        local player p
        local integer i = 0
        loop
            set p = Player(i)
            if (GetPlayerSlotState(p) == PLAYER_SLOT_STATE_PLAYING and GetPlayerController(p) == MAP_CONTROL_USER) then 
                call TriggerRegisterPlayerChatEvent(t, p, PREFIX, false)
                set lastPlayerChat[i] = PlayerChat.create()
            endif
            set i = i + 1
            exitwhen i == bj_MAX_PLAYER_SLOTS
        endloop 
        call TriggerAddAction(t, function Main)
        set table = Table.create()
        set p = null
        set t = null
    endfunction
endlibrary
 
Last edited:
Level 15
Joined
Nov 30, 2007
Messages
1,202
Ops deleted my message. This is solved anyway for late comers. The only problem i have is using this global in another scope:

public PlayerChat array lastPlayerChat[24] For example this line set x = S2R(lastPlayerChat[0].word[1])informs me that the . syntax is not allowed if im not in the same scope as the library it was declared.
 
Status
Not open for further replies.
Top