Have simple probleam - Page 1

Have simple probleam

Discuss game development here, from a distinct game project to an accessible third-party mutator, down to the interaction and design of individual units if you like.

Moderator: Moderators

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

Re: Have simple probleam

Post 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
Last edited by zwzsg on 21 Jan 2010, 21:06, edited 1 time in total.
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Re: Have simple probleam

Post by Argh »

Keypresses are by definition unsynced!
This. Same with mouse.
Voidranaut
Posts: 47
Joined: 20 Dec 2009, 21:16

Re: Have simple probleam

Post 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
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: Have simple probleam

Post 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.
Voidranaut
Posts: 47
Joined: 20 Dec 2009, 21:16

Re: Have simple probleam

Post 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?
Last edited by Voidranaut on 22 Jan 2010, 05:03, edited 3 times in total.
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Re: Have simple probleam

Post by smoth »

Good luck with whatever endeavor you pursue. Sorry for my shitty advice early on.
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Re: Have simple probleam

Post by Argh »

error = 2, LuaRules/draw.lua, error = 2, LuaGadgets/gadgets.lua, [string "LuaGadgets/actions.lua"]:54: attempt to index local 'g' (a function value)
That kind of error halts gadgetHandler, IIRC, and can basically crash the whole Lua state for Gadgets.

IOW, if you have an error that bad (usually comes up with people attempting to port CA Gadgets, hmm) then you probably needed something from CA's Gadget files in the LuaRules directory (action.lua, system.lua, etc.) before it will operate.

So, removing that thing basically removed the conflict and allowed zwswg's code to operate.
User avatar
Forboding Angel
Evolution RTS Developer
Posts: 14673
Joined: 17 Nov 2005, 02:43

Re: Have simple probleam

Post by Forboding Angel »

Yeah, morph has bricked evo gadgets before. Once you actually know what's going on the fix is fairly simple, but when you're staring at infolog going "OMGWTF?!?!?! GHEY!"... yeah.
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: Have simple probleam

Post by zwzsg »

Voidranaut wrote:hay one last question

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

what significance is the red syntax?
It's a RegExp. Don't even try to understand that now.

Regular Expresions are very powerful, but very hard to come up with or even decode. Leave them to the Wizards.

The whole line though put into "MsgForMe" the part of the string "msg" that follows the identifier (or nil if there is no identifier).
User avatar
lurker
Posts: 3842
Joined: 08 Jan 2007, 06:13

Re: Have simple probleam

Post by lurker »

Specifically, () means "output the part inside the ()" and .* means "everything from here to the end".
But if you want a simpler method (which won't fail if the gadget name has certain weird things in it):

Code: Select all

if msg:sub(1,#message_identifier) == message_identifier then
    local MsgForMe = msg:sub(#message_identifier + 1)
    --stuff
end
Post Reply

Return to “Game Development”