Page 1 of 1
How to Add info to a table?
Posted: Fri Mar 08, 2013 1:47 pm
by noobbotter
I'm creating my own custom function that will check for any nearby players within a certain distance. If found, and that player isn't already in the table, I want to add that player's name to a table along with the time the player was found. My end goal here is that the bot will compare the table when it sees someone nearby and if that player is already in the table, and the entry was created more than (for example) 5 minutes ago, then my bot will decide to move along vice continue farming that area.
Then of course, I'd also have to have a check where if a player is not detected that is already in the table, then it would remove that entry from the table.
I think I can figure most of it out except I don't know how I would add or delete an entry from a table. Thanks for any help you can provide.
Re: How to Add info to a table?
Posted: Fri Mar 08, 2013 1:52 pm
by Administrator
table.insert() will take care of that for you.
Re: How to Add info to a table?
Posted: Fri Mar 08, 2013 3:04 pm
by noobbotter
Thanks for giving me a place to start with that. I created a rough function to check nearby players and add them into a table. The table construct is basically going to look like this:
playerTable = {
"Someguy" = {foundtime =
time found, updatetime =
time updated},
"Anotherplayer" = {foundtime =
time found, updatetime =
time updated}
}
I'm going to use the player's name as the name of a table within the playerTable. Here's what I have so far:
Code: Select all
function checkNearbyPlayers()
local objectList = CObjectList();
objectList:update();
for i = 0,objectList:size() do
local obj = objectList:getObject(i);
local curdistance = distance(player.X,player.Z,player.Y,obj.X,obj.Z,obj.Y)
if obj ~= nil and obj.Type == PT_PLAYER and obj.Address ~= player.Address and obj.Name ~= "<UNKNOWN>" and (600 > distance(player.X,player.Z,player.Y,obj.X,obj.Z,obj.Y) ) then
local tempPlayer = obj.Name
if table.contains(playerTable, tempPlayer) then
if ((os.time() - playerTable.tempPlayer.foundtime)/60) > 5 then
nearbyPlayer = "yes";
else
playerTable.tempPlayer.updatetime = os.time();
end
else
table.insert(playerTable, tempPlayer={"foundtime"=os.time(), "updatetime"=os.time()});
end
end
end
end
Then I'll have another function that will periodically check for if the nearbyPlayer variable is "yes" and if it is, my bot will move on to a different area. Then yet another function that will periodically check for any players in my table who hasn't been updated in over 5 minutes, and if not, then it will assume they're no longer around and will remove that entry from the table. Does the code above look like it will work? I want to figure out as much of this as I can before I get home and get on my computer.
Thanks.
Re: How to Add info to a table?
Posted: Fri Mar 08, 2013 7:14 pm
by Administrator
If you're going to use the players' name as the key, there's a few things you could do to make the code easier to read and manage.
To insert/update:
Code: Select all
playerTable[name] = {"foundtime"=os.time(), "updatetime"=os.time()}
To check if exists:
Code: Select all
if( playerTable[name] ) then
-- Exists
end
To remove:
Re: How to Add info to a table?
Posted: Fri Mar 08, 2013 11:57 pm
by rock5
Also, this is wrong "playerTable.tempPlayer.foundtime". What you mean is "playerTable[tempPlayer].foundtime" because you want to use the value inside tempPlayer not tempPlayer itself.
Also this is the wrong usage of table.insert
Code: Select all
table.insert(playerTable, tempPlayer={"foundtime"=os.time(), "updatetime"=os.time()});
table.insert comes in 2 forms
- table.insert(table, value)
table.insert(table, key, value)
The first adds the 'value' to the end of an indexed table, eg. if a 'table' has 5 indexed values ([1]=value1,[2]=value2,etc.) then the top command will add [6]=value.
The second command allows you to specify the key as well so it will add key=value.
Also you don't surround variables with quotes.
So your command should be
Code: Select all
table.insert(playerTable, tempPlayer, {foundtime=os.time(), updatetime=os.time()});
You could also do
Code: Select all
playerTable[tempPlayer] = {foundtime=os.time(), updatetime=os.time()};
Re: How to Add info to a table?
Posted: Sat Mar 09, 2013 12:26 am
by lisa
noobbotter wrote:I created a rough function to check nearby players and add them into a table.
The GM detect userfunction tracks players, have a look at it.
http://www.solarstrike.net/phpBB3/viewt ... =27&t=2516
Re: How to Add info to a table?
Posted: Sat Mar 09, 2013 8:37 am
by noobbotter
Thanks everyone. I'll resume working on this once I figure out this other little issue I'm having.
In my onload section, I have a custom onLeaveCombat function which among other things will call my checkBags() function. Basically, if bags 1 thru 3 are full at the time of leave combat it will set variable named bagsFull.
I'm having trouble getting this variable available so that when my bot reaches a certain waypoint, if this variable is set it will go to certain waypoint tag. I put in a printf and when the printf runs, it stops the waypoint file and reports that the variable is nil. I tried adding at the end of the functions "return bagsFull" to return the value of it back up to it's calling function but that wouldn't work either.
I guess I can strip down the code and post the important parts to see if someone can tell me what I'm doing wrong.
Code: Select all
<onload>
local bagsFull = "no";
function checkBags()
for slot = 61, 150 do
local item = inventory.BagSlot[slot]
if item.Empty then
count = count + 1
end
end
if count == 0 then
print("No space left in bags 1 thru 3.") -- this prints successfully.
bagsFull = "yes";
end
return bagsFull -- tried it with and without this.
end
settings.profile.events.onLeaveCombat = function()
checkBags();
TundraApplyPots();
return bagsFull -- tried it with and without this.
end
</onload>
<!-- # 23 --><waypoint x="2561" z="-7113" y="578" tag="centerpoint">
<!-- insert code to check for full bags or players nearby -->
-- printf("bagsfull is %s.\n",bagsFull);
if nearbyPlayer == "yes" or bagsFull == "yes" then
__WPL:setForcedWaypointType("TRAVEL");
GetMounted();
__WPL:setWaypointIndex(__WPL:findWaypointTag("returnpath"))
end
</waypoint>
So that's a very stripped down version of my waypoint file. I only included the lines that are critical to being able to change waypoints when the bags are full and I can't get it to recognize the variable when it checks it. Thoughts?
EDIT: Nevermind the above question about the variable... i finally got it figured out. If I'm going to return the variable from within a function, then I don't declare it at the beginning. Is that right?
Re: How to Add info to a table?
Posted: Sat Mar 09, 2013 10:04 am
by rock5
Your initial problem is you declared bagsFull local to the onLoad so it is not available anywhere else. So you should have left off the 'local'.
The better way, though,
is to return a value. In that case you should declare bagsFull local to the checksBag function.
There is an obvious mistake in your logic. If all you do onleavecombat is set the variable but you don't otherwise do anything with that variable, then why check it onleavecombat? At the waypoint you can just call the function directly without any need for onleavecombat.
Code: Select all
if nearbyPlayer == "yes" or checkBags() == "yes" then
Re: How to Add info to a table?
Posted: Sat Mar 09, 2013 12:27 pm
by noobbotter
That's a good point. Why run checkbags() on every onleavecombat when I can just run it at the one waypoint where I check it?
Thanks for clearing up the variable declaration thing. I've always had trouble with that.
Once side question here... I'm trying to use a carpentry encyclopedia inside my house in a script and it seems to initially click on it but something happens right away so it doesn't sit down and begin reading. Here's what I have for that:
Code: Select all
startTime = os.time();
inventory:useItem(202712); -- Carpentry Encyclopedia
repeat
newTime = os.time();
yrest(10000);
until (newTime - startTime)/60 >= 30
any thoughts on what would cause it to stop using it immediately after it clicks it?
EDIT:::
Nevermind. it seems to be working now. dunno what caused it to not work that one time. Good thing is that it's not critical and my bot won't die if it doesn't work.