Project Zomboid

Project Zomboid

getZombieByID
 This topic has been pinned, so it's probably important
dhert  [developer] 30 Jul, 2023 @ 7:18pm
How to use in your mods
Q: As a modder, I want to use this in my own mods. How do I do that?

It's easy!

First, require the file:
local zombies = require('ZedCache')

When you have a zombie's ID that you want to get as another client, simply do:
local myZombie = zombies:get(ID)

If no zombie is found, `nil` is returned. You must check for this. That's it, I told you it was easy!

A full working example is below.
Last edited by dhert; 30 Jul, 2023 @ 7:27pm
< >
Showing 1-4 of 4 comments
dhert  [developer] 30 Jul, 2023 @ 7:29pm 
Let's start with the client side.

FILE: lua/client/ZedCacheTest.lua
local zombies = require('ZedCache') local OnWeaponHitCharacter = function(player, target, weapon, damage) if isClient() then print("OnWeaponHitCharacter CLIENT: " .. player:getOnlineID()) print("SENDING: " .. target:getOnlineID()) print(" Weapon: " .. tostring(weapon)) print(" Damage: " .. tostring(damage)) sendClientCommand("ZedShare", "Test", { zed = target:getOnlineID(), weapon = weapon, damage = damage }) end end Events.OnGameBoot.Add(function() Events.OnWeaponHitCharacter.Add(OnWeaponHitCharacter) if isClient() then -- Commands for syncing events local Commands = { ZedShare = { Test = function(args) local player = getPlayerByOnlineID(args.id) if player and not player:isLocalPlayer() then local zombie = zombies:get(args.zed) if zombie then print("GOT: " .. tostring(zombie:getOnlineID())) else print("NO zombie") end end end } } local onServerCommand = function(module, command, args) if Commands[module] and Commands[module][command] then Commands[module][command](args) end end Events.OnServerCommand.Add(onServerCommand) end end)

In the code above, when the event "OnWeaponHitCharacter" is triggered, we send a server command with the zombie's ID that we hit, the weapon being used, and how much damage is being done. The server then will relay all of this data to all connected players.

When a player receives the "ZedShare/Test" Server command, they use the provided Zombie's Online ID to retrieve their object from the cache and you can then perform any action as necessary.
Last edited by dhert; 30 Jul, 2023 @ 7:41pm
dhert  [developer] 30 Jul, 2023 @ 7:34pm 
The server's "relay" is below.

FILE: lua/server/ZedCacheServer.lua
if not isServer() then return end -- our structure local Commands = { ZedShare = { Test = function(source, args) local myId = source:getOnlineID() -- sync it to all other clients local players = getOnlinePlayers() local player for i=0, players:size()-1 do player = players:get(i) -- send to all other players if player and player:getOnlineID ~= myId then sendServerCommand(player, "ZedShare", "Test", { id = myId, zed = args.zed, damage = args.damage, weapon = args.weapon }) end end end } } -- Add our Event Events.OnClientCommand.Add(function(module, command, source, args) if Commands[module] and Commands[module][command] then Commands[module][command](source, args) end end)

The above is a simply relay for the server to send the command to all connected players.

The server DOES NOT build a zombie cache, only clients. Zombies do not exist like that on the server, and this would be pointless.
dhert  [developer] 30 Jul, 2023 @ 7:35pm 
I hope the above helps!

If you have any questions or issues with integrating this into your mod, please feel free to comment here.

Any comments here that are not directly related towards mod development will be deleted.
Glytch3r 31 Jul, 2023 @ 1:19am 
This is a great idea.
This might be a solution i could use for ny zeds to sync
< >
Showing 1-4 of 4 comments
Per page: 1530 50