• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[Solved] How to stop a peasant from building?

Status
Not open for further replies.
Level 15
Joined
Dec 19, 2020
Messages
286
There is not an easy way to see if a peasant has a building order when they are moving. The only way i know how to see if a peasant has been ordered to build something is the following: if you click on an individual peasant, you can see if the building button is highlighted. If it is highlighted, the peasant (or any other worker) is ordered to build a structure.
If you want your worker to stop, just give him any other order (move, patrol, etc.)
 
Level 30
Joined
Feb 18, 2014
Messages
3,623
Create three variables:

BuildTrigger = Trigger (Array)
i = Integer
t = Trigger

Now, create a trigger called "Prevent Build", convert it to custom text and copy paste this script in it:
JASS:
function BuildActions takes nothing returns nothing
    set udg_i = 0
    set udg_t = GetTriggeringTrigger()
    loop
        exitwhen udg_i>11
        if udg_t == udg_BuildTrigger[udg_i] then
            call DisplayTimedTextToPlayer(Player(udg_i), 0, 0, 60, "You cannot build")
            call ForceUICancelBJ( Player(udg_i) )
            return
        endif
        set udg_i = udg_i + 1
    endloop
endfunction

function InitTrig_Prevent_Build takes nothing returns nothing
    local integer i = 0
    loop
        exitwhen i>11
        set udg_BuildTrigger[i] = CreateTrigger()
        call TriggerAddAction(udg_BuildTrigger[i], function BuildActions)
        if GetLocalPlayer() == Player(i) then
            call TriggerRegisterGameEvent(udg_BuildTrigger[i], EVENT_GAME_BUILD_SUBMENU)
        endif
        set i = i + 1
    endloop
endfunction
Whenever the worker open up the build menu, it will close automatically.
 
Level 5
Joined
Mar 24, 2020
Messages
80
Hi Stan

I spent quite a lot of time trying to solve this issue but there didn't seem to be a simple way to just stop the construction.

I had to resort to this:

  • Stop building
    • Events
      • Unit - A unit Begins construction
    • Conditions
      • (Unit-type of (Constructing structure)) Equal to Building
    • Actions
      • Unit - Kill (Constructing structure)
      • Player - Add 250 to (Owner of (Triggering unit)).Current lumber
Obviously add any extra conditions you may need. However, if there are multiple buildings obv you will need to do this trigger a few times with variants.

Not the most streamlined way but in terms of a GUI solution, it works.
 
Level 30
Joined
Feb 18, 2014
Messages
3,623
JASS:
function CancelBuild takes nothing returns nothing
    if IsUnitSelected(GetEnumUnit(), Player(udg_i)) == true and UnitHasBuffBJ(GetEnumUnit(), 'BUfa') == true then //'BUfa' = Frost Armor Buff
        call ForceUICancelBJ( Player(udg_i) )
        call DisplayTimedTextToPlayer(Player(udg_i), 0, 0, 60, "This worker cannot build")
    else
    endif
endfunction

function BuildActions takes nothing returns nothing
    set udg_i = 0
    set udg_t = GetTriggeringTrigger()
    loop
        exitwhen udg_i>11
        if udg_t == udg_BuildTrigger[udg_i] then
            set bj_wantDestroyGroup = true
            call ForGroupBJ( GetUnitsOfPlayerAndTypeId(Player(udg_i), 'hpea'), function CancelBuild) //'hpea' = Peasant
            return
        endif
        set udg_i = udg_i + 1
    endloop
endfunction

function InitTrig_Prevent_Build takes nothing returns nothing
    local integer i = 0
    loop
        exitwhen i>11
        set udg_BuildTrigger[i] = CreateTrigger()
        call TriggerAddAction(udg_BuildTrigger[i], function BuildActions)
        if GetLocalPlayer() == Player(i) then
            call TriggerRegisterGameEvent(udg_BuildTrigger[i], EVENT_GAME_BUILD_SUBMENU)
        endif
        set i = i + 1
    endloop
endfunction
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,285
This might be a silly question, but what's the correct way to detect if a peasant is ordered to build ANY building and order him to stop?
The peasant gets issued a point target order of the building's unit type (4 character raw data value). This can be detected like any point target order and also cancelled like any point target order. Cancelling the order requires that the unit be pause cycled as the event fires before the actual order is issued so issuing "stop" or other approaches would require being done in a separate trigger thread to occur in the correct order.

This will not detect if a peasant is ordered to assist construction of an existing building. In this case the peasant is not building the building, but rather repairing an existing building to make it construct faster.
 
JASS:
function CancelBuild takes nothing returns nothing
    if IsUnitSelected(GetEnumUnit(), Player(udg_i)) == true and UnitHasBuffBJ(GetEnumUnit(), 'BUfa') == true then //'BUfa' = Frost Armor Buff
        call ForceUICancelBJ( Player(udg_i) )
        call DisplayTimedTextToPlayer(Player(udg_i), 0, 0, 60, "This worker cannot build")
    else
    endif
endfunction

function BuildActions takes nothing returns nothing
    set udg_i = 0
    set udg_t = GetTriggeringTrigger()
    loop
        exitwhen udg_i>11
        if udg_t == udg_BuildTrigger[udg_i] then
            set bj_wantDestroyGroup = true
            call ForGroupBJ( GetUnitsOfPlayerAndTypeId(Player(udg_i), 'hpea'), function CancelBuild) //'hpea' = Peasant
            return
        endif
        set udg_i = udg_i + 1
    endloop
endfunction

function InitTrig_Prevent_Build takes nothing returns nothing
    local integer i = 0
    loop
        exitwhen i>11
        set udg_BuildTrigger[i] = CreateTrigger()
        call TriggerAddAction(udg_BuildTrigger[i], function BuildActions)
        if GetLocalPlayer() == Player(i) then
            call TriggerRegisterGameEvent(udg_BuildTrigger[i], EVENT_GAME_BUILD_SUBMENU)
        endif
        set i = i + 1
    endloop
endfunction
uhhh @Warseeker , also my Engineer unit can build buildings. My mistake. You now made it to check the peasant instead of any unit that can build


EDIT: It's OK, I added call ForGroupBJ( GetUnitsOfPlayerAndTypeId(Player(udg_i), 'engi'), function CancelBuild)
 
Last edited:
Status
Not open for further replies.
Top