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
➜ General
➜ Setting world variables from plugins
Setting world variables from plugins
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
Posted by
| Larkin
(278 posts) 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? | Top |
|
Posted by
| Ked
Russia (524 posts) 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. | Top |
|
Posted by
| Nick Gammon
Australia (23,133 posts) 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 | Top |
|
Posted by
| Larkin
(278 posts) 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. | Top |
|
Posted by
| Ked
Russia (524 posts) 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. | Top |
|
Posted by
| Larkin
(278 posts) 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! | 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.
20,182 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top