Jump to content

when .take with item.get_vnum() --- shortcut lua function ?


Go to solution Solved by Syreldar,

Recommended Posts

Hello!

I need a function to use for:

when vnum_NPC.take with item.get_vnum() == vnum_item begin

that makes as short as possible the when-begin clause, because I need to include a lot of item values for the item.get_vnum() function. The function has to return a group of items divided into various arrays (e.g.: local name_item_group_1 = {vnum_item_1, 2 , 3, etc...}) for the various sub types of items (e.g.: swords, daggers, bows, etc...). The result has to look like that:

 

when vnum_NPC.take with new_function() begin

and not when vnum_NPC.take with item.get_vnum() == vnum_item_1 or item.get_vnum() == vnum_item_2 or item.get_vnum() == vnum_item_3 or etc... begin

 

Thank you in advance!

Link to comment
Share on other sites

  • Premium

        function right_item()
            local value_x = item.get_vnum()
            if value_x >= 85001 and value_x <= 85024 then
                return true
            else
                return false
            end
        end

 

if questname.right_item() == true then

Link to comment
Share on other sites

32 minutes ago, WeedHex said:

        function right_item()
            local value_x = item.get_vnum()
            if value_x >= 85001 and value_x <= 85024 then
                return true
            else
                return false
            end
        end

 

if questname.right_item() == true then

Stop to kill Lua please.

function right_item()
	return item.get_vnum() >= 85001 and item.get_vnum() <= 85024
end

And don't reply if you don't understand what exactly he want.

3 hours ago, TheMadNurse said:

that makes as short as possible the when-begin clause, because I need to include a lot of item values for the item.get_vnum() function. The function has to return a group of items divided into various arrays (e.g.: local name_item_group_1 = {vnum_item_1, 2 , 3, etc...}) for the various sub types of items (e.g.: swords, daggers, bows, etc...). The result has to look like that:

function CanTakeItem(itemVnum)
	local table_items = { 14500, 12000, 18000 }

	for _, v in pairs(table_items) do
		if v == itemVnum then
			return true
		end
	end
    return false
end

-- when npc_id.take with questName.CanTakeItem(item.get_vnum()) begin

 

  • Love 1
Link to comment
Share on other sites

  • Premium
  • Solution
34 minutes ago, Tasho said:
 

 


if v == itemVnum then
  return true
end

 

 

It's good practice to use understandable variable names.

quest QUESTNAME begin
	state start begin
		function CanTakeItem(vnum)
			local accepted_items = {14500, 12000, 18000};
			return table_is_in(accepted_items, vnum);
		end -- function

		when NPCVNUM.take with QUESTNAME.CanTakeItem(item.get_vnum()) begin
		--
		end -- when
	end -- state
end -- quest

table_is_in is a function which already exists in every questlib.lua.

This is the hidden content, please

Yours does the same thing, but custom-made functions are meant to be readable.

  • Metin2 Dev 4
  • Good 1
  • Love 4

 

"Nothing's free in this life.

Ignorant people have an obligation to make up for their ignorance by paying those who help them.

Either you got the brains or cash, if you lack both you're useless."

Syreldar

Link to comment
Share on other sites

10 hours ago, Syreldar said:

It's good practice to use understandable variable names.


quest QUESTNAME begin
	state start begin
		function CanTakeItem(vnum)
			local accepted_items = {14500, 12000, 18000};
			return table_is_in(accepted_items, vnum);
		end -- function

		when NPCVNUM.take with QUESTNAME.CanTakeItem(item.get_vnum()) begin
		--
		end -- when
	end -- state
end -- quest

table_is_in is a function which already exists in every questlib.lua.

This is the hidden content, please

Yours does the same thing, but custom-made functions are meant to be readable.

Thank you!

Your reply suits my request, but I need a way to manage different tables in order to make the function data sheet as clear as possible, like this way:

function CanTakeItem(vnum_item)
	local table_1 = {x1, x2, x3}
	local table_2 = {y1, y2, y3}

	return table_is_in(table_1, vnum_item)
	return table_is_in(table_2, vnum_item)
end

But it obviously is wrong.

I don't need range of values, but specified values.

 

Is the multiple "for index = 1, table.getn(table) do" for each table choice the only option ? Like this way:

function CanTakeItem(vnum_item)
	local table_1 = {x1, x2, x3}

	for index_1 = 1, table.getn(table_1) do
		if vnum_item == table_1[index_1] then
			return true
		else
			return false
		end
	end

	local table_2 = {y1, y2, y3}

	for index_2 = 1, table.getn(table_2) do
		if vnum_item == table_2[index_2] then
			return true
		else
			return false
		end
	end
end

when vnum_NPC.take with name_quest.CanTakeItem(item.get_vnum()) == true begin

Thank you in advance again!

Link to comment
Share on other sites

  • Premium
4 hours ago, TheMadNurse said:

Thank you!

Your reply suits my request, but I need a way to manage different tables in order to make the function data sheet as clear as possible, like this way:


function CanTakeItem(vnum_item)
	local table_1 = {x1, x2, x3}
	local table_2 = {y1, y2, y3}

	return table_is_in(table_1, vnum_item)
	return table_is_in(table_2, vnum_item)
end

 

 

The hell are you doing?

Just insert more values to the table, you don't need to make 2 tables..and that's not a range what the hell XD

quest QUESTNAME begin
	state start begin
		function CanTakeItem(vnum)
			local accepted_items = {14500, 12000, 18000, 1000, 2000, 7500, 9000, 18329};
			return table_is_in(accepted_items, vnum);
		end -- function

		when NPCVNUM.take with QUESTNAME.CanTakeItem(item.get_vnum()) begin
		--
		end -- when
	end -- state
end -- quest

You can add as many as you want, just stop killing the code holy..

 

"Nothing's free in this life.

Ignorant people have an obligation to make up for their ignorance by paying those who help them.

Either you got the brains or cash, if you lack both you're useless."

Syreldar

Link to comment
Share on other sites

I simply asked something more than what Syreldar showed me, storing values in more than one table, because I need to include a lot of values, just to have the data as clear as possible. It's not a problem if anyone from you can't afford that last my request, your support was enought helpful for me anyway. I'll follow Syreldar's function like this way:

local table =

{

x1, x2, x3, ---table_1

y1, y2, y3 ---table_2

z1, z2, z3 ---table_3

etc...

}

 

Tne two-return in the function I posted was simply an example to show you what I meant, I wrote that it was obviously wrong, and the "range of values" was referred to the other users who replied to me with that solution, including range of values (e.g.: item.get_vnum() >= x1 and item.get_vnum() <= x2).

 

Don't worry, I know how to lua code just as you all do, I could not be expert as you, but I developed a lot of usefull systems and I know what I'm doing too. By the way, thank you all again, expecially to Syreldar.

Link to comment
Share on other sites

  • Premium
2 hours ago, Cyber36 said:

Is the same possible with the use event?

Alias: when use with GetItemVnum(item.get_vnum()) begin [...]

 

King Regards

Cyber

 

No it's not, unfortunately.

6 hours ago, TheMadNurse said:

I simply asked something more than what Syreldar showed me, storing values in more than one table, because I need to include a lot of values, just to have the data as clear as possible. It's not a problem if anyone from you can't afford that last my request, your support was enought helpful for me anyway. I'll follow Syreldar's function like this way:


local table =

{

x1, x2, x3, ---table_1

y1, y2, y3 ---table_2

z1, z2, z3 ---table_3

etc...

}

 

Tne two-return in the function I posted was simply an example to show you what I meant, I wrote that it was obviously wrong, and the "range of values" was referred to the other users who replied to me with that solution, including range of values (e.g.: item.get_vnum() >= x1 and item.get_vnum() <= x2).

 

Don't worry, I know how to lua code just as you all do, I could not be expert as you, but I developed a lot of usefull systems and I know what I'm doing too. By the way, thank you all again, expecially to Syreldar.

function GetItemTables(index)
	local tables = {
		[1] = {1, 101, 29838, 134},
		[2] = {94729, 4398292, 3928, 12},
		[3] = {298, 4325, 34289, 438}
	};

	return tables[index];
end -- function

function DoesIndexContainItem(index, vnum)
	return table_is_in(QUESTNAME.GetItemTables(index), vnum);
end -- function

when NPCVNUM.take with QUESTNAME.DoesIndexContainItem(1|2|3, item.get_vnum()) begin
	---
end -- when

 

 

"Nothing's free in this life.

Ignorant people have an obligation to make up for their ignorance by paying those who help them.

Either you got the brains or cash, if you lack both you're useless."

Syreldar

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

Announcements



×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / 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.