Register forum user name Search FAQ

Gammon Forum

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 ➜ Modifying variables based on function argument

Modifying variables based on function argument

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


Posted by Gore   (207 posts)  Bio
Date Tue 20 Mar 2007 09:06 PM (UTC)
Message
Basically I've got a bunch of functions that go along the lines of:

function reset.drink ()
  if bal.drink == .5 then
    bal.drink = true
  end
end

function reset.salve ()
  if bal.salve == .5 then
    bal.salve = true
  end
end

function reset.herb ()
  if bal.herb == .5 then
    bal.herb = true
  end
end

And so on and so forth, and I was wondering if there was anyway to condense them into one function, such as

function reset.balance (type)

this way I could save space in my script file, any suggestions?
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #1 on Tue 20 Mar 2007 09:11 PM (UTC)
Message
Sure.


function reset.balance(type)
  if bal[type] == .5 then
    bal[type] = true
  end
end


In Lua, for a table t, t.str is a shortcut for t["str"]. If you do t[var], that means "look up key var in table t'.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Gore   (207 posts)  Bio
Date Reply #2 on Wed 21 Mar 2007 01:45 PM (UTC)
Message
Lua is amazing. Do you know if there's anyway to do that in vbscript? Because I've been trying to figure that out for a long time :(
Top

Posted by Shaun Biggs   USA  (644 posts)  Bio
Date Reply #3 on Wed 21 Mar 2007 02:58 PM (UTC)
Message
I'm not terribly sure if there's an easier way to do it other than what I'm going to suggest, but there's a simple workaround I've used for a long time. Just an extra value to the function saying what variable to modify. Then just have a case section picking the variable to modify.


function reset( action )
  select action
    case "drink"
      if bal.drink == .5 then
        bal.drink = true
      end
    case "salve"
      if bal.salve == .5 then
        bal.salve = true
      end
    case "herb"
      if bal.herb == .5 then
        bal.herb = true
      end
  end select
end


My only concern is that the value for bal.foo has both decimal values and boolean values in there. It kind of obscures the code a bit, and is harder to follow than just a normal test of bal.foo == 1 for true. Granted, I haven't seen the rest of the script, so it might make sense to have it that way.

It is much easier to fight for one's ideals than to live up to them.
Top

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #4 on Wed 21 Mar 2007 08:02 PM (UTC)
Message
Quote:

Do you know if there's anyway to do that in vbscript?


It would be easy if you had arrays that could have named indexes (rather than numbers). I think there is such a thing, I can't remember offhand what it is.

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


19,314 views.

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

Go to topic:           Search the forum


[Go to top] top

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