Page 1 of 2

Have simple probleam

Posted: 21 Jan 2010, 07:17
by Voidranaut
I have the following code

its suposed to echo what keys I hit to the console

Probleam: I got a widget with the same commands working but I cant get this gadget to work

question1): does the Spring.Echo command work for gadgets?

question2): am I confused on how the synced code command is ment to be used?

Code: Select all

function gadget:GetInfo()
	return 
	{
		name = "tets",
		desc = "Allows me to play and see what is needed to add commands",
		author = "me myself and I",
		date = "here , now. and a little last week",
		license = "potato and fries",
		layer = 1,
		enabled = true
	}
end

echo = Spring.Echo

if (gadgetHandler:IsSyncedCode()) then

	function gadget:KeyPress(key, mods, isRepeat, label, unicode)
		
			echo ( key )
		
	end

end
thanks for your time any info would be great

Re: Have simple probleam

Posted: 21 Jan 2010, 07:19
by smoth

Code: Select all

Spring.Echo("WOOOOOOOOOOOOORDS")
use it like that.

Re: Have simple probleam

Posted: 21 Jan 2010, 07:20
by Voidranaut
smoth wrote:

Code: Select all

Spring.Echo("WOOOOOOOOOOOOORDS")
use it like that.
k thanks will try

Re: Have simple probleam

Posted: 21 Jan 2010, 07:22
by smoth
if that works then what you did wrong was likely not putting local before the var name echo...

Re: Have simple probleam

Posted: 21 Jan 2010, 07:27
by Voidranaut
ok so I tried it and it was a no go

nothing showed up on my consle even if I hit all shinny buttons I wanted


**modded cod per your spec**

Code: Select all

function gadget:GetInfo()
	return 
	{
		name = "tets",
		desc = "Allows me to play and see what is needed to add commands",
		author = "me myself and I",
		date = "here , now. and a little last week",
		license = "potato and fries",
		layer = 1,
		enabled = true
	}
end

echo = Spring.Echo

if (gadgetHandler:IsSyncedCode()) then

	function gadget:KeyPress(key, mods, isRepeat, label, unicode)
		
			echo ( "woooorrrddssss" )
		
	end

end




Re: Have simple probleam

Posted: 21 Jan 2010, 07:36
by smoth
Voidranaut wrote:
**modded cod per your spec**
function gadget:GetInfo()
return
{
name = "tets",
desc = "Allows me to play and see what is needed to add commands",
author = "me myself and I",
date = "here , now. and a little last week",
license = "potato and fries",
layer = 1,
enabled = true
}
end

local echo = Spring.Echo

if (gadgetHandler:IsSyncedCode()) then

function gadget:KeyPress(key, mods, isRepeat, label, unicode)

echo ( "woooorrrddssss" )

end

end
try changing that.

Re: Have simple probleam

Posted: 21 Jan 2010, 07:45
by Voidranaut
still no go

querry: I am closeing spring everytime I make changes but not the lobby .. would that make my probleam?

question: do I even need to close spring? or is there a reload gadget command I can do?

new code:

Code: Select all

function gadget:GetInfo()
	return 
	{
		name = "tets",
		desc = "Allows me to play and see what is needed to add commands",
		author = "me myself and I",
		date = "here , now. and a little last week",
		license = "potato and fries",
		layer = 1,
		enabled = true
	}
end

echo = Spring.Echo()

if (gadgetHandler:IsSyncedCode()) then

	function gadget:KeyPress(key, mods, isRepeat, label, unicode)
		
			Spring.Echo( "woooorrrddssss" )
		
	end

end




Re: Have simple probleam

Posted: 21 Jan 2010, 08:23
by CarRepairer
KeyPress callin is only in unsynced, not synced code.

change

Code: Select all

if (gadgetHandler:IsSyncedCode()) then
to

Code: Select all

if (gadgetHandler:IsSyncedCode()) then
else
Edit: scratch that, I don't think this is even possible. You should do this in a widget and send the data to the gadget using Spring.SendLuaRulesMsg if you need it there. But the above code still applies to how you separate unsynced vs synced code.

Re: Have simple probleam

Posted: 21 Jan 2010, 08:27
by lurker
smoth, local is a scoping thing, it has no effect on whether the value actually gets set if nothing else uses the same name. What are you doing?

Re: Have simple probleam

Posted: 21 Jan 2010, 08:38
by Argh

Code: Select all

function gadget:GetInfo()
   return
   {
      name = "test",
      desc = "Allows me to play and see what is needed to add commands",
      author = "me myself and I",
      date = "here , now. and a little last week",
      license = "potato and fries",
      layer = 1,
      enabled = true
   }
end

echo = Spring.Echo()

if (gadgetHandler:IsSyncedCode()) then

function gadget:RecvLuaMsg(msg, playerID)
	if string.find(msg,"luaMessage") then
		msg = msg:gsub("luaMessage","")
                Spring.Echo("Value of kcount = "..msg)
	end
end

else  --UNSYNCED

local kcount = 0
   function gadget:KeyPress(key, mods, isRepeat, label, unicode)
         kcount = kcount + 1
Spring.SendLuaRulesMsg("luaMessage"..kcount)
   end
end

Re: Have simple probleam

Posted: 21 Jan 2010, 08:44
by lurker
Argh wrote:

Code: Select all

function gadget:GetInfo()
   return
   {
      name = "test",
      desc = "Allows me to play and see what is needed to add commands",
      author = "me myself and I",
      date = "here , now. and a little last week",
      license = "potato and fries",
      layer = 1,
      enabled = true
   }
end

echo = Spring.Echo()

if (gadgetHandler:IsSyncedCode()) then

function gadget:RecvLuaMsg(msg, playerID)
	if string.find(msg,"luaMessage") then
		msg = msg:gsub("luaMessage","")
                Spring.Echo("Value of kcount = "..msg)
	end
end

else  --UNSYNCED

local kcount = 0
   function gadget:KeyPress(key, mods, isRepeat, label, unicode)
         kcount = kcount + 1
   end
end
Argh, perhaps you should write more then two thirds of the gadget before you post it. Using keypress data instead of just a count, actually sending the data...

Re: Have simple probleam

Posted: 21 Jan 2010, 08:48
by Argh

Code: Select all

msg:gsub("luaMessage","")
You can use that to do whatever. If he wants to intercept a given key value, then either he needs to record the number manually or include keysyms.lua

Re: Have simple probleam

Posted: 21 Jan 2010, 09:31
by smoth
lurker wrote:smoth, local is a scoping thing, it has no effect on whether the value actually gets set if nothing else uses the same name. What are you doing?
My bad... no more half awake lua advice..

Re: Have simple probleam

Posted: 21 Jan 2010, 09:46
by Voidranaut
Argh wrote:

Code: Select all

msg:gsub("luaMessage","")
You can use that to do whatever. If he wants to intercept a given key value, then either he needs to record the number manually or include keysyms.lua
wow no wonder I was havieng trouble with this simple echo command in a gadget

so from what you guys wrote
1) I have to echo out of synced code
2) include the msg:gsub() to grab the passed message?
3) also make a widget that will get the key stroke and pass it to msg:gsub()?

is that right or do I have things backwards/ommited?

Re: Have simple probleam

Posted: 21 Jan 2010, 16:36
by lurker
You can echo out of unsynced just fine.

Code: Select all

[[infoblock goes here]]

local Echo = Spring.Echo

function gadget:KeyPress(...) Echo(...) end

Re: Have simple probleam

Posted: 21 Jan 2010, 20:47
by zwzsg
No.

Echo can used be both in synced and unsynced.


However,

When you press a key, you are alone to press the key. You're not pressing the key of other players along as yours.

Keypresses are by definition unsynced!


Working exemple:

Code: Select all

function gadget:GetInfo()
	return {
		name = "KeyCatchEx",
		desc = "Show how to catch keypresses in gadgets",
		author = "zwzsg",
		date = "21st of January 2010",
		version = "1.0",
		license = "Public Domain",
		layer = 0,
		enabled = true
	}
end

local up_arrow,down_arrow,right_arrow,left_arrow,enter,space,dee = 273,274,275,276,13,32,100

local message_identifier = gadget:GetInfo().name..": "

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

	function gadget:RecvLuaMsg(msg,player)
		local MsgForMe=string.match(msg,message_identifier.."(.*)")
		if MsgForMe then
			Spring.Echo("Synced recieved a message from unsynced:")
			Spring.Echo(MsgForMe)
		end
	end

else--UNSYNCED

	function gadget:KeyPress(key)
		Spring.Echo("Key "..key.." pressed in unsynced")
		Spring.SendLuaRulesMsg(message_identifier..key.." was pressed")
	end

end

Re: Have simple probleam

Posted: 21 Jan 2010, 20:52
by Argh
Keypresses are by definition unsynced!
This. Same with mouse.

Re: Have simple probleam

Posted: 22 Jan 2010, 01:23
by Voidranaut
Thanks all for all the info and help :)

I am still a little puzzled by this though

I tried out zwzsg sript wich he provided (thank you for takeing the time and energy zwzsg) and it did not work (wich means I managed to somehow screw it up) the expected text did not show up in the console or infolog

the fact it did not work segjests I am making some SUPER stupid error by assuming something I should not be

so let me put down a few simple things I have been doing and assumeing are right

1) gadgets go in the LuaRules/Gadets folder

2) gadgets must have same save name as the name depicted in function gadget:GetInfo()

3) its ok to keep spring lobby open when adding/changing gadgets

4) you need to start your spring game after all gadgets are ready to be tested

5) widgets with the function gadget:KeyPress(key) cant interfear with gadgets that have the function gadget:KeyPress(key) even if they are in the game mod at the same time

6) haveing the text file of a gadget open wile game is running is ok

...

I cant think of much more right now

...

the fact that something this simple is giving me this much trouble makes me wonder if I put something in my mod that is interfearing with gadgets Spring.Echo command (I dont know if that is even possible) but it is worth mentioning that I made a widget with the echo that works fine everytime...

at this point I am pretty lost and puzzled at what my next simple step should be

I guess I will take out all other gadgets and see if it works with just that one in there

thanks for helping me through this guys :) hope I can repay one day

Re: Have simple probleam

Posted: 22 Jan 2010, 01:53
by zwzsg
Voidranaut wrote:and it did not work
That's odd. I did test right before posting.
Voidranaut wrote:gadgets go in the LuaRules/Gadets folder
I hope you spelt the folder name correctly...
Voidranaut wrote:gadgets must have same save name as the name depicted in function gadget:GetInfo()
Not necessarly.
Voidranaut wrote:3) its ok to keep spring lobby open when adding/changing gadgets
Yeah.
Voidranaut wrote:4) you need to start your spring game after all gadgets are ready to be tested
Yeah.

Or you could use the command /luarules reload
Voidranaut wrote:5) widgets with the function gadget:KeyPress(key) cannot interfear with gadgets that have the function gadget:KeyPress(key) even if they are in the game mod at the same time
Hmm, not sure!

If I recall well, when a KeyPress returns false, it means the gadget didn't take it, and is letting the KeyPress free to be grabbed by another gadget. But when KeyPress returns true, it means the gadget took the keypress, and the keypress is not anymore available to other gadget, other widgets, or the engine.
Voidranaut wrote:6) haveing the text file of a gadget open wile game is running is ok
Yes.

Re: Have simple probleam

Posted: 22 Jan 2010, 03:52
by Voidranaut
ok so I played with removeing gadgets and found out what I did that was stopping the gadget from working

oddly enough it had everything to do with a probleam I was gettting help on with another thread in this forum

I took the unit_morph gadget and made a copy of it then modded the copy (I was trying to gain the ability to give units multiple morphing buttons but managed to create this error)

error = 2, LuaRules/draw.lua, error = 2, LuaGadgets/gadgets.lua, [string "LuaGadgets/actions.lua"]:54: attempt to index local 'g' (a function value)

now from what I heard the error could mean a few things but with the fact that it stoped zwzsg script I was wondering if that helps narrow down what I could have done wrong with my original script

if not oh well

but the big point is that zwzsg's script works great and I have deffently learned alot from you guys thanks :)

hay one last question

local MsgForMe=string.match(msg,message_identifier.."(.*)")

what significance is the red syntax?