MousePress & KeyPress call-ins

MousePress & KeyPress call-ins

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

Moderator: Moderators

Post Reply
InDesign
Posts: 3
Joined: 08 Aug 2011, 17:46

MousePress & KeyPress call-ins

Post by InDesign »

I searched and read the forum. I got my very first gadget working with call-in GameStart() and displayed debug message with Spring.Echo ( "msg"). For some unknown reason swapping GameStart() with the MousePress or KeyPress do not display message. I have pasted (to the end of file) these functions to Example Mod game_spawn.lua file to make sure my new script is able to be executed whatsoever.

Code: Select all

function gadget:MousePress(x, y, button)
  Spring.Echo( "Mouse button pressed! " )
end
I suspect already malfunction with my Spring Engine and OS (though I can select & click to select units inside Example Mod). Am I missing something obvious? Not to mention working Lua MousePress example would be awesome.
My Spring Engine version is 0.82.7.1, OS: Win7, Intel-Celeron cpu.
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: MousePress & KeyPress call-ins

Post by knorke »

welcome!
you must seperate your gadget code in a synced and an unsynced part.

Code: Select all

if gadgetHandler:IsSyncedCode() then
--synced stuff goes here: 
--creating units, giving resources, ...
else
--unsynced stuff goes here:
--reading keypresses, drawing on screen,...
end
wiki and http://answers.springlobby.info/ has some info how you can send stuff back and forth between synced/unsynced.
imkharn
Posts: 7
Joined: 10 Aug 2011, 05:39

Re: MousePress & KeyPress call-ins

Post by imkharn »

Someone else wrote the music.lua script for me.

Are you saying they messed up the code?
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: MousePress & KeyPress call-ins

Post by knorke »

music script sounds like widget.
widgets are always unsynced.
in first post you write about a gadget?
InDesign
Posts: 3
Joined: 08 Aug 2011, 17:46

Re: MousePress & KeyPress call-ins

Post by InDesign »

Knorke I appreciate your input.
Image
Demo showing these scripts in action under post as attachement.
Solution to thread problem, before bit backstory. My version of Spring Engine is 0.82.7.1, OS is Win7, data of this post is 11 august 2011.
Unix users pay attention to upper- and lowercase! Change file names accordingly.
Press F11 in-game to access widget menu, F12 to take screenshot and F5 to toggle SpringUI off/ on.
moddir is your independent game folder (archive). For example valid
folder name for game is: your_game.sdd
widgets are for user interface (UI), they are stored in:
moddir/LuaUI/widgets/your_script.lua
gadgets are for creating everything on game, they are stored in:
moddir/LuaRules/gadgets/your_script.lua
gadgets need gadget framework to work which is *.lua file in:
moddir/LuaRules/main.lua

main.lua file content:

Code: Select all

VFS.Include("LuaGadgets/gadgets.lua",nil, VFS.BASE)
Create it yourself if you do not have it.
Widgets get loaded into game only if they are in correct folder.
No widget in gadget folder and vice versa.

To the point, in moddir/LuaUI/widgets/ add this *.lua script:

Code: Select all

-- default Lua script info block
function widget:GetInfo()
  return {
    name      = "MousePress event",
    desc      = "Invoked by MousePress call-in to forward message",
    author    = "InDesign",
    date      = "Aug 11, 2011",
    license   = "GNU GPL, v2 or later",
    layer     = 5,
    enabled   = true -- this script enabled by default?
  }
end

-- Spring.SendLuaRulesMsg is Spring Engine function
--  MessageDispatcher is name, same naming is used in
-- Alpha Domination game by Sunspot
-- You can store functions into variables, isn't it cool?
local MessageDispatcher = Spring.SendLuaRulesMsg

-- MousePress is a Lua API function, full list can be found:
-- http://springrts.com/wiki/LuaCallinReturn
function widget:MousePress(x, y, button)
  -- When mouse press event happens we send message out
  -- to invoke RecvLuaMsg event
  MessageDispatcher("mousepress")
end
In moddir/LuaRules/gadgets/ add this *.lua script:

Code: Select all

-- default Lua script info block
function gadget:GetInfo()
  return {
    name      = "MousePress action",
    desc      = "Invoked by RecvLuaMsg call-in to aquire message",
    author    = "InDesign",
    date      = "Aug 11, 2011",
    license   = "GNU GPL, v2 or later",
    layer     = 5,
    enabled   = true -- this script enabled by default?
  }
end
-- By sending message with Spring.SendLuaRulesMsg also
-- named MessageDispatcher in event_mouse_press.lua
-- RecvLuaMsg call-in gets executed
function gadget:RecvLuaMsg(msg, playerID)
  -- With more functions sending messages it becomes important
  -- to do identity check
  -- Spring.Echo is function to display messages on screen
  if msg == "mousepress" then Spring.Echo("Mouse pressed!") end
end
Test your game. Things to think about: what about syncronized scripts? How to make it work all in one *.lua script? Are you using coconuts!?
Attachments
demo.sdd.sd7
(1.21 KiB) Downloaded 19 times
mouse_pressed.jpg
mouse_pressed.jpg (11.3 KiB) Viewed 1571 times
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: MousePress & KeyPress call-ins

Post by knorke »

hings to think about: what about syncronized scripts? How to make it work all in one *.lua script? Are you using coconuts!?
Are those question you have? I dont really understand what you mean. Bananas!
Post Reply

Return to “Lua Scripts”