• 🏆 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!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

Array in the store

Level 6
Joined
Aug 26, 2016
Messages
109
Here is a small piece of code with a button and a function to click on the button to purchase an item, how? You can make an array of about 15 items so as not to create a click function for each button with the issuance of a pre-prepared item.


JASS:
private function click takes nothing returns nothing
    if GetPlayerState(GetOwningPlayer(udg_UnitBody[GetConvertedPlayerId(GetTriggerPlayer())]), PLAYER_STATE_RESOURCE_GOLD) >= 80 then 
     call SetPlayerStateBJ( GetOwningPlayer(udg_UnitBody[GetConvertedPlayerId(GetTriggerPlayer())]), PLAYER_STATE_RESOURCE_GOLD, ( GetPlayerState(GetOwningPlayer(udg_UnitBody[GetConvertedPlayerId(GetTriggerPlayer())]), PLAYER_STATE_RESOURCE_GOLD) - 80 ) )
    call UnitAddItemByIdSwapped(udg_Items[1],udg_UnitBody[GetConvertedPlayerId(GetTriggerPlayer())] )
    else
    call DisplayTextToForce( GetForceOfPlayer(GetOwningPlayer(udg_UnitBody[GetConvertedPlayerId(GetTriggerPlayer())])), "Нету деняг!!" )
    endif
endfunction

private function initSs takes integer id returns nothing
    local trigger Click = CreateTrigger()
    local trigger Click2 = CreateTrigger()
    local player p = GetOwningPlayer(GetTriggerUnit())
    set gameUIS = BlzGetOriginFrame(ORIGIN_FRAME_GAME_UI, 0)
   
    set butS[1] = BlzCreateSimpleFrame("Pistol", gameUIS, 0)
    call BlzFrameSetAbsPoint(butS[1], FRAMEPOINT_CENTER, 0.1, 0.5)
    call BlzTriggerRegisterFrameEvent(Click, butS[1], FRAMEEVENT_CONTROL_CLICK)
    call TriggerAddAction(Click, function click)
  
    set text1[1] = BlzCreateFrame("PistolText1", gameUIS, 0, 0)
    call BlzFrameSetAbsPoint(text1[1], FRAMEPOINT_CENTER, 0.1, 0.47)

    set text2[1] = BlzCreateFrame("PistolText2", gameUIS, 0, 0)
    call BlzFrameSetAbsPoint(text2[1], FRAMEPOINT_CENTER, 0.1, 0.46)
    
        call BlzFrameSetVisible(butS[1], false)
        call BlzFrameSetVisible(text1[1], false)
        call BlzFrameSetVisible(text2[1], false)
    
 if GetLocalPlayer() == p then
        call BlzFrameSetVisible(butS[1], false)
        call BlzFrameSetVisible(text1[1], false)
        call BlzFrameSetVisible(text2[1], false)
        endif
   
   
        set  Click = null
        set  Click2 = null
        set p = null
endfunction
 

Uncle

Warcraft Moderator
Level 65
Joined
Aug 10, 2018
Messages
6,662
Use a For Loop and a framehandle Array to create and track the buttons:
vJASS:
globals
    framehandle array buttons
endglobals

function CreateButtons take nothing return nothing
    local integer i = 0
    loop
        set buttons[i] = BlzCreateSimpleFrame(frameName[i], gameUIS, 0)
        // manage the frame and it's events
        set i = i + 1
        exitwhen i > 15
    endloop
endfunction
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,223
If you can get the button that was clicked in the the click event response function then you can use that button value to lookup the context associated with the button, such as an array index by performing a linear search through an array. This is how UI event handlers in languages like Python, C# and Java work and allows 1 function to handle the logic of multiple different UI elements. This is how the classic dialog buttons work in Warcraft III. I do not know if the new UI natives support this though...

With Lua it might be possible to create nameless function instances with different states using a loop that you can then bind to each individual button. In the code this is represented by a single function, but at runtime it generates multiple different functions with their own states. In theory using JASS/vJASS this is also possible, but the state management is a lot more difficult and might end up relying on mapping state to a trigger with each button getting its own unique trigger instance.
 
Top