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


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Saving Accelerator

Saving Accelerator

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


Posted by Ruckinfok   (7 posts)  [Biography] bio
Date Mon 21 Feb 2011 06:12 PM (UTC)

Amended on Mon 21 Feb 2011 06:35 PM (UTC) by Ruckinfok

Message
Ok, so I'm making a plugin to use accelerator to keybind some stuff, and I read http://www.gammon.com.au/forum/?id=4960, but I'm having trouble getting it to save my state. Where do I put all that code? What is the name of the accelerator table? I will have to change all occurances of "what" with that name right? Thanks in advance.

Edit: I am getting the following error:

Compile error
Plugin: Accelerator (called from world: ansalon)
Immediate execution
[string "Plugin"]:181: ')' expected (to close '(' at line 180) near ','
Error context in script:
177 :
178 : lua_reserved_words = {}
179 :
180 : for _, v in (
181*: "and", "break", "do", "else", "elseif", "end", "false",
182 : "for", "function", "if", "in", "local", "nil", "not", "or",
183 : "repeat", "return", "then", "true", "until", "while"
184 : ) do lua_reserved_words [v] = true end
185 :
[WARNING] \\QUADS\worlds\plugins\Accelerator.xml
Line 54: Error parsing script (Cannot load)
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #1 on Mon 21 Feb 2011 07:18 PM (UTC)
Message
In lines 181 and 184 the ( and ) should be { and } (curly brackets).

However you absolutely should not even be using that code directly.

You just use the existing module. You just add a couple of lines to your plugin, as described near the bottom of that thread, eg.



require "serialize"  -- needed to serialize table to string

function OnPluginSaveState ()
  -- get accelerator table
  local t = AcceleratorList () or {}  -- use empty table if no list

  SetVariable ("accelerators", "accelerators = " .. serialize.save_simple ( t ))
end -- function OnPluginSaveState


Then in OnPluginInstall you are going to have to put the accelerators back.

- Nick Gammon

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

Posted by Ruckinfok   (7 posts)  [Biography] bio
Date Reply #2 on Mon 21 Feb 2011 07:57 PM (UTC)

Amended on Mon 21 Feb 2011 08:09 PM (UTC) by Ruckinfok

Message
Nick Gammon said:

Then in OnPluginInstall you are going to have to put the accelerators back.


What do you mean by this?

This is what I have now:


<script>
function OnPluginInstall()
Accelerator ("alt+numpad4", "flee west")
Accelerator ("alt+numpad6", "flee east")
Accelerator ("alt+numpad8", "flee north")
Accelerator ("alt+numpad2", "flee south")
SetVariable ("accelerator", serialize ("accelerator"))  --> serialize accelerator table
end


require "serialize"  -- needed to serialize table to string

function OnPluginSaveState ()
  -- get accelerator table
  local t = AcceleratorList () or {}  -- use empty table if no list

  SetVariable ("accelerators", "accelerators = " .. serialize.save_simple ( t ))
end -- function OnPluginSaveState


</script>


I took out the SetVariable ("accelerator", serialize ("accelerator")) part, and I stopped getting errors, but it still isn't saving. I imagine you're telling me I need to "load" my set of accelerators during my onplugininstall function, but how do I do this? Also, will it save it individually for each world file, like usual for variables (I imagine yes)?

Sorry, I'm so new to this!
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #3 on Tue 22 Feb 2011 12:28 AM (UTC)
Message
OK, let's go back a step. Why do you want to save them? You just have the four as far as I can see, and they will automatically be installed. How do others get created?

- Nick Gammon

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

Posted by Ruckinfok   (7 posts)  [Biography] bio
Date Reply #4 on Tue 22 Feb 2011 12:40 AM (UTC)

Amended on Tue 22 Feb 2011 12:41 AM (UTC) by Ruckinfok

Message
Nick Gammon said:

OK, let's go back a step. Why do you want to save them?


First of all, I use multiple world files for multiple characters, and would like to have separate accelerators set for different characters for the same keys (without having to manage multiple files).

Second of all, I intend to share this plugin with others. I like to try to make plugins I share with others manageable without having to edit the text in the xml file.

Nick Gammon said:

You just have the four as far as I can see, and they will automatically be installed. How do others get created?


I use the following aliases to manage them:


<alias
   match="bind '*' '*'"
   enabled="y"
   sequence="100"
   send_to="12"
  ><send>
  Accelerator("%1", "%2")
  Note("%2 bound to keystroke %1")
  SaveState ()
</send></alias>
 
<alias
   match="bindlist"
   enabled="y"
   sequence="100"
   send_to="12"
  ><send>
for _, v in pairs (AcceleratorList ()) do 
  Note (v) 
end
SaveState ()
</send></alias>
I put those 4 in because I was certain I would use them across all characters, and because I was getting error messages about not having any accelerators, so I supplied some on install so squash the error.
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #5 on Tue 22 Feb 2011 05:47 AM (UTC)

Amended on Tue 22 Feb 2011 05:48 AM (UTC) by Nick Gammon

Message
OK, I see. Well the saving part is fine. If you check your variable "accelerators" in the GUI interface (after saving the world) you should see something like this:


accelerators = {
  [1] = "Alt+Numpad2 = flee south",
  [2] = "Alt+Numpad4 = flee west",
  [3] = "Alt+Numpad6 = flee east",
  [4] = "Alt+Numpad8 = flee north",
  }


Now, we need to process that in OnPluginInstall.

Add the code below in bold (and take out your line about setting the variable):



function OnPluginInstall()
  Accelerator ("alt+numpad4", "flee west")
  Accelerator ("alt+numpad6", "flee east")
  Accelerator ("alt+numpad8", "flee north")
  Accelerator ("alt+numpad2", "flee south")


  -- convert string back into Lua table
  assert (loadstring (GetVariable ("accelerators") or "")) ()

  -- if found, process it
  if accelerators then

    for k, v in ipairs (accelerators) do
       local key, action = string.match (v, "^(%S+) = (.*)$")
       if key then
         Accelerator (key, action)
       end -- if matched
    end -- for

  end -- if

end -- OnPluginInstall



That converts the string variable back into a table, and then goes through the table, converting each entry into keyname/action. Then it does an "Accelerator" on each of those.

- Nick Gammon

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

Posted by Ruckinfok   (7 posts)  [Biography] bio
Date Reply #6 on Tue 22 Feb 2011 12:36 PM (UTC)
Message
Thanks, this works flawlessly!
[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.


16,746 views.

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

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

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

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

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

[Best viewed with any browser - 2K]    [Hosted at HostDash]