How to Add info to a table?
-
- Posts: 527
- Joined: Fri Aug 31, 2012 1:15 pm
How to Add info to a table?
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.
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.
- Administrator
- Site Admin
- Posts: 5307
- Joined: Sat Jan 05, 2008 4:21 pm
Re: How to Add info to a table?
table.insert() will take care of that for you.
-
- Posts: 527
- Joined: Fri Aug 31, 2012 1:15 pm
Re: How to Add info to a table?
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:
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.
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
Thanks.
- Administrator
- Site Admin
- Posts: 5307
- Joined: Sat Jan 05, 2008 4:21 pm
Re: How to Add info to a table?
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:
To check if exists:
To remove:
To insert/update:
Code: Select all
playerTable[name] = {"foundtime"=os.time(), "updatetime"=os.time()}
Code: Select all
if( playerTable[name] ) then
-- Exists
end
Code: Select all
playerTable[name] = nil;
Re: How to Add info to a table?
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
table.insert comes in 2 forms
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 beYou could also do
Also this is the wrong usage of table.insert
Code: Select all
table.insert(playerTable, tempPlayer={"foundtime"=os.time(), "updatetime"=os.time()});
- 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()});
Code: Select all
playerTable[tempPlayer] = {foundtime=os.time(), updatetime=os.time()};
- Please consider making a small donation to me to support my continued contributions to the bot and this forum. Thank you. Donate
- I check all posts before reading PMs. So if you want a fast reply, don't PM me but post a topic instead. PM me for private or personal topics only.
- How to: copy and paste in micromacro
________________________
Quote:- “They say hard work never hurt anybody, but I figure, why take the chance.”
- Ronald Reagan
Re: How to Add info to a table?
The GM detect userfunction tracks players, have a look at it.noobbotter wrote:I created a rough function to check nearby players and add them into a table.
http://www.solarstrike.net/phpBB3/viewt ... =27&t=2516
Remember no matter you do in life to always have a little fun while you are at it ![Wink ;)](./images/smilies/icon_e_wink.gif)
wiki here http://www.solarstrike.net/wiki/index.php?title=Manual
![Wink ;)](./images/smilies/icon_e_wink.gif)
wiki here http://www.solarstrike.net/wiki/index.php?title=Manual
-
- Posts: 527
- Joined: Fri Aug 31, 2012 1:15 pm
Re: How to Add info to a table?
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.
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?
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>
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?
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.
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
- Please consider making a small donation to me to support my continued contributions to the bot and this forum. Thank you. Donate
- I check all posts before reading PMs. So if you want a fast reply, don't PM me but post a topic instead. PM me for private or personal topics only.
- How to: copy and paste in micromacro
________________________
Quote:- “They say hard work never hurt anybody, but I figure, why take the chance.”
- Ronald Reagan
-
- Posts: 527
- Joined: Fri Aug 31, 2012 1:15 pm
Re: How to Add info to a table?
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:
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.
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
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.
Who is online
Users browsing this forum: Ahrefs [Bot] and 1 guest