Register forum user name Search FAQ

Gammon Forum

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.
 Entire forum ➜ MUSHclient ➜ Lua ➜ confusing with the global variable in plugin

confusing with the global variable in plugin

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


Posted by Yeedoo   China  (12 posts)  Bio
Date Sun 19 Feb 2012 02:29 AM (UTC)
Message
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!
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #1 on Sun 19 Feb 2012 03:36 AM (UTC)
Message
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.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by Nick Gammon   Australia  (23,120 posts)  Bio   Forum Administrator
Date Reply #2 on Mon 20 Feb 2012 01:34 AM (UTC)
Message
Plugins and the "main" world share different script spaces.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Yeedoo   China  (12 posts)  Bio
Date Reply #3 on Tue 21 Feb 2012 05:03 AM (UTC)
Message
Resolved
I tried to visit a global table that was not initialized correctly.
Thank you ,all of you
Top

Posted by Wahnsinn   USA  (8 posts)  Bio
Date Reply #4 on Tue 28 Feb 2012 01:24 PM (UTC)
Message
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?
Top

Posted by Nick Gammon   Australia  (23,120 posts)  Bio   Forum Administrator
Date Reply #5 on Tue 28 Feb 2012 06:54 PM (UTC)
Message
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.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Wahnsinn   USA  (8 posts)  Bio
Date Reply #6 on Wed 29 Feb 2012 02:49 AM (UTC)
Message
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?
Top

Posted by Nick Gammon   Australia  (23,120 posts)  Bio   Forum Administrator
Date Reply #7 on Wed 29 Feb 2012 03:22 AM (UTC)
Message
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.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Wahnsinn   USA  (8 posts)  Bio
Date Reply #8 on Wed 29 Feb 2012 03:39 AM (UTC)
Message
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!
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.


26,469 views.

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

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.