Lua:Tutorial GettingStarted: Difference between revisions

From Spring
Jump to navigationJump to search
8611 (talk | contribs)
edited with better beginner widget
clean up widget example page
Line 1: Line 1:
[[Engine_Development|Development]] < [[Lua_Scripting|Lua Scripting]] < {{FULLPAGENAME}}
[[Engine_Development|Development]] < [[Lua_Scripting|Lua Scripting]] < {{FULLPAGENAME}}
Here are some examples of small widgets, with useful code to get you started! To play with each example, place the code into a "my_new_widget_name.lua" file in your Spring/LuaUI/widgets dir (create it if not already present) and run Spring with any game. Your widget should then appear ingame!
To see many more examples, check out the Luaui/widgets directory of the games in [[Gamedev:PublicRepos|PublicRepos]].


<code><pre>
<code><pre>
function widget:GetInfo()
function widget:GetInfo()
   return {
   return {
       name        = "Name as shown in widget list",
       name        = "Name as shown in widget list",
       desc        = "",
       desc        = "Description as normally shown in tooltip",
       author         = "john doe",
       author       = "It could be you!",
       date        = "date",
       date        = "now",
       license         = "PD",
       license     = "PD", -- should be compatible with Spring
       layer         = 0,
       layer       = 0,
      experimental  = false,
       enabled     = true
       enabled         = true
   }
   }
end
end


local stringOfWords = "BLAH BLAH BLAH"
local helloWorld = "I STARTED LOOK AT ME JIM!"
local toggle
local toggle


function widget:Initialize()
function widget:Initialize()
   Spring.Echo(stringOfWords .. " I STARTED LOOK AT ME JIM!")
   Spring.Echo(helloWorld)
end
end




function widget:Update(frame)
function widget:Update(frame)
   if(toggle == true) then
   if (toggle == true) then
       Spring.Echo("TOCK")
       Spring.Echo("TOCK")
       toggle = false
       toggle = false
Line 33: Line 36:
   end
   end
end
end
</pre></code>
<code><pre>
function widget:GetInfo()
  return {
      name        = "Unit Announcer",
      desc        = "Units are proud to be alive",
      author      = "Some guy",
      date        = "I don't know why we include dates",
      license      = "GPLv2 or later",
      layer        = 0,
      enabled      = true
  }
end
function widget:UnitCreated(unitID, unitDefID, unitTeam, builderID)
  Spring.Echo("HELLO")
  local humanName = UnitDefs[unitDefID].humanName
  Spring.Echo("I am a " .. humanName)
  local numWeapons = #UnitDefs[unitDefID].weapons
  Spring.Echo("I have " .. numWeapons .. " weapon .. (numWeapons>0 and "s" or ""))


  if numWeaponders>0 then
    Spring.Echo("FEAR ME")
  end
end
</pre></code>
</pre></code>
[[category: Lua]]
[[category: Lua]]

Revision as of 14:20, 13 August 2015

Development < Lua Scripting < Lua:Tutorial GettingStarted

Here are some examples of small widgets, with useful code to get you started! To play with each example, place the code into a "my_new_widget_name.lua" file in your Spring/LuaUI/widgets dir (create it if not already present) and run Spring with any game. Your widget should then appear ingame!

To see many more examples, check out the Luaui/widgets directory of the games in PublicRepos.


function widget:GetInfo()
   return {
      name         = "Name as shown in widget list",
      desc         = "Description as normally shown in tooltip",
      author       = "It could be you!",
      date         = "now",
      license      = "PD", -- should be compatible with Spring
      layer        = 0,
      enabled      = true
   }
end

local helloWorld = "I STARTED LOOK AT ME JIM!"
local toggle

function widget:Initialize()
   Spring.Echo(helloWorld)
end


function widget:Update(frame)
   if (toggle == true) then
      Spring.Echo("TOCK")
      toggle = false
   else
      Spring.Echo("TICK")
      toggle = true
   end
end


function widget:GetInfo()
   return {
      name         = "Unit Announcer",
      desc         = "Units are proud to be alive",
      author       = "Some guy",
      date         = "I don't know why we include dates",
      license      = "GPLv2 or later",
      layer        = 0,
      enabled      = true
   }
end

function widget:UnitCreated(unitID, unitDefID, unitTeam, builderID)
  Spring.Echo("HELLO")

  local humanName = UnitDefs[unitDefID].humanName
  Spring.Echo("I am a " .. humanName)

  local numWeapons = #UnitDefs[unitDefID].weapons
  Spring.Echo("I have " .. numWeapons .. " weapon .. (numWeapons>0 and "s" or ""))

  if numWeaponders>0 then
    Spring.Echo("FEAR ME")
  end
end