Send message to all players from gadget

Send message to all players from gadget

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

Moderator: Moderators

Post Reply
LEDZ
Posts: 66
Joined: 27 Jun 2011, 13:43

Send message to all players from gadget

Post by LEDZ »

What Lua function does one use when wanting to show a message to all players.
An event occurs for player1 (lost 10 units), gadget sends message to all players with "player1 has lost 10 units"
I thought it was Spring.SendMessage but I just tested it with no luck.
Anyone know?
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6242
Joined: 29 Apr 2005, 01:14

Re: Send message to all players from gadget

Post by FLOZi »

If you want it to just appear in the chat you could just use Spring.Echo. Not elegant but it will work.
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: Send message to all players from gadget

Post by zwzsg »

Spring.SendMessage works and has the advantage over Spring.Echo that you can pass playernames with syntax such as <PLAYER0>

Here is short gadget as an exemple.
LEDZ
Posts: 66
Joined: 27 Jun 2011, 13:43

Re: Send message to all players from gadget

Post by LEDZ »

I've tried a these and it still doesn't work. Each player only sees their own announcements instead of everybody's.
I even tried Spring.SendMessageToPlayer which each playerID with the same outcome. Anyone know why?
User avatar
aegis
Posts: 2456
Joined: 11 Jul 2007, 17:47

Re: Send message to all players from gadget

Post by aegis »

can you pastebin your gadget code showing the only-to-one-player problem?
LEDZ
Posts: 66
Joined: 27 Jun 2011, 13:43

Re: Send message to all players from gadget

Post by LEDZ »

http://pastebin.com/Y0PPsGZg

These lines are responsible for the message process.
Using SendMessageToPlayer is my latest attempt.
I have also tried Echo and SendMessage with exactly the same results.
User avatar
Niobium
Posts: 456
Joined: 07 Dec 2008, 02:35

Re: Send message to all players from gadget

Post by Niobium »

Here's an example using the synced->unsynced method:

Code: Select all

if gadgetHandler:IsSyncedCode() then
    
    ----------------------------------------------------------------
    -- Synced
    ----------------------------------------------------------------
    function gadget:UnitDestroyed(uID, uDefID, uTeam, aID, aDefID, aTeam)
        SendToUnsynced('TeamLostAUnit', uTeam)
    end
    
else
    
    ----------------------------------------------------------------
    -- Unsynced
    ----------------------------------------------------------------
    function gadget:RecvFromSynced(identifier, arg1, arg2, arg3, ...)
        if identifier == 'TeamLostAUnit' then
            local teamID = arg1
            local _, playerID = Spring.GetTeamInfo(teamID)
            if playerID then
                local playerName = Spring.GetPlayerInfo(playerID)
                Spring.Echo(string.format('%s lost a unit !', playerName))
            end
        end
    end
    
end
It's a little more code than if you were to just put the echo in the synced :UnitDestroyed callin, but the synced side of a gadget isn't the proper place for displaying information anyway as it can't do any drawing/display etc. Also the synced side of a gadget can't get player names.

You might know this already but gadgets run on every players PC, not the server, so messaging all players can be accomplished by just having the gadget message the local player (as Spring.Echo does).
LEDZ
Posts: 66
Joined: 27 Jun 2011, 13:43

Re: Send message to all players from gadget

Post by LEDZ »

Thanks for the replies.
I know how this all works, I have a fully function synced->unsynced gadget now. What I'm trying to do is make it so that every player is responsible for tracking their own deaths/kills and then sending a message to everyone with things like "player 1 got 100 kills" etc.
I could do it so that each player keeps track of everybody's kills and deaths and displays the info to themselves (using Echo). However, I thought it would be more efficient to make each player responsible for their own updates.
Does this make sense to you?
User avatar
Niobium
Posts: 456
Joined: 07 Dec 2008, 02:35

Re: Send message to all players from gadget

Post by Niobium »

LEDZ wrote:Thanks for the replies.
I know how this all works, I have a fully function synced->unsynced gadget now. What I'm trying to do is make it so that every player is responsible for tracking their own deaths/kills and then sending a message to everyone with things like "player 1 got 100 kills" etc.
I could do it so that each player keeps track of everybody's kills and deaths and displays the info to themselves (using Echo). However, I thought it would be more efficient to make each player responsible for their own updates.
Does this make sense to you?
Having the gadget work it out for everyone is better because it doesn't add any network traffic, which is more important than negligible CPU load.

What exactly are you trying to do? Have players announce their kills? Show a scoreboard? Something else?
LEDZ
Posts: 66
Joined: 27 Jun 2011, 13:43

Re: Send message to all players from gadget

Post by LEDZ »

I've gone with SendCommands say bla bla now, it works ok.
Would the sending of messages contribute a lot to network traffic?
The widget tracks metal profit/loss from killing and losing units. It also tracks kills and deaths and announces certain milestones. I'll pastebin the code once I've tidied it up a bit.
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: Send message to all players from gadget

Post by zwzsg »

Niobium wrote:Also the synced side of a gadget can't get player names.
It sorta can, <PLAYER0> <PLAYER1> <PLAYER2> get replaced by the player name when used in Spring.SendMessage (does not work with Spring.Echo)
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Send message to all players from gadget

Post by knorke »

LEDZ wrote:However, I thought it would be more efficient to make each player responsible for their own updates.
Does this make sense to you?
imo not worth it.
Just have one gadget that tracks all kills and send the count/score to all players. If you care about only counting kills that happend in a players LOS, you can check that too in a gadget.
Post Reply

Return to “Lua Scripts”