Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to "verify" your details, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Entire forum
➜ MUSHclient
➜ Lua
➜ Pulling along set of directions
Pulling along set of directions
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
Posted by
| Thalir
(13 posts) Bio
|
Date
| Wed 15 Aug 2007 12:36 AM (UTC) |
Message
| For example, I have a set of different directions running from my home to city A, b, C:
n, e, s, w, u, d <--- in this format
If I pull a cart north, there are two messages:
i) You pull a cart north <---- When I start
ii) You have pulled a cart north <---- Once I've done pulling and am in the next room.
How might I tackle this, to trigger cart pulling along the set of directions I provide, plus recognize I've moved into a new room and use the next available direction?
Would it be possible to store these dirs in a text file and read from them?
I'm guessing the solution is to somehow use the key order by putting the dirs in a lua table.
=================================
In summary:
-How to pull a cart, following the directions?
-How to switch the set of directions (cities)?
-And a way to stop and start, but still know which dir I'm up to?
================================
I'd also like to express my utmost gratitude for all those helpful people replying to my previous posts, I am very glad mushclient has such great technical support.
| Top |
|
Posted by
| Shaun Biggs
USA (644 posts) Bio
|
Date
| Reply #1 on Wed 15 Aug 2007 01:43 AM (UTC) |
Message
| I would just create a table with the directions listed in order. cartdirs={"n", "e", "s", "w", "u", "d"} Then have a regex trigger match="^You pull a cart (north|south|east|west|up|down)$"
Then just have a function like this:
function nextpull()
if #cartdirs > 0 then
Send( "pull cart "..cartdirs[1] )
table.remove( cartdirs, 1 )
end
end
To make it start or resume, just have an alias to enable the trigger and send the first direction. To pause it, just disable the trigger. |
It is much easier to fight for one's ideals than to live up to them. | Top |
|
Posted by
| Thalir
(13 posts) Bio
|
Date
| Reply #2 on Wed 15 Aug 2007 02:17 AM (UTC) |
Message
| Thanks, I'd like to ask, what does
#cartdirs > 0
mean? Is that a check to see if the table is empty | Top |
|
Posted by
| Shaun Biggs
USA (644 posts) Bio
|
Date
| Reply #3 on Wed 15 Aug 2007 02:24 AM (UTC) |
Message
| You are correct. #varname tells you how many elements are in the table. if #cartdirs == 0, then the table is empty. |
It is much easier to fight for one's ideals than to live up to them. | Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #4 on Wed 15 Aug 2007 04:53 AM (UTC) |
Message
| As a minor note, the #table syntax only works for arrays i.e. tables with only numeric keys. And strictly speaking, the # operator also only returns the highest integer key in the contiguous range starting from 1. (At least, for sparse arrays.)
E.g.
[david@thebalrog:~]$ cat test.lua
t = {}
t.a = 1
t.b = 1
print(#t)
t = {}
t[1] = 1
t[2] = 1
print(#t)
t[10] = 1
print(#t)
[david@thebalrog:~]$ lua test.lua
0
2
2
This can be tricky, because e.g.
[david@thebalrog:~]$ cat test.lua
t = {}
t[10] = 1
print(#t)
[david@thebalrog:~]$ lua test.lua
0
The proper way to check if a table is empty, "proper" meaning one that will always work, is to see if next returns nil for that table. If next(t) == nil, then the table is empty; if next(t) ~= nil, the table has something in it. |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | Top |
|
Posted by
| Shaun Biggs
USA (644 posts) Bio
|
Date
| Reply #5 on Wed 15 Aug 2007 05:24 AM (UTC) |
Message
| True, but in this case, the array has to be sequential and starting with an index of 1 for the function to work, so #t will do the job better than next(t) will. It is important to make the distinction between the two though, so thanks for clarifying. |
It is much easier to fight for one's ideals than to live up to them. | Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #6 on Wed 15 Aug 2007 05:46 AM (UTC) |
Message
| Main reason I pointed it out was that I've been bitten in the neck a few times by this. It's interesting to follow Lua list traffic a bit because this is something of a point of contention.
For instance, if you set indices 1, 2 and 4, you get #t == 4, but if you set 1, 2 and 10, you get #t == 2... some people think the # operator isn't quite right because it doesn't always do what you think it should do, but then sometimes it does something you thought it wasn't supposed to do, and all that stuff. |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | Top |
|
Posted by
| Shaun Biggs
USA (644 posts) Bio
|
Date
| Reply #7 on Wed 15 Aug 2007 06:16 AM (UTC) |
Message
| You can also use table.setn(t, 20) to set the size of the table to 20 elements. At that point, you can have 2 elements filled, and #t would return 20. And even then table.remove(t) will remove the item in #t (even if there was nothing there to begin with), so #t will then return 19. Point being... be careful with data set construction and proper removal of elements. |
It is much easier to fight for one's ideals than to live up to them. | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
20,286 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top