View topic - MousePress & KeyPress call-ins



All times are UTC + 1 hour


Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: 10 Aug 2011, 17:30 

Joined: 08 Aug 2011, 16:46
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:
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.


Top
 Offline Profile  
 
PostPosted: 10 Aug 2011, 17:38 
Moderator
User avatar

Joined: 22 Feb 2006, 01:02
Location: cheap kitchen
welcome!
you must seperate your gadget code in a synced and an unsynced part.
Code:
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.


Top
 Offline Profile  
 
PostPosted: 10 Aug 2011, 18:56 

Joined: 10 Aug 2011, 04:39
Someone else wrote the music.lua script for me.

Are you saying they messed up the code?


Top
 Offline Profile  
 
PostPosted: 10 Aug 2011, 19:00 
Moderator
User avatar

Joined: 22 Feb 2006, 01:02
Location: cheap kitchen
music script sounds like widget.
widgets are always unsynced.
in first post you write about a gadget?


Top
 Offline Profile  
 
PostPosted: 10 Aug 2011, 19:39 

Joined: 08 Aug 2011, 16:46
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:
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:
-- 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:
-- 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 14 times
mouse_pressed.jpg
mouse_pressed.jpg [ 11.3 KiB | Viewed 434 times ]
Top
 Offline Profile  
 
PostPosted: 16 Aug 2011, 23:02 
Moderator
User avatar

Joined: 22 Feb 2006, 01:02
Location: cheap kitchen
Quote:
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!


Top
 Offline Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: Bing [Bot] and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group

Site layout created by Roflcopter et al.