Project Zomboid

Project Zomboid

TchernoLib B41
Tchernobill  [developer] 8 Jun, 2023 @ 7:19pm
Global Object simple interface
What are Global Objects ?
Objects that can be added / removed / evolve over time.
They also work both for solo and multiplayer.
Among vanilla global objects are: farming plants, camping fire, rain water collectors.

Why this mod ?
Generic and specific code are deeply intricated in vanilla classes.
Main generic handled problematics are:
- object load and unload when approaching
- object load when loading the game
- specific data transfer between server and client
< >
Showing 1-4 of 4 comments
Tchernobill  [developer] 8 Jun, 2023 @ 7:48pm 
Simplified interface
Use one unique keyword per GlobalObject system
e.g. "farming" is a vanilla system keyword, "portal" is a modded system keyword.
This is referred as key in the code and below.
Use your own key in your mods.

Server
see SGlobalObjectCreator.lua (top of the file) for interface or:

Server side initialisation
e.g. SMoatsGO.lua from Moats
e.g. SPortalCore.lua from Portal Gun
if isClient() then return end require 'GlobalObject/SGlobalObjectCreator' ShGO.initSGO(key,objectModDataKeys)
objectModDataKeys:[optional] list of parameter names to save over time.
Those are mostly the parameters passed through args in ShGO.createCGO and other client functions described below.
e.g. see Moats.modDataKeys from Moats

Client
see CGlobalObjectCreator.lua (top of the file) for interface or:

Client side initialisation
e.g. CMoatsGO.lua from Moats
e.g. CPortalCore.lua from Portal Gun
require 'GlobalObject/CGlobalObjectCreator' ShGO.initCGO(key)
Last edited by Tchernobill; 2 Apr, 2024 @ 7:35am
Tchernobill  [developer] 8 Jun, 2023 @ 7:51pm 
Client Global Object Creation / Removal / Update
Code below must be called during the game.
It requires init phases to be completed.
Client side Global Object instance creation
e.g. CPortalManager.lua from Portal Gun
local pos = ShGO.convertPosToTable(targetSquare)--targetSquare is of type IsoGridSquare local args = {}--args must be a table filled with POD[en.wikipedia.org]s (no Java Items, no cross references..) ShGO.createCGO(key, pos, character, args)--character is the source IsoGameCharacter, can be nil
Note: pos can be directly the IsoGridSquare instance.
Note: this is only for the initial object instance creation. At server restart (or game restart in solo) as well as when the associated square is re-loaded, creation is handled by the generic code. You have nothing to do. You can still receive the potential creation messages if you have specific code to do, see client events.

Client side Global Object instance removal
e.g. CPortalManager.lua from Portal Gun
ShGO.removeCGO(key, pos, character)--same params details as creation.

Client side Global Object instance update
ShGO.updateCGO(key, pos, character, args)--same params details as creation.
Last edited by Tchernobill; 8 Jun, 2023 @ 8:07pm
Tchernobill  [developer] 8 Jun, 2023 @ 7:52pm 
Client events for your specific code
Events subscription requires ShGO.initCGO(key) to have been called before.

'OnObjectAdded_'..key
New client event on specific Global Object creation.
Use event registration as usual:
Events['OnObjectAdded_'..key].Add(myClientSideModdedFunction) Events['OnObjectAdded_'..key].Remove(myClientSideModdedFunction)
myClientSideModdedFunction = function (luaObj) <MyJob> end
luaObj:luaObject associated to the IsoObject
Very important: the IsoObject may not be loaded at this point, only the lua object.
All data that can be used while the object is not loaded must be in the luaObject.
To know if the IsoObject is loaded, you have to store the luaObject and poll by calling
local isoObj = luaObj:getIsoObject()
Do this the less you can to maintain optimum cpu cost.

'OnObjectRemoved_'..key
New client event on specific Global Object deletion:
Use event registration as usual:
Events['OnObjectRemoved_'..key].Add(myClientSideModdedFunction) Events['OnObjectRemoved_'..key].Remove(myClientSideModdedFunction)
myClientSideModdedFunction = function (luaObj) <MyJob> end
luaObj: notice it is likely not valid anymore (because the IsoObject may already be removed or altered)

Contextual menu interface when a player right-click a square including a Global Item of that system
'OnCGlobalObjectContextMenu_'..key
New Event can be used client side.
Use event registration as usual:
Events['OnCGlobalObjectContextMenu_'..key].Add(myClientSideModdedFunction) Events['OnCGlobalObjectContextMenu_'..key].Remove(myClientSideModdedFunction)
myClientSideModdedFunction = function (context, isoObj, playerNum, x, y, test) <MyJob> end
context: the usual context to add your option
isoObj: client side Global Object of type 'key' you clicked on
playerNum: local num of the clicking player
x: position on screen
y: position on screen
test: boolean, vanilla, not sure, you probably should not do anything when test is true
Last edited by Tchernobill; 8 Jun, 2023 @ 8:26pm
Tchernobill  [developer] 8 Jun, 2023 @ 8:15pm 
Server events for your specific code
This is very advanced, do not bother with it until you are familiar with the points above.
Event subscription requires ShGO.initCGO(key) to have been called before.

'OnSGlobalObjectReceiveCommand_'..key
New event can be used server side.
Use event registration as usual:
Events['OnSGlobalObjectReceiveCommand_'..key].Add(myServerSideModdedFunction) Events['OnSGlobalObjectReceiveCommand_'..key].Remove(myServerSideModdedFunction)
myServerSideModdedFunction = function (command, playerObj, args, result, objectSystemInstance) <MyJob> end
command: string (e.g. 'add_'..key)
playerObj: server side player obj related to the client initiating the command
args: client command arguments
result: depends on the command:
  • add_-> nil if failed or created lua object if succeeded
  • remove_-> lua object about to be removed from system.
  • custom command -> nil
objectSystemInstance: the instance of the object system for ease of use. (you probably should neglect that, it seems broken I still do not know why)

e.g. SMoatsContinuity.lua from Moats.
Last edited by Tchernobill; 2 Apr, 2024 @ 7:57am
< >
Showing 1-4 of 4 comments
Per page: 1530 50