Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, 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.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ MUSHclient
➜ Lua
➜ How to make a function's argument part of a string?
How to make a function's argument part of a string?
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
Posted by
| Eloni
(28 posts) Bio
|
Date
| Sun 25 Dec 2016 02:11 AM (UTC) Amended on Sun 25 Dec 2016 02:14 AM (UTC) by Eloni
|
Message
| I feel like this is a dumb question, but it has been bothering me for a few days. None of these work. I feel like I'm misunderstanding something very basic. I couldn't get this to work with a simple print function, either.
Code:
function killThis2(thing)
DoAfterSpecial(math.random(1,3), "Send('kill ', (thing))", 12)
end
Sends to mud:
------
Code:
something = ""
function killThis3(thing)
something = thing
DoAfterSpecial(math.random(1,3), "Send('kill ' .. (thing))", 12)
end
Returns error:
Run-time error
World: world.org
Immediate execution
[string "Timer: "]:1: attempt to concatenate global 'thing' (a nil value)
stack traceback:
[string "Timer: "]:1: in main chunk
----
Code:
function killThis4(thing)
DoAfterSpecial(math.random(1,3), "Send('kill ', [[thing]])", 12)
end
Sends to mud:
| Top |
|
Posted by
| Fiendish
USA (2,534 posts) Bio
Global Moderator |
Date
| Reply #1 on Sun 25 Dec 2016 09:24 AM (UTC) Amended on Sun 25 Dec 2016 09:25 AM (UTC) by Fiendish
|
Message
|
function say_something_in_three_seconds (thing)
DoAfterSpecial(3, "Send('say "..thing.."')", 12)
end
|
https://github.com/fiendish/aardwolfclientpackage | Top |
|
Posted by
| Eloni
(28 posts) Bio
|
Date
| Reply #2 on Sun 25 Dec 2016 12:33 PM (UTC) |
Message
| Thank you very much! This is exactly what I was looking for. | Top |
|
Posted by
| Nick Gammon
Australia (23,133 posts) Bio
Forum Administrator |
Date
| Reply #3 on Tue 27 Dec 2016 04:59 AM (UTC) |
Message
| I've been thinking about why this doesn't work:
function killThis2(thing)
DoAfterSpecial(math.random(1,3), "Send('kill ', (thing))", sendto.script)
end
The reason is the scope of the variable thing.
That variable exists only inside the function 'killThis2' - it is a parameter, and thus only has meaning while that function executes. However what `DoAfterSpecial` does is create a timer for the specified interval that sends:
The brackets aren't necessary. Anyway, the problem is, that in 3 seconds time thing won't exist in the context in which the timer is executed. Hence it will be nil.
Fiendish's suggest of concatenating the argument like this fixes it:
DoAfterSpecial(3, "Send('say "..thing.."')", sendto.script)
Now what gets queued up is:
This is because thing is being evaluated right now, inside the function (assuming thing is "hello Nick") and thus the correct word is queued for the timer. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | 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.
13,300 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top