First,I am a newbie, not only for mushclient but also for lua
I tried to visit a _G var created by my script from a plugin. but failed
Then i tried to create a global variable from a plugin
such as in the function OnPluginInstall
but when i print the _G table in mushclient, i couldnt find the var that i created in plugin
by the way, the mushclient vision is 4.81
How to fix that? Any advice will be appreciated, Thank you!
Can you post your code please? If it's too large, feel free to dump it at pastebin.com and give us the link. Without seeing the code, any number of things could be happening.
Plugins and the "main" world share different script spaces.
Resolved
I tried to visit a global table that was not initialized correctly.
Thank you ,all of you
I'm having a rough time with an attempt at a global variable in a plugin as well. Trying to follow the various instructions in the forum closely, I'm using this:
<![CDATA[
require "serialize"
ch = {}
-- Some functions that assign values to ch items
-- In desperation I even added
SetVariable ("ch", "ch = " .. serialize.save_simple (ch))
-- to one that I knew was setting values to ensure that SetVariable was being called
-- -----------------------------------------------------------------
-- Built-in callbacks
-- -----------------------------------------------------------------
function OnPluginInstall ()
assert (loadstring (GetVariable ("ch") or "")) ()
if ch.HP == nil then
ColourNote ("red", "", "Initializing ch")
ch = {
name = "Unknown",
race = "Unknown",
class = "Unknown",
hasMagic = false,
isFighting = false,
align = "Neutral",
HP = { cur = 0, total = 100, },
MA = { cur = 0, total = 0, },
stats = { STR = 1, AGI = 1, INT = 1, HEA = 1, WIS = 1, CHA = 1, }
}
else
ColourNote ("yellow", "", "Loading " .. ch.name)
end
end
function OnPluginSaveState ()
SetVariable ("ch", "ch = " .. serialize.save_simple (ch))
end
Yet every time I load the world I see the red message that the ch variable is being initialized. Can someone educate me on where I'm going wrong?
This works for me:
<muclient>
<plugin
name="test_variables"
author="Nick Gammon"
id="d391d2f095192d2d48f9870e"
language="Lua"
purpose="testing initializing variables"
save_state="y"
date_written="2012-02-28 18:12:26"
requires="4.81"
version="1.0"
>
</plugin>
<!-- Script -->
<script>
<![CDATA[
require "serialize"
ch = {}
-- -----------------------------------------------------------------
-- Built-in callbacks
-- -----------------------------------------------------------------
function OnPluginInstall ()
assert (loadstring (GetVariable ("ch") or "")) ()
if ch.HP == nil then
ColourNote ("red", "", "Initializing ch")
ch = {
name = "Unknown",
race = "Unknown",
class = "Unknown",
hasMagic = false,
isFighting = false,
align = "Neutral",
HP = { cur = 0, total = 100, },
MA = { cur = 0, total = 0, },
stats = { STR = 1, AGI = 1, INT = 1, HEA = 1, WIS = 1, CHA = 1, }
}
else
ColourNote ("yellow", "", "Loading " .. ch.name)
end
end
function OnPluginSaveState ()
SetVariable ("ch", "ch = " .. serialize.save_simple (ch))
end
]]>
</script>
</muclient>
I only see the initializing message the first time. Make sure you have "save_state" set to y (line in bold) or it won't work.
This isn't a global variable by the way, it is a plugin variable saved in the plugin state file.
Thanks so much Nick, you nailed it. Once I added save_state to the plugin it finally started working as expected.
The docs say that creating a variable without "local" make it global, or is that in reference to some scripting location other than a plugin? What would be the proper way to expose my ch variable here so that it can be read and written by other plugins?
Global just means it is in the "global environment" table (effectively, a global variable) as opposed to being local to the current block.
It is nothing to do with whether or not the variable is shared between plugins. Each plugin has its own script space (and hence its own variables), by design.
Excellent, I appreciate the clarification, thank you. I'm really enjoying working with MUSHclient and Lua, and the fact that you reply so promptly in the forums and with such helpful information is just... priceless. Great work!