Page 1 of 1

How to set a building-unit into placement mode

Posted: 01 Jan 2018, 10:36
by PicassoCT
Hi,
i have a unit who builds things selected in the gui. I now want to select a building for placement. In such a way that the ghosted building becomes the mouse cursor.
How do i do that?

Spring.SetCommandActive seemed to be the candidate for that at first, but then i realized its just for taking commands into action from the queue.

Thanks for the help

Re: How to set a building-unit into placement mode

Posted: 03 Jan 2018, 19:13
by sprunk
SetCommandActive
I assume you're talking about SetActiveCommand. How are you using it?

This shouldn't work:

Code: Select all

Spring.SetActiveCommand(-UnitDefNames.peewee.id)
Try this instead:

Code: Select all

local cmdIndex = Spring.GetCmdDescIndex(-UnitDefNames.peewee.id)
Spring.SetActiveCommand(cmdIndex)
Or:

Code: Select all

Spring.SetActiveCommand("buildunit_peewee")

Re: How to set a building-unit into placement mode

Posted: 03 Jan 2018, 23:39
by PicassoCT

Code: Select all

function BuildHandler(button)
  local alt, ctrl, meta, shift = Spring.GetModKeyState()
  local _, _, lmb, mmb, rmb, outsideSpring = Spring.GetMouseState()
  local opt = {}
  if alt   then push(opt,"alt")   end
  if ctrl  then push(opt,"ctrl")  end
  if meta  then push(opt,"meta")  end
  if shift then push(opt,"shift") end

  if button==1 then
	 builderDefID= Spring.GetUnitDefID(facs[openedMenu+1].unitID)
	 if builderDefID and UnitDefs[builderDefID].isFactory then
		Spring.GiveOrderToUnit(facs[openedMenu+1].unitID, -(facs[openedMenu+1].buildList[pressedBOpt+1]),{},opt)
	 else --select building and select build
		Spring.SelectUnitArray({[1]=facs[openedMenu+1].unitID})		
	--TODO Find a solution here	
	--Spring.SetActiveCommand(-(facs[openedMenu+1].buildList[pressedBOpt+1]))
	--	Spring.GiveOrderToUnit(facs[openedMenu+1].unitID,  -facs[openedMenu+1].buildList[pressedBOpt+1], {},  opt)
	
	 end
    Spring.PlaySoundFile(sound_queue_add, 0.95)
  elseif button==3 then
    push(opt,"right")
    Spring.GiveOrderToUnit(facs[openedMenu+1].unitID, -(facs[openedMenu+1].buildList[pressedBOpt+1]),{},opt)
    Spring.PlaySoundFile(sound_queue_rem, 0.97)
  end
end
Solution did not work, added example code for a better problem description- which was part of the buildbar widget.

Re: How to set a building-unit into placement mode

Posted: 04 Jan 2018, 01:11
by Silentwings
Perhaps the ActionCommand function in https://springrts.com/wiki/Chili:Example_3 answers your question?

Re: How to set a building-unit into placement mode

Posted: 05 Jan 2018, 01:08
by Google_Frog
This is how the ZK menu deals with button clicks (or key presses for hotkeys). https://github.com/ZeroK-RTS/Zero-K/blo ... #L555-L590

It handles factory queues and terraform. What you want should be this cut down version:

Code: Select all

local function ClickFunc(mouse, cmdID)
    local left, right = mouse == 1, mouse == 3
    local alt, ctrl, meta, shift = Spring.GetModKeyState()
    
    local index = Spring.GetCmdDescIndex(cmdID)
    if index then
        Spring.SetActiveCommand(index, mouse or 1, left, right, alt, ctrl, meta, shift)
    end
end
The cmdID is just -UnitDefID.

Re: How to set a building-unit into placement mode

Posted: 05 Jan 2018, 12:13
by PicassoCT
Thanks - you all where right - it is spring.SetActivCommand- i was just using it wrong