'EventHandler.SendNetworkEvent("giveitem:BFG9000");'

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!

Moderator: GZDoom Developers

Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

Please bear in mind that the people helping you do not automatically know how much you know. You may be asked to upload your project file to look at. Don't be afraid to ask questions about what things mean, but also please be patient with the people trying to help you. (And helpers, please be patient with the person you're trying to help!)
Post Reply
User avatar
Matt
Posts: 9696
Joined: Sun Jan 04, 2004 5:37 pm
Preferred Pronouns: They/Them
Operating System Version (Optional): Debian Bullseye
Location: Gotham City SAR, Wyld-Lands of the Lotus People, Dominionist PetroConfederacy of Saudi Canadia
Contact:

'EventHandler.SendNetworkEvent("giveitem:BFG9000");'

Post by Matt »

From the EventHandler description in the wiki:
Note that any event handler can handle any name, so if you need to send a string you can simply check for Name starting with a value, for example:

Code: Select all

EventHandler.SendNetworkEvent("giveitem:BFG9000");
In the event handler, you check that the event's Name starts with "giveitem:", extract the following value and take action.
The example code is tantalizingly suggestive of the possibilities but I can't make heads or tails of how to use it based on the underlined description.

Is "giveitem" a special word here, or the name of a sample custom function? Is "BFG9000" converted into an int from the string, or is it treated as a class name, or something else? Or is it that you give a BFG and this returns the number of BFGs given???
User avatar
phantombeta
Posts: 2088
Joined: Thu May 02, 2013 1:27 am
Operating System Version (Optional): Windows 10
Graphics Processor: nVidia with Vulkan support
Location: Brazil

Re: 'EventHandler.SendNetworkEvent("giveitem:BFG9000");'

Post by phantombeta »

It just passes the item to give as part of the name parameter. The wiki likely doesn't have an example for this because there was no built-in string splitting function until I suggested it somewhat recently.
Here's some actual EventHandler code for that:

Code: Select all

version "3.2.5"

class ItemGiveEventHandler : EventHandler {
    override void NetworkProcess (ConsoleEvent e) {
        if (e.Player < 0 || !(players [e.Player]) || !(players [e.Player].mo))
            return;

        let player = players [e.Player].mo;

        Array<string> command;
        e.Name.Split (command, ":");

        if (command.Size () != 2 || !(command [0] ~== "giveitem"))
            return;


        player.GiveInventory (command [1], 1);
    }
}
This should work. With this, you can do "netevent giveitem:BFG9000" in the console to give yourself a BFG.
User avatar
Matt
Posts: 9696
Joined: Sun Jan 04, 2004 5:37 pm
Preferred Pronouns: They/Them
Operating System Version (Optional): Debian Bullseye
Location: Gotham City SAR, Wyld-Lands of the Lotus People, Dominionist PetroConfederacy of Saudi Canadia
Contact:

Re: 'EventHandler.SendNetworkEvent("giveitem:BFG9000");'

Post by Matt »

Thanks, that's exactly what I was hoping for!

May I add your code to the wiki?
User avatar
phantombeta
Posts: 2088
Joined: Thu May 02, 2013 1:27 am
Operating System Version (Optional): Windows 10
Graphics Processor: nVidia with Vulkan support
Location: Brazil

Re: 'EventHandler.SendNetworkEvent("giveitem:BFG9000");'

Post by phantombeta »

Sure. Should probably remove the version statement and change it to Allman style, though.
Post Reply

Return to “Scripting”