always come out this erro!! parameter is always nil!!Why?

Posted by Lucas828 on Thu 04 Jun 2009 12:04 AM — 11 posts, 42,990 views.

#0
Run-time error
World: myworld
Immediate execution
[string "Script file"]:5: bad argument #1 to 'GetVariable' (string expected, got nil)
stack traceback:
[C]: in function 'GetVariable'
[string "Script file"]:5: in function 'mywork'
[string "Trigger: "]:1: in main chunk
Error context in script:
1 : function mywork(var1)
2 : --print (_VERSION);
3 :
4 :
5*: local v = GetVariable(var1);
6 : local mymaster = GetVariable("master");
7 : local myskills = GetVariable("skills");
8 :
9 : --print (mymaster);

I am newbie.Just want to trigger a patten and pass the matched content to function mywork.
why the var1 is always nil? I am confused, because I can see the matched content when choose the send to output!!
Australia Forum Administrator #1
Can you show the trigger please? http://mushclient.com/copying

I am guessing you are calling mywork from the trigger. Triggers take 3 arguments (or 4 in Lua if you want), like this:


function mywork (name, line, wildcards, styles)

local v = GetVariable(wildcards [1]);

end -- function


The third argument is the array of wildcards (the first one is the trigger name, and the second one is the matching line). Thus the first wildcard is wildcards [1]. However that doesn't totally explain what you are seeing, because var1 in your case would have been the trigger name. If you show the whole trigger it might become clearer.
#2
<triggers
muclient_version="4.12"
world_file_version="15"
date_saved="2009-06-03 23:26:14"
>
<trigger
enabled="y"
lines_to_match="2"
match="*hell*"
send_to="12"
sequence="100"
>
<send>mywork "%2";</send>
</trigger>
</triggers>
</muclient>
#3
yes, you are right, I just simply pass the %1 as the parameter to function mywork.

previously, I use zmud. this is the first i get in touch with mushclient. what is tigger name and line? why I need to pass these to function?


I need some simple example, in *.lua files to study mushclient.
Australia Forum Administrator #4
You didn't copy the trigger as I suggested in the link http://mushclient.com/copying did you? Otherwise the date and time wouldn't be there.

Quote:

what is tigger name and line? why I need to pass these to function?


You are really combining two approaches here. Read up on http://mushclient.com/scripting for more details.

One approach is to use "send to script" (which you did), in which case the logical thing is to put the script instructions in the send box itself.

The other approach is to put the function name into the "script" box in the trigger, in which case MUSHclient passes the function name, and matching line to the script. You don't have to do it.

The trigger below demonstrates that (see http://mushclient.com/pasting for how to copy that back into the client):


<triggers>
  <trigger
   enabled="y"
   match="*hell*"
   script="myworld"
   sequence="100"
  >
  </trigger>
</triggers>


However more simply, just do what you want in the trigger send box, like this:


<triggers>
  <trigger
   enabled="y"
   match="*hell*"
   send_to="12"
   sequence="100"
  >
  <send>

local v = GetVariable("%1")
local mymaster = GetVariable("master")
local myskills = GetVariable("skills")

print (mymaster)
print (myskills)
print (v)

</send>
  </trigger>
</triggers>


Now you don't need a script file at all, and you don't need to call a function - just do it all inline.
#5
the result is same.

I copy the function script in the send box.

when I type say yousayhellooo

and choose to send to script

v is still nil

when I choose to send to output,

the result appears on the screen:

local v = GetVariable("ooo");
local mymaster = GetVariable("master")
local myskills = GetVariable("skills")

print (v)
print (mymaster)
print (myskills)




<triggers>
<trigger
enabled="y"
match="*hell*"
send_to="2"
sequence="100"
>
<send>

local v = GetVariable("%2");
local mymaster = GetVariable("master")
local myskills = GetVariable("skills")

print (v)
print (mymaster)
print (myskills)
</send>
</trigger>
</triggers>

Australia Forum Administrator #6
OK, you say v is nil. Does the variable "ooo" actually exist? If not, v will be nil.
#7
Yes, "ooo" exists!

If I choose to send to output.

When I type say yousayhellooo.

I can see the "ooo" on the screen!

But if I choose to send to script. the v is nil.

why?

Need I have to register the mushclient?
#8
oh, the question is that GetVariable does fail.

when choose to send to script.

print(%2);--that works, show the "ooo" on the screen

local v = GetVariable(%2);-- nil appears!
#9
yes, GetVariable(%2) failed!

local v = %2;
print (v); -- that works!!

thanks Nick!!
USA #10
You still need to use quotes around the %num for the variables.

The %1 and %2 are directly replaced with whatever is captured. This means that it is not displayed as a string, but swapped out entirely. If the second parameter matches "foo", then here is how the following will show up when processed:

local v = GetVariable(%2)
becomes
local v = GetVariable(foo)

local v = GetVariable("%2)
becomes
local v = GetVariable("foo")

The first example will then attempt to pass the string stored in the variable named foo to the GetVariable function. Since this is not set, nil is passed to the function, causing the error.