Jump to content
  • 0

Help with a simple lua script


garganeo

Question

Hi guys, first off thanks alot to every1 contributing to the mangos project - it is loveable .. honestly ! I can't express how happy I am to see so many gamers working on the same thing yet from very different perspectives and with different goals .. it is amazing !

now to my point: I am using mangoszero, on ubuntu server (everything works like a charm) and I am trying to add a few scripts to customize my server a bit...
I need a lua script that deletes equipped items from a player on death.... (pretty hardcore ikr) .. that's all !
I started learning lua to be able to make it myself, but no luck so far :( all I found out was I need to include the right server hook - which for my case should be:

SERVER_HOOK_DEATH = 6

but I dont know how to write the rest :S In terms of algorithm I want the script to do this : when a player dies, destroy all equipped gear. Any help would be most appreciated !!!
 

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

By the way. It seems that you may have been looking at some arcemu lua scripts and documentation. SERVER_HOOK_DEATH does not exist on Eluna.
Instead you must use PLAYER_EVENT_ON_KILL_PLAYER and PLAYER_EVENT_ON_KILLED_BY_CREATURE both to get the same effect.
http://www.elunaengine.com/Global/RegisterPlayerEvent.html

 

Here is the PM if someone is interested:
 

1 hour ago, Rochet2 said:

You should start out with the event.
The event was on death, so you find out what register function to use and what event id to use.
From the documentation you can see this: http://www.elunaengine.com/Global/RegisterPlayerEvent.html

There is no on death event, but there are kill player and killed by creature, which together are all the situations a player can die in. The event IDs are 6 and 8.

Then you call the registering function with the eventid and a function you want to execute on that event.
The event documentation should show you what the parameters should be for the function you register.

For example:


local function MyFunction(event, killer, killed)
    -- code
end
RegisterPlayerEvent(6, MyFunction)
RegisterPlayerEvent(8, MyFunction)


Now you can start thinking about the logic that happens on that event.
Just like in the topic here https://www.getmangos.eu/forums/topic/10168-help-with-a-simple-lua-script/?do=findComment&comment=77164

we will use a loop.
From looking at the documentation here http://www.elunaengine.com/Player/GetItemByPos.html
you can see that to get the player equipment you need to use
bag 255 and slots 0 to 18
So you need to use a loop. I recommend looking at  http://lua-users.org/wiki/ForTutorial
The lua-users wiki tries to explain how the language features work through comprehensive examples.

Below we have the code with the loop:


local function MyFunction(event, killer, killed)
    local bag = 255
    for slot = 0, 18 do
        local item = killed:GetItemByPos( bag, slot )
    end
end
RegisterPlayerEvent(6, MyFunction)
RegisterPlayerEvent(8, MyFunction)

In this code we have set bag to be 255 and slot will be iterated from 0 to 18
Inside the for-loop we will call GetItemByPos on killed, so the player who died, and we pass the bag and the slot to it.
Notice how we call killed:GetItemByPos( bag, slot ), not Player:GetItemByPos( bag, slot )
The documentation uses Player:GetItemByPos( bag, slot ) to show that this method is only available to a Player type object.
If killed is a creature the method will not exist and you will receive an error.

If a slot does not contain an item, then item will be nil. Otherwise it will be a valid item object that we can call methods on.
We need to check if the item is actually an item instead of nil. Only then we can delete it.
 


if item then
    killed:RemoveItem(item, 1)
end


I hope you have time to take a look on your own at how the language works.
There are good resources out there like
http://lua-users.org/wiki/TutorialDirectory - A good hands on tutorial for language features with examples.
http://www.lua.org/manual/5.2/ - A full reference manual for the language for any details you need on the language.
Also something I find very very useful are the online tools to run lua code. This allows you to try out things with the language instead of having to start up a server every time.
For example: https://repl.it/languages/lua and http://www.lua.org/demo.html

For eluna you should read
http://www.elunaengine.com/index.html
https://github.com/ElunaLuaEngine/Eluna/blob/master/docs/USAGE.md
https://github.com/ElunaLuaEngine/Eluna/blob/master/docs/IMPL_DETAILS.md
as they cover some basics, some details that are good to know and the documentation of what you can use.

 

I hope this was enough of a preemptive explanation.

 

Link to comment
Share on other sites

An important question before we go further here: Do you want the gear broken or deleted? From what you said it seems you want it deleted.

 

While I myself am not very good with LUA (yet!) I would imagine that from what I have seen of it you would need something like the following pattern:

Server hook for death, on death each gear slot is targeted in the script followed by deletion of the gear in that slot. I would imagine the script ahould also have a check to see if gear exists in that slot and if not, no action is taken.

I would imagine that can all he done via LUA. @Rochet2 would be the one to better answer you here.

Link to comment
Share on other sites

Thank you for the quick response! ... well completely destroyed .. like you die - you repop with no items on. Only the ones in your backpack and bank are left untouched. And yes the way you described the algorithm would suffice for my case I think ... I've seen similar code in Player.cpp - like selecting an item and destroying it (something like:
for (int i = EQUIPMENT_SLOT_START; i < EQUIPMENT_SLOT_END; ++i)
    DestroyItem(i, update)

but this is no good for me since I cant translate it into lua ....

Link to comment
Share on other sites

Your threads are all over the place.

You can search the Eluna documentation for functions:
http://www.elunaengine.com/?search=getitem

One of the functions listed is http://www.elunaengine.com/Player/GetItemByPos.html
which you can use on the dying player.
From the documentation you can see what bag and slot numbers to use to get the equipment. Just loop through all of them and each time check if the item could be fetched and delete it.

Deletion happens with http://www.elunaengine.com/Player/RemoveItem.html

This should help you with figuring out how registering/hooking works:
https://github.com/ElunaLuaEngine/Eluna/blob/master/docs/USAGE.md#registering-functions-to-events

Link to comment
Share on other sites

Awesome !!! Im checking this out right now will post back (here only) and I am sory about the extra posts I tryed deleting some of them when I figured they were in the wrong sections but I couldnt .. wont happen again though. Thanks alot !

Link to comment
Share on other sites

Thanks alot for everything man !!!

I wasn't able to do it by myself even with the help of the above posts, and so Rochet2 has basically written it for me in a personal message. After this, I finally had the following code that solved the issue :

local function RemoveItemsOnDeath (event, killer, killed) 			
	
	local bag = 255
	for slot = 0, 18
	    local item = killed:GetItemByPos( bag, slot )
    if item then
	    killed:RemoveItem(item, 1)
	end
	end
end

RegisterPlayerEvent(6, RemoveItemsOnDeath)
RegisterPlayerEvent(8, RemoveItemsOnDeath)

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Privacy Policy Terms of Use