[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  How to make a function's argument part of a string?
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

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] Refresh page


Posted by Eloni   (28 posts)  [Biography] 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:


kill nil

------
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:

kill thing
[Go to top] top

Posted by Fiendish   USA  (1,641 posts)  [Biography] 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
[Go to top] top

Posted by Eloni   (28 posts)  [Biography] 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.
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] 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:


Send('kill ', (thing))


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:


Send('say hello Nick')



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
[Go to top] 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.


1,723 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]