stepping/rotating through a table (tab targeting)

Post Reply
BeardKingX
Posts: 11
Joined: Fri Feb 15, 2013 6:29 am

stepping/rotating through a table (tab targeting)

Post by BeardKingX »

I have a table that holds all the mobs in a room. I echo this list to a miniconsole. Each mob echoed to the miniconsole is followed by a link to target that mob. My my current target is highlighted within the miniconsole. This allows me to see all the mobs in a room, click to target any mob, and shows me what i am currently targeting.

I would like to create a hotkey that rotates my target, moving from my current target to the next target within this table, but I have been unable to figure out a way to do this and am looking for suggestions.

BeardKingX
Posts: 11
Joined: Fri Feb 15, 2013 6:29 am

Re: stepping/rotating through a table (tab targeting)

Post by BeardKingX »

I am getting there, now just need to figure out how to loop back to start.
Code: [show] | [select all] lua
local curtarg = 0
for i,j in pairs(room_objects) do
	if i == mytarget then
		curtarg = 1
	elseif curtarg == 1 and next(room_objects) ~= nil then
		send("SETTARGET " .. i)
		break
	elseif curtarg == 1 and next(room_objects) == nil then
		--loop back to start
	end
end

User avatar
SlySven
Posts: 1023
Joined: Mon Mar 04, 2013 3:40 pm
Location: Deepest Wiltshire, UK
Discord: SlySven#2703

Re: stepping/rotating through a table (tab targeting)

Post by SlySven »

Can't you use, what is it, #room_object to get a count of the objects in the room_objects table - this ONLY works for tables (indexes starting at 1 and increasing monotonically) - I think that is how you use the '#' operator but if I am wrong I'm sure someone will say... :-)

BeardKingX
Posts: 11
Joined: Fri Feb 15, 2013 6:29 am

Re: stepping/rotating through a table (tab targeting)

Post by BeardKingX »

it should work, but it always shows the table as empty (zero).

even if I run through something simply like this:
Code: [show] | [select all] lua
for i,j in pairs(room_objects) do
	echo("\n" .. i)
end
echo("\n" .. #room_objects)
it will echo every object but still return a zero on #room_objects.
Last edited by BeardKingX on Wed Mar 09, 2016 6:16 am, edited 1 time in total.

BeardKingX
Posts: 11
Joined: Fri Feb 15, 2013 6:29 am

Re: stepping/rotating through a table (tab targeting)

Post by BeardKingX »

apparently the # operator only counts entries with integer keys, which room_objects does not have.

here is a working script
Code: [show] | [select all] lua
local target_count = 0
local tab_target = {}

	for k,v in pairs(room_objects) do 
		target_count = target_count + 1 
		tab_target[target_count] = k
	end
	for k,v in pairs(tab_target) do
		if v == mytarget and k+1 <= target_count then
			send("SETTARGET " .. tab_target[k+1])
			break
		elseif k == target_count then
			send("SETTARGET " .. tab_target[1])
			break
		elseif k == target_count and mytarget == nil then
			send("SETTARGET " .. tab_target[1])
			break
		end
	end
I have a trigger that sets mytarget when the mud server replies to the SETTARGET command.

Post Reply