EndCore Framework

en-consumables

What food, drink, medicine and armour do when used: animations, survival stat changes, effects over time and sickness risk.

en-consumables decides what eating, drinking and medicine actually do. When you use a food, drink, medical or armour item, you play an animation with a prop in hand and see a progress bar you can cancel. The item is only taken, and its effects applied, if the bar finishes.

Items can restore hunger and thirst, heal you, set armour, purge radiation, treat infection or boost immunity. They can also apply effects over time, like a course of antibiotics, give you a temporary stamina boost, or carry a risk of making you sick (murky water, dodgy burgers). The Medicine skill makes healing stronger, and the Iron Gut perk lowers the chance of getting sick.

At a glance

Depends onen-core, en-inventory
Start afterBoth of the above
Database tablesNone
Config filesconfig/shared.lua
Works withen-skills (healing and foodRisk bonuses, Medicine XP)

How it works

Using an item

Every key in Config.items is registered as a usable item in en-inventory. When a player uses one:

  1. The server checks the player isn't dead and isn't within Config.cooldown of their last use.
  2. The client plays the animation and progress bar. You can't use items while swimming, ragdolled or dead.
  3. If the bar completed, the server removes the item, then applies the effects. If the item is already gone, nothing happens.

The server rejects a "finished" reply that arrives in less than 80% of the item's duration, and gives up waiting after 30 s.

Effects

Effects change en-core's survival stats; see Survival system.

  • Positive health is multiplied by 1 + healing bonus. It's given on a 0–100 scale and converted to the ped's real health range on the client.
  • armour sets armour to at least that value, scaled by the item's metadata.durability.
  • Negative hunger, thirst, radiation and infection values remove that amount.

Effects over time

An overTime block repeats its effects every interval seconds, ticks times. Using the same item again restarts the course rather than stacking it. A course stops if the player dies or unloads.

Risk

After use, a risk block rolls this chance:

lua
chance = risk.chance * (1 - math.min(0.9, foodRiskBonus))

If it hits, the player gets the listed infection or radiation and sees risk.message.

Configuration

KeyDefaultWhat it does
Config.cooldown1.5Seconds between uses of any consumable
Config.animationsdrink, can, eat, eatCan, pill, bandage, medkit, armour{ dict, clip, flag, prop = { model, bone, pos = vec3, rot = vec3 } }; prop is optional
Config.items14 itemsConsumable definitions, keyed by item name

Item fields

FieldWhat it does
labelProgress bar text
durationMilliseconds
animationKey in Config.animations
walkAllow walking while using (default true)
effectsApplied once on finish: hunger, thirst, health, armour, radiation, infection, immunity
overTime{ interval, ticks, radiation?, infection?, immunity?, health?, hunger?, thirst? }
risk{ chance = 0..1, infection?, radiation?, message }
stamina{ seconds }: keeps stamina topped up for that long

Default items

ItemWhat it does
water, canned_soda, bread, canned_beans, mreHunger and thirst
dirty_waterThirst, with a 35% chance of +8 infection
energy_drink90 s of stamina
burgerHunger, with an 8% chance of getting sick
bandage+15 health
painkillers+5 health, then +3 every 10 s, 6 times
first_aid_kit+50 health, -5 infection, no walking
antibiotics-10 infection, +5 immunity, then -6 infection every 30 s, 5 times
rad_pills-20 radiation, then -15 every 30 s, 4 times
armour100 armour, no walking

Adding consumables

lua
Config.items.boiled_rice = {
    label = 'Eating rice', duration = 5000, animation = 'eatCan',
    effects = { hunger = 40, thirst = -8 },
}

Config.items.raw_meat = {
    label = 'Eating raw meat', duration = 6000, animation = 'eat',
    effects = { hunger = 25 },
    risk = { chance = 0.5, infection = 12, message = 'You shouldn\'t have eaten that raw.' },
}

Config.items.morphine = {
    label = 'Injecting morphine', duration = 4000, animation = 'pill', walk = false,
    effects = { health = 20 },
    overTime = { interval = 5, ticks = 10, health = 4 },
}
Warning

Each item must also exist in en-inventory/data/items.lua, or players can never get or use it. See Add an item. Give medical items the medical category so they train the Medicine skill.

Exports

Server

ExportArgumentsReturns
ApplyEffectssource, effects, reason?nil. Applies an effects table with the same keys as Config.items[*].effects
IsConsumableitemNameboolean

Client

ExportReturns
IsBusy()boolean: a consumable is being used

Events

EventSidePayload
en-consumables:server:consumedServer-localsource, itemName, definition
en-consumables:client:effectsClient{ health?, armour? }
en-consumables:client:staminaClientseconds

en-skills uses en-consumables:server:consumed to award Medicine XP for medical items.

Examples

A medic NPC that patches players up without using an item:

lua
-- server
AddEventHandler('mymedic:server:treat', function()
    local source = source
    exports['en-consumables']:ApplyEffects(source, { health = 60, infection = -25, immunity = 10 }, 'medic')
end)

Stop players from opening your menu mid-bite:

lua
-- client
RegisterCommand('mymenu', function()
    if exports['en-consumables']:IsBusy() then return end
    -- open the menu
end)

Track what players eat:

lua
AddEventHandler('en-consumables:server:consumed', function(source, itemName, definition)
    if definition.risk then
        print(('%s risked eating %s'):format(GetPlayerName(source), itemName))
    end
end)