[Home] [Downloads] [Search] [Help/forum]

Gammon Software Solutions forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  confusing with the global variable in plugin

Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

confusing with the global variable in plugin

[Reply to this subject]  Reply to this subject   [New subject]  Start a new subject   [Refresh] Refresh page


Posted by Yeedoo   China  (12 posts)  [Biography] bio
Date Sun 19 Feb 2012 02:29 AM (UTC)  quote  ]
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!
[Go to top] top

Posted by Twisol   USA  (2,230 posts)  [Biography] bio
Date Reply #1 on Sun 19 Feb 2012 03:36 AM (UTC)  quote  ]
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
[Go to top] top

Posted by Nick Gammon   Australia  (18,797 posts)  [Biography] bio   Forum Administrator
Date Reply #2 on Mon 20 Feb 2012 01:34 AM (UTC)  quote  ]
Message
Plugins and the "main" world share different script spaces.

- Nick Gammon

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

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

Posted by Wahnsinn   USA  (8 posts)  [Biography] bio
Date Reply #4 on Tue 28 Feb 2012 01:24 PM (UTC)  quote  ]
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?
[Go to top] top

Posted by Nick Gammon   Australia  (18,797 posts)  [Biography] bio   Forum Administrator
Date Reply #5 on Tue 28 Feb 2012 06:54 PM (UTC)  quote  ]
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
[Go to top] top

Posted by Wahnsinn   USA  (8 posts)  [Biography] bio
Date Reply #6 on Wed 29 Feb 2012 02:49 AM (UTC)  quote  ]
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?
[Go to top] top

Posted by Nick Gammon   Australia  (18,797 posts)  [Biography] bio   Forum Administrator
Date Reply #7 on Wed 29 Feb 2012 03:22 AM (UTC)  quote  ]
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
[Go to top] top

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


1,431 views.

[Reply to this subject]  Reply to this subject   [New subject]  Start a new subject   [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

[Home]

Written by Nick Gammon - 5K

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( http://www.gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Internet Contents Rating Association (ICRA) - 2K]    [Web site powered by FutureQuest.Net]