[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]  General
. . -> [Subject]  Setting world variables from plugins
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Setting world variables from plugins

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


Posted by Larkin   (278 posts)  [Biography] bio
Date Fri 16 Mar 2007 03:30 PM (UTC)
Message
I've got a plugin to parse ATCP codes sent by Lusternia. I want to pass the data I parse to world variables in one or more world files. Is there a way I can force the variables to go to the worlds?

Currently, I'm using an alias, i_setvar, to "fake it," but I'd like to be able to do this with a function. I think it might be possible to load a world object by GUID and set the variable through the object, but if there is an easier or more general solution, I'd really like to hear it. For example, I know that using GetPluginVariable with an empty first argument will grab a variable from the current world (which confuses me when multiple worlds are open), but I don't see an equivalent method for setting a variable in a particular plugin or world. What am I missing?
[Go to top] top

Posted by Ked   Russia  (524 posts)  [Biography] bio
Date Reply #1 on Fri 16 Mar 2007 07:35 PM (UTC)
Message
You can't set variables in a plugin from outside that plugin, but to set a variable (or do anything else) in a separate world you just need to get that world's "object" and call its methods, like you would on the current world:


-- presuming that the world's id is saved in a variable in the
-- current world
local w = GetWorldById(GetVariable("WorldID"))

w:SetVariable("SomeVar", "something")


You'd need checks to ensure that the world you want actually exists, of course, but that's the general idea.
[Go to top] top

Posted by Nick Gammon   Australia  (21,391 posts)  [Biography] bio   Forum Administrator
Date Reply #2 on Sat 17 Mar 2007 06:25 AM (UTC)
Message
Quote:

I don't see an equivalent method for setting a variable in a particular plugin or world.


It's a design decision to not allow it. Plugins are supposed to be self-contained, and not affect other plugins, or the main world either for that matter.

Thus, reading variables in the main world, or other plugins, is allowed, but not writing.

Ked's method will probably work, however, as a work-around.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Larkin   (278 posts)  [Biography] bio
Date Reply #3 on Sat 17 Mar 2007 11:22 AM (UTC)
Message
Okay. I can live with that, and I understand design decisions. :)

This ATCP plugin has to be a plugin because of the need for packet-level control (Thanks, Ked, by the way), but it wants to set the health, mana, etc values in the world file to override/augment the prompt values. One of the rare special cases, I guess.
[Go to top] top

Posted by Ked   Russia  (524 posts)  [Biography] bio
Date Reply #4 on Sat 17 Mar 2007 05:45 PM (UTC)
Message
If all you want is to set variables in the same world as the one where the plugin is installed, then you can use MXP entities instead of variables. Those are essentially the same thing as variables but can be set/read from anywhere. My own ATCP plugin actually does just that with code like (using health as an example):


local maxHealth      = "f2ac05048dc" -- holds maximum health level
local currHealth     = "12b987b460e" -- holds the current health level

function SetCurrHealth(val)
  --[[
  Sets the current health value
  ]]
  local val = val or ""
  SetEntity(currHealth, tonumber(val))
end

function SetMaxHealth(val)
  local val = val or ""
  SetEntity(maxHealth, tonumber(val))
end

function GetCurrHealth()
  --[[
  Returns the current health value, or nil if
  it isn't set yet.
  ]]
  local val = GetEntity(currHealth)
  if val == "" then
    return nil
  else
    return tonumber(val)
  end
end

function GetMaxHealth()
  local val = GetEntity(maxHealth)
  if val == "" then
    return nil
  else
    return tonumber(val)
  end
end


I put the code above into a lua file in Mushclient/lua/mylibrary folder and require it in the ATCP plugin and anywhere I need access to the ATCP values.
[Go to top] top

Posted by Larkin   (278 posts)  [Biography] bio
Date Reply #5 on Sat 17 Mar 2007 07:36 PM (UTC)
Message
I saw the entity functions in your code and just decided that I wanted to do it my way, not even bothering to look and see what those functions were even doing. The MXP entity method could come in handy with a couple other plugins I have in mind, too.

Thanks again!
[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.


5,354 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]