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: 10238
Joined: 24 Jan 2006, 21:12

How to set a building-unit into placement mode

Post by PicassoCT » 01 Jan 2018, 10:36

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
0 x

sprunk
Posts: 84
Joined: 29 Jun 2015, 07:36

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

Post by sprunk » 03 Jan 2018, 19:13

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")
0 x

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

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

Post by PicassoCT » 03 Jan 2018, 23:39

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.
0 x

User avatar
Silentwings
Moderator
Posts: 3593
Joined: 25 Oct 2008, 00:23

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

Post by Silentwings » 04 Jan 2018, 01:11

Perhaps the ActionCommand function in https://springrts.com/wiki/Chili:Example_3 answers your question?
0 x

Google_Frog
Moderator
Posts: 2440
Joined: 12 Oct 2007, 09:24

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

Post by Google_Frog » 05 Jan 2018, 01:08

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.
2 x

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

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

Post by PicassoCT » 05 Jan 2018, 12:13

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

Post Reply

Return to “Lua Scripts”