How to set a building-unit into placement mode

How to set a building-unit into placement mode

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
User avatar
PicassoCT
Journeywar Developer & Mapper
Posts: 10450
Joined: 24 Jan 2006, 21:12

How to set a building-unit into placement mode

Post 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
sprunk
Posts: 100
Joined: 29 Jun 2015, 07:36

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

Post 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")
User avatar
PicassoCT
Journeywar Developer & Mapper
Posts: 10450
Joined: 24 Jan 2006, 21:12

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

Post 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.
User avatar
Silentwings
Posts: 3720
Joined: 25 Oct 2008, 00:23

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

Post by Silentwings »

Perhaps the ActionCommand function in https://springrts.com/wiki/Chili:Example_3 answers your question?
Google_Frog
Moderator
Posts: 2464
Joined: 12 Oct 2007, 09:24

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

Post 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.
User avatar
PicassoCT
Journeywar Developer & Mapper
Posts: 10450
Joined: 24 Jan 2006, 21:12

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

Post by PicassoCT »

Thanks - you all where right - it is spring.SetActivCommand- i was just using it wrong
Post Reply

Return to “Lua Scripts”