Gadget based buttons

Gadget based buttons

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

Moderator: Moderators

Post Reply
Voidranaut
Posts: 47
Joined: 20 Dec 2009, 21:16

Gadget based buttons

Post by Voidranaut »

currently I am trying to understand what it takes to add custom command buttons to a units menue

so far I am failry certain that I understand almost nothing of what is envolved in praforming this process

but I have been looking at unit_mex_upgrade as a refrance and trying to make my own random button with what I see there

the following script is what I have created so far

Code: Select all

function gadget:GetInfo()
   return {
      name = "CUTests",
      desc = "Gadget testing ground (only tests run here will do real scripts from what I learn here)",
      author = "zwzsg (and then horribly ruined by Voidranaut)",
      date = "not sure",
      version = "1.0",
      license = "Public Domain",
      layer = 0,
      enabled = true
   }
end
local echo = Spring.Echo
local message_identifier = gadget:GetInfo().name..": "
local once = 1
local height = 0
local increase = 0
local x1 = 7510
local z1 = 2600
local x2 = 7010
local z2 = 2800
local FindUnitCmdDesc = Spring.FindUnitCmdDesc

local CMD_UPGRADEMEX = 38274

local Happy = { 
  id      = CMD_HAPPY, 
  type    = CMDTYPE.ICON_UNIT_OR_AREA, 
  name    = 'Happy', 
  cursor  = 'Attack', 
  action  = 'upgrademex', 
  tooltip = 'Hammberger time', 
  hidden  = false, 
  params  = {} 
} 

if (gadgetHandler:IsSyncedCode()) then--SYNCED

  function gadget:Initialize()

	for _,u in ipairs(Spring.GetAllUnits()) do 			
			local insertID = 
   			FindUnitCmdDesc(u, CMD.CLOAK)      or 
    			FindUnitCmdDesc(u, CMD.ONOFF)      or 
    			FindUnitCmdDesc(u, CMD.TRAJECTORY) or 
    			FindUnitCmdDesc(u, CMD.REPEAT)     or 
    			FindUnitCmdDesc(u, CMD.MOVE_STATE) or 
    			FindUnitCmdDesc(u, CMD.FIRE_STATE) or 
    			123456 -- back of the pack

			local defID = Spring.GetUnitDefID(u) 
			local def = UnitDefs[ defID ]["metalCost"] 
		
			if( defID == 367 ) then
				Spring.InsertUnitCmdDesc( u, insertID + 3, Happy)
			end	
			
	end

  end



	
else--UNSYNCED (its just ehre at the moment not realy beaing used)

   function gadget:KeyPress(key) --key scope exists within the function
	if( key == 121 ) then
		for _,u in ipairs(Spring.GetAllUnits()) do
			
			Spring.SendLuaRulesMsg(message_identifier..u.." was pressed")
			Spring.Echo( u )
		end
	end


  	Spring.Echo("Key "..key.." pressed in unsynced") 
	
      	Spring.SendLuaRulesMsg(message_identifier..key.." was pressed")
   end

end
probleam: so far I have no run time errors or compiling errors but never the less I dont get the button showing up in my units menue

Question: am I missing something basic that is part of this process? or completely misinterpriting the function of the commands I am trying to use to insert the command?

any info would be helpful

thank you for your time


EDIT!!!!!***

ok sorry for this

I changed my inicializing code to this

Code: Select all

function gadget:GameFrame(n)

	echo( n )

	if( n == 200) then
		
		for _,u in ipairs(Spring.GetAllUnits()) do 

			local insertID = 
   			FindUnitCmdDesc(u, CMD.CLOAK)      or 
    			FindUnitCmdDesc(u, CMD.ONOFF)      or 
    			FindUnitCmdDesc(u, CMD.TRAJECTORY) or 
    			FindUnitCmdDesc(u, CMD.REPEAT)     or 
    			FindUnitCmdDesc(u, CMD.MOVE_STATE) or 
    			FindUnitCmdDesc(u, CMD.FIRE_STATE) or 
    			123456 -- back of the pack

			echo( insertID )
			
			local defID = Spring.GetUnitDefID(u) 
			local def = UnitDefs[ defID ]["metalCost"] 
		
			if( defID == 367 ) then
				Spring.InsertUnitCmdDesc( u, insertID + 3, Happy)
			end	
			
		end
	end
end
now it works like a charm

but that leave me with a new question

Question: can buttons only be added if they are in game frame?
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: Gadget based buttons

Post by zwzsg »

No.

And it is a wonder you can get any code working at all with such a fluctuant spelling.
Voidranaut
Posts: 47
Joined: 20 Dec 2009, 21:16

Re: Gadget based buttons

Post by Voidranaut »

zwzsg wrote:No.

And it is a wonder you can get any code working at all with such a fluctuant spelling.
sorry about that I will start spell checking my English before I post

thanks for letting me know about the spelling now I can fix it :)

I do have one more problem though
Problem: have been trying to link my custom button happy to a custom function

My assumption is that Happy.action is responsible for what function gets called upon clicking the button.. however when I change this field to a custom function nothing seems to be called now this can only mean that I am confused on what exactly is going on or what rules are in play here which leads to my questions

Question1): does the string in action HAVE to be lower case? That seems to be a trend in all the gadgets I have

probleam2): in the following code I am attempting to call a function named MyFirstCommand by clicking on the button on in my units menu. The fact that it does not work suggests that there are other parameters that could be working against me in the following code?

Question2): is there anything in the following code that needs to be added/Changed to allow my custom function to be called? or a good source of info that I could reference to fix the problem myself?

Code: Select all

 function gadget:GetInfo()
   return {
      name = "CUTests",
      desc = "Gadget testing ground (only tests run here will do real scripts from what I learn here)",
      author = "zwzsg (and then horribly ruined by Voidranaut)",
      date = "not sure",
      version = "1.0",
      license = "Public Domain",
      layer = 0,
      enabled = true
   }
end
local echo = Spring.Echo
local message_identifier = gadget:GetInfo().name..": "
local once = 1
local height = 0
local increase = 0
local x1 = 7510
local z1 = 2600
local x2 = 7010
local z2 = 2800
local FindUnitCmdDesc = Spring.FindUnitCmdDesc

local CMD_UPGRADEMEX = 38274

local Happy = { 
  id      = CMD_HAPPY, 
  type    = CMDTYPE.ICON_UNIT_OR_AREA, 
  name    = 'Happy', 
  cursor  = 'Attack', 
  action  = 'myfirstcommand', 
  tooltip = 'Hammberger time', 
  hidden  = false, 
  params  = {} 
} 

if (gadgetHandler:IsSyncedCode()) then--SYNCED

  function gadget:Initialize()

	

  end


function gadget:GameFrame(n)

	--echo( n )

	if( n == 200) then
		
		for _,u in ipairs(Spring.GetAllUnits()) do 

			local insertID = 
   			FindUnitCmdDesc(u, CMD.CLOAK)      or 
    			FindUnitCmdDesc(u, CMD.ONOFF)      or 
    			FindUnitCmdDesc(u, CMD.TRAJECTORY) or 
    			FindUnitCmdDesc(u, CMD.REPEAT)     or 
    			FindUnitCmdDesc(u, CMD.MOVE_STATE) or 
    			FindUnitCmdDesc(u, CMD.FIRE_STATE) or 
    			123456 -- back of the pack

			echo( insertID )
			
			local defID = Spring.GetUnitDefID(u) 
			local def = UnitDefs[ defID ]["metalCost"] 
		
			if( defID == 367 ) then
				Spring.InsertUnitCmdDesc( u, insertID + 3, Happy)
			end	
			
		end
	end
	
	if( n % 5 == 0  ) then

		
		
		--if( x2 <= 7260 ) then
			--Spring.LevelHeightMap( x1, z1 , x2, z2, height )
			--height = height + 1
			--x1 = x1 - 1
			--x2 = x2 + 1
			--z1 = z1 + 1
			--z2 = z2 - 1
			--increase = increase + 0.1
		--end


		--Spring.AddTeamResource( 1, "m", 5000 )
		--Spring.AddTeamResource( 1, "e", 5000 )



		--Spring.SetUnitPosition( number unitID, number x, number z [, boolean alwaysAboveSea] ) -> nil

		--Spring.CreateUnit( 367, 7110, 45, 2700,(0), 1, nil )	
		
	end 

	

end	--end gameframe

	function MyFirstCommand()
		echo( "HAMBURGER TIME" )


	end 

	
else--UNSYNCED

   function gadget:KeyPress(key) --key scope exists within the function
	if( key == 121 ) then
		for _,u in ipairs(Spring.GetAllUnits()) do
			
			Spring.SendLuaRulesMsg(message_identifier..u.." was pressed")
			Spring.Echo( u )
		end
	end


  	Spring.Echo("Key "..key.." pressed in unsynced") 
	
      	Spring.SendLuaRulesMsg(message_identifier..key.." was pressed")
   end

end 
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: Gadget based buttons

Post by zwzsg »

Voidranaut wrote:Question: can buttons only be added if they are in game frame?
What's wrong:

- Spring.GetAllUnits() gets all the units, not unit types, but the individual live units.

- So in your loop, each u is a unit. Not a unit type, not a unit definition, not the idea of a unit, but a physical unit.

- When the gadget initialize, there are no units.

- At frame 200, there is most likely one unit per team, the commander. So you gadget give the new button to them.

- The gadget does not work like a charm. I will not give the new button to any other unit that the commander (or whatever you start with).





What you should do:

- Understand your own code. Even if it's copy pasta from other code, read it and follow what it does. Your problem are far from being tricky engine bugs, they're just pretty obvious flaw in your logic. Just by using the cognitive abilities you were born with, you should have been able to solve most of your issues.

- You should use the callin gadget:UnitCreated(u,ud,team) instead of GameFrame. UnitCreated gets called every time a unit is created (duh!), and even tells you the uniID and unitDefId of that unit (saving you the loop on all units for instance).

- Writing defID == 367 is BAAAD!! Lurker told you already. Use something like defID == UnitDefNames.corkrog.id (where corkrog would be replaced by your unitname).













Typing Real Programmers Don't Use Pascal into a web search engine should turn up a copy.
http://www.omen.net.au/~synic/code/gothc25.htm
http://e-drexler.com/p/06/00/EOC_Cover.html


Voidranaut wrote:I do have one more problem though
Problem: have been trying to link my custom button happy to a custom function

My assumption is that Happy.action is responsible for what function gets called upon clicking the button.. however when I change this field to a custom function nothing seems to be called... now this can only mean that I am confused on what exactly is going on or what rules are in play here which leads to my questions
It doesn't work like that. Pressing the button does NOT directly call a function.

Instead, you must use the call-in CommandFallback.

Now that one is a bit complicated. Whenever the engine see a command it doesn't know (here, it would be CMD_HAPPY, except you should have given it a numerical value (that isn't used by any other command)), it would call:

function gadget:CommandFallback(u,ud,team,cmd,params,options,tag)

Which should return two boolean, used and finished. That is, the first boolean means you recognised the command, and the second boolean means that you're done with that command and the engine can proceed to the next in the queue of that unit.


Often, you also want to use:

function gadget:AllowCommand(u,ud,team,cmd,params,options,tag,synced)

That would check the command the moment it's queued, and you should make it return false when you recognise the command and don't want it to be allowed, and true otherwise (so true if it's an unrelated command or if you want to allow the command).
Post Reply

Return to “Lua Scripts”