Jump to content

Recommended Posts

I'm trying to write some code to apply item restrictions to a character's equip slots.

 

I'm pretty clueless with lua so this is probably going to be way off but this is my first attempt (which needless to say doesn't work).

local function TooHeavy ( inst, self )	if item = ("backpack") then		self:DropItem(item)	endendlocal fn = function(inst)	inst:ListenForEvent ( "onequipfn",  function() TooHeavy (inst) end )

Can someone tell me what I'm doing wrong or maybe point me in the right direction?

  • Equality comparisons use two equal signs (==)
  • You're passing the player prefab to the TooHeavy function, not the item; plus, the second parameter of the TooHeavy function would never be given a value
  • onequipfn is a variable that is a part of the 'equippable' component (which is added to items, not players). It is also not an event, but a variable that expects a function (you'd use: item.components.equippable.onequipfn = function(item, owner) print(tostring(owner).." equipped "..tostring(item)) end).
  • Events pass 2 parameters to callback functions (the function you give to ListenForEvent): the source of the event and a table of event data. For the 'equip' event, the event data has the item in key 'item'
Try this (I made the disallowed equipment a table so that it's easy to add more stuff):

local disallowed_equipment = {    "backpack",    --"other_prefab",}local function TooHeavy ( inst, data )    local item = data.item    if table.contains( disallowed_equipment, item.prefab ) then        inst.components.inventory:DropItem(item)    endend local fn = function(inst)     inst:ListenForEvent( "equip", TooHeavy )
Edited by squeek

Works perfectly, again thank you so much.

 

Is there a simple way to tell the difference between event variables and function variables in the files so I don't make the same mistake again? I had assumed only variables introduced with <function variable ()> were function based and had just guessed that onequipfn would be an event.

Works perfectly, again thank you so much.

 

Is there a simple way to tell the difference between event variables and function variables in the files so I don't make the same mistake again? I had assumed only variables introduced with <function variable ()> were function based and had just guessed that onequipfn would be an event.

No problem.

Events are fired through the EntityScript function PushEvent (see scripts/entityscript.lua, all prefabs in the game are an EntityScript), and any other entity can listen for those events. The parameters for event callback functions are always: 1) the source of the event 2) event data as a table, meaning you will only ever need to define event listener functions as: function( inst, data ) [note: the parameter names don't actually matter, for clarities sake, you could define the parameter list like so: function( event_source, event_data_table )]. Unfortunately, like with most/all of Don't Starve, there is no documentation available for what events exist, so to find an event, you need to look in the relevant game Lua files (to see where the "equip" event is pushed, check scripts/components/inventory.lua and search for "equip" [with the double quotes; it will be in the Inventory:Equip function])

Callback functions are usually variables within components (see this thread for an explanation of components) that will get called whenever certain things happen. When defining a callback function, you'll have to look at the component's Lua to see what parameters will get passed to the function. So, for onequipfn, look in scripts/components/equippable.lua and search for onequipfn. Then, from the prefab, you simply set the onequipfn variable of its equippable component to your callback function (inst.components.equippable.onequipfn = YourFunction).

Because of the lack of documentation, being able to read and understand the base game files is really important when trying to mod for this game. When to use event listeners and when to use callbacks is really a matter of knowing when one method would work while the other wouldn't.

Edited by squeek

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
  • Create New...