 |
Yetzederixx Beginner
Joined: 19 Oct 2010 Posts: 14 Location: Sulphur, LA
|
Posted: Wed Oct 27, 2010 5:31 am
Function Return Values in Lua |
I wrote a function today because I couldn't figure out how %ismember searched through the variables. So I defaulted my lists to alphabetical and adopted a binary search method. It always returned true, well I thought it was, because in the trigger that calls the function the true always sent the true commands. Well after some testing it wasn't the code that was wrong as #echo @FN would echo true/false as appropriate. When you want to return true/false you have to do so by returning 1 or 0 if you are using zScript to test the return value, at least this is how I got it to work.
Code: |
--Param 1: Name of character to search for
--Param 2: String list to search in
maxE = table.maxn(zs.param(2))
low = 1 --Lua starts it's arrays at 1 not 0
high = maxE
while low < high do
mid = math.floor(low + ((high - low) / 2))
if zs.param(2)[mid] < zs.param(1) then
low = mid + 1
else
high = mid
end
end
if ((low < maxE) and (zs.param(2)[low] == zs.param(1))) then
return 1
else
return 0
end
|
Here's a snippit of the zScript that uses the the function
Code: |
#switch (@binIsMember(%1, @BlackRobes)) {#cw 96} |
|
|
|
 |
Zugg MASTER

Joined: 25 Sep 2000 Posts: 23379 Location: Colorado, USA
|
Posted: Wed Oct 27, 2010 4:58 pm |
Yes, functions always need a return result. Are you reporting a problem, or just letting people know?
The %ismember function in zScript will be faster than your Lua function. %ismember does a fast hash-table lookup, which will be faster than a binary search method and is independent of the list sort method. At least in v3.31. You didn't mention what version of CMUD you are using. In the older 2.37 the %ismember was much slower. |
|
|
 |
Yetzederixx Beginner
Joined: 19 Oct 2010 Posts: 14 Location: Sulphur, LA
|
Posted: Wed Oct 27, 2010 8:50 pm |
Initially I had it return true or false, actually it was:
Code: |
return ((low < maxE) and (zs.param(2)[low] == zs.param(1))) |
In the zScript would always read it as a return value of true no matter what, even when testing with a known false value. When I would #echo @binIsMember(char, var) it would echo the appropriate respose of true or false. So I guess I'm sort of reporting a bug, or at least something to be aware of.
Reguardless it was still a good exercise in working with Lua, and I got to read a bunch of good helpfiles reguardless, thanks for the input.[/code] |
|
|
 |
Zugg MASTER

Joined: 25 Sep 2000 Posts: 23379 Location: Colorado, USA
|
Posted: Thu Oct 28, 2010 10:49 pm |
Hmm, interesting. The true/false boolean return should have worked too. Looks like maybe an issue with the Lua boolean values not being converted to the zScript 0/1 values. I'll add that to the bug list.
|
|
|
 |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|