Unable to get playername??

Unable to get playername??

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

Moderator: Moderators

Post Reply
Von66341
Posts: 111
Joined: 10 Feb 2011, 03:00

Unable to get playername??

Post by Von66341 »

Hey hey!

I have this portion of the code:

Code: Select all

	if(input ~= "") then 
						for i,v in pairs(Spring.GetTeamList()) do
						if v > 0 then  -- a check because for some reason when i = 1, then v = 0 and teamID = nil
						local teamID = Spring.GetTeamList()[v]
							for j,k in pairs(Spring.GetTeamUnits(teamID)) do --k is unitID
								if k > 0 then --a check similar to above 
								_,playerID,_,_,_,_,_,_=Spring.GetTeamInfo(teamID)
								playername,_,_,_,_,_,_,_,_,_=Spring.GetPlayerInfo(playerID)
								end
							end 
						end 
					end 
					Spring.SendLuaUIMsg(msgbox.." Playername;"..playername..";Answer "..counter..";".." "..input)
			end
When i run its errors says: '[f=0000901] LuaUI::RunCallIn: error = 2, MouseRelease, [string "LuaUI/Widgets/gui_pausescreen.lua"]:213: attempt to concatenate global 'playername' (a nil value)'

Does anyone know what I am doing wrong?
It works once before, but it doesn't now.
I am connecting the 2 laptops through wireless.
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Unable to get playername??

Post by knorke »

This makes no sense:

Code: Select all

 for j,k in pairs(Spring.GetTeamUnits(teamID)) do --k is unitID
	if k > 0 then --a check similar to above
	_,playerID,_,_,_,_,_,_=Spring.GetTeamInfo(teamID)
	playername,_,_,_,_,_,_,_,_,_=Spring.GetPlayerInfo(playerID)
	end
 end
The variables of the loop are j and k. But nothing is done with them in the loop. Why do you loop over all units in a team if you just want a playername?

if k > 0 then --a check similar to above
this a bit strange since unitIDs are always positive, so the check is unneeded.
Use Spring.ValidUnitID (unitid) to see if a unitid is valid.

Also you get playername several times but SendLuaUIMsg is outside the loops, so only the last one will ever get send?
What are you trying to do?
Von66341
Posts: 111
Joined: 10 Feb 2011, 03:00

Re: Unable to get playername??

Post by Von66341 »

Hey!

Am trying to get the playername.
The player will be typing message on the screen, i want to add the playername to this message, after which sendluamessage to get another widget to write this message to a txt file.

How should I do it right?
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Unable to get playername??

Post by knorke »

Von66341 wrote:The player will be typing message on the screen
in chat? in some textbox you made with lua?
Why is that in gui_pausescreen.lua? help.

Since it is a widget the only player able to click the "send message" or whatever button is the local player. I would use Spring.GetMyPlayerID and pass that to GetTeamInfo

Also the funfact of SendMessage might be of interesst.
Von66341
Posts: 111
Joined: 10 Feb 2011, 03:00

Re: Unable to get playername??

Post by Von66341 »

Yup. in a textbox i created in lua.
is in gui_pausescreen.lua because the players are writing the message at gamepause, so i merge my textbox with the pausescreen widget.

I manage to get it working. my new code:

Code: Select all

if(input ~= "") then 
				if(playercount ==0) then 
						for i,v in pairs(Spring.GetTeamList()) do
							if v > 0 then  -- a check because for some reason when i = 1, then v = 0 and teamID = nil
							local teamID = Spring.GetTeamList()[v]
									_,playerID =Spring.GetTeamInfo(teamID)
									playername = Spring.GetPlayerInfo(playerID)
									Spring.Echo("playername")
									Spring.Echo(playername)
									playercount= playercount+1
									break
							end
						end 
				end 
			end 
			
				if(playername == nil) then 
				--Spring.Echo("nil")
				else 
				--Spring.Echo("message")
				Spring.SendLuaUIMsg(msgbox.." Playername;"..playername..";Answer "..counter..";".." "..input)
				--Spring.Echo(msgbox.." Playername;"..playername..";Answer "..counter..";".." "..input)
				end 
		input = ""
		end
Do you see if i might have any further problems with the code i write?
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Unable to get playername??

Post by knorke »

Von66341 wrote:for i,v in pairs(Spring.GetTeamList()) do
if v > 0 then -- a check because for some reason when i = 1, then v = 0 and teamID = nil
this is still wrong.
with the v>0 thing you only mask that the loop is wrong.
I think it should be
for _ ,teamID in ipairs(Spring.GetTeamList()) do

has to do with tables, key/values bla, a Lua tutorial can explain better than me.
if(playercount ==0) then
since I can not see the whole widget, I have no idea what this does, among other things.
Why is there a break in the loop?
Von66341
Posts: 111
Joined: 10 Feb 2011, 03:00

Re: Unable to get playername??

Post by Von66341 »

Hey!

Thanks!
I manage to further edit the code.
Thanks for the tip on Spring.GetMyPlayerID!
Post Reply

Return to “Lua Scripts”