Setting world variables from plugins

Posted by Larkin on Fri 16 Mar 2007 03:30 PM — 6 posts, 26,250 views.

#0
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?
Russia #1
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.
Australia Forum Administrator #2
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.
#3
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.
Russia #4
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.
#5
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!