Jump to content
Angelix

Non-existent ID check

Recommended Posts

Is there a way to check if an item ID exists or not? Within the scripts of course.

 

I have this code:

function MazeChest(Player, Item)
	if GetChaFreeBagGridNum(Player) <= 0 then
		SystemNotice(Player, "You need at least one free inventory slot to open ["..GetItemName(GetItemID(Item)).."].")
		UseItemFailed(Player)
		return
	end
	if MazeChestVar[GetItemID(Item)] == nil then
		SystemNotice(Player, "Please contact administrator since ["..GetItemName(GetItemID(Item)).."] currently has no use.")
		UseItemFailed(Player)
		return
	end
	local Random = math.floor(math.random(1, table.getn(MazeChestVar[GetItemID(Item)])))
	GiveItem(Player, 0, MazeChestVar[GetItemID(Item)][Random], 1, 5)
	Notice("["..GetChaDefaultName(Player).."] has opened ["..GetItemName(GetItemID(Item)).."] and obtained ["..GetItemName(MazeChestVar[GetItemID(Item)][Random]).."].")
end

This line will randomly give an ID given on the variables associated with it.

	local Random = math.floor(math.random(1, table.getn(MazeChestVar[GetItemID(Item)])))

Lets say this are the items:

MazeChestVar[1] = {2, 3, 4, 5, 6, 7, 8, 9}

It will all work out fine if none of those ID's are commented out in ItemInfo. I can then use this code to do a check if the ItemInfo ID is not zero.

	local Random = 0
	while Random == 0 do
		Random = math.floor(math.random(1, table.getn(MazeChestVar[GetItemID(Item)])))
	end

Although my problem still persists if I comment out a line in ItemInfo, but the "random" will still generate a number which results in the variable "Random" not being zero.

 

My question is, how can I validate the existence of that number in ItemInfo? Lets say the variable results in "1", this will get it out of the loop and try to give that item to the player, but the player will not get that item since it's a non-existent item in ItemInfo (it does not exist if it's commented). How can I do a check for that?

 

Yes, I know I can purposely remove items from the variable storage, but I don't want that. I want to be able to put all items inside the variable and just comment out lines in ItemInfo and later just update the ItemInfo in order for players to be able to get those items, instead of having to modify a lot of scripts. Please note this is just an example, as I want to use this in around 16 chests with each chest containing around 25 items.

Edited by Angelix

Share this post


Link to post
Share on other sites
function MazeChest(Player, Item)
	local Random = 0
	local tab = MazeChestVar[GetItemID(Item)]
	local errorMsg
	if not tab then
		errorMsg = string.format("Please contact administrator since [%s] currently has no use.",GetItemName(GetItemID(Item)))
	elseif GetChaFreeBagGridNum(Player) <= 0 then
		errorMsg =  string.format("You need at least one free inventory slot to open [%s].",GetItemName(GetItemID(Item)))
	end
	if errorMsg then
		SystemNotice(Player,errorMsg)
		UseItemFailed(Player)
		return
	end	
	while (GetItemName(Random) == "unknown" )do
		Random = tab[math.floor(math.random(1, table.getn(tab)))]
	end
	GiveItem(Player, 0, MazeChestVar[GetItemID(Item)][Random], 1, 5)
	Notice(string.format("[%s] has opened [%s] and obtained [%s].",GetChaDefaultName(Player),GetItemName(GetItemID(Item)),GetItemName(Random)))
end

 

  • Like 1

Share this post


Link to post
Share on other sites

@Billy, thank you! Never thought of the "Unknown" name, hahah. Regarding the rest of the script, guess I learned something new today, will adapt to new scripts from here on, thanks for helping out. :)

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...