View topic - Unable to get playername??



All times are UTC + 1 hour


Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: 31 Oct 2011, 17:33 

Joined: 10 Feb 2011, 03:00
Hey hey!

I have this portion of the code:
Code:
   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.


Top
 Offline Profile  
 
PostPosted: 31 Oct 2011, 17:56 
Moderator
User avatar

Joined: 22 Feb 2006, 01:02
Location: cheap kitchen
This makes no sense:
Code:
 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?


Top
 Offline Profile  
 
PostPosted: 31 Oct 2011, 17:58 

Joined: 10 Feb 2011, 03:00
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?


Top
 Offline Profile  
 
PostPosted: 31 Oct 2011, 18:08 
Moderator
User avatar

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


Top
 Offline Profile  
 
PostPosted: 31 Oct 2011, 18:19 

Joined: 10 Feb 2011, 03:00
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:
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?


Top
 Offline Profile  
 
PostPosted: 31 Oct 2011, 18:33 
Moderator
User avatar

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

Quote:
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?


Top
 Offline Profile  
 
PostPosted: 31 Oct 2011, 18:46 

Joined: 10 Feb 2011, 03:00
Hey!

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


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

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


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.