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


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  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] Refresh page


Posted by Gore   (207 posts)  [Biography] 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?
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] 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
[Go to top] top

Posted by Gore   (207 posts)  [Biography] 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 :(
[Go to top] top

Posted by Shaun Biggs   USA  (644 posts)  [Biography] 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.
[Go to top] top

Posted by Nick Gammon   Australia  (22,975 posts)  [Biography] 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
[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.


13,899 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 HostDash]