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 ➜ code never enters first condition in plugin

code never enters first condition in plugin

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


Posted by Kevnuke   USA  (145 posts)  Bio
Date Wed 31 Jul 2019 11:51 AM (UTC)

Amended on Wed 31 Jul 2019 11:52 AM (UTC) by Kevnuke

Message
I've been trying to figure out the flaw in this for days and I'm not seeing the problem. Put plainly, the code I pasted is supposed to grab all the "items" in the room I've entered (other functions add/remove it as well, as needed), which includes inanimate objects, mobs loyal to other players, mobs I want to hunt, etc, and sorts them into tables using the values of the fields. The table "prey" is the one I'm trying to show in the miniwindow, which works, perfectly. What -isn't- working is trying to get my current target, which I match using its id, to use the font that makes it red and italic on that list.

https://paste.ofcode.org/zxkrm75kAA9RQAbSbXdnCJ

I had considered that the id is becoming a number in json.decode but I ruled that out by printing the type. It's always a string type.

Eventually I plan to have a table whose keys are the short_desc of each mob in an area, which are themselves tables with things like their priority in relation to other mobs in the room so I could, for example, make sure the boss is at the top of the list, strength rating, their targeting name to generically target similar enemies as you see them, ex.
tname = {"a young rat" = "rat", "a goblin miner" = "goblin"}


This is all assuming I can get my current problem out of the way.

Any insight would be appreciated.
Top

Posted by Nick Gammon   Australia  (23,120 posts)  Bio   Forum Administrator
Date Reply #1 on Wed 31 Jul 2019 09:58 PM (UTC)
Message
Specifically which of those tests if failing? What are your debugging prints telling you?

You know, to find if a non-numeric table is empty or not you can use "next", for example:


if next (t) == nil then
  -- table t is empty
end -- if empty


Are the things you are comparing numbers or strings? Numbers do not compare equal to strings in Lua, eg.


if 5 == "5" then ...


That will compare false.

If you want more help I suggest you "tprint" the table in question, and then your debugging messages.

- Nick Gammon

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

Posted by Kevnuke   USA  (145 posts)  Bio
Date Reply #2 on Thu 01 Aug 2019 06:52 AM (UTC)

Amended on Fri 02 Aug 2019 12:45 AM (UTC) by Kevnuke

Message
After looking at my plugin again, I had a few places where variables were not declared before the functions that used them, I didn't think Lua cared about that but the only reason I even moved them was because I was getting errors saying that a global variable was nil. After fixing that, I got another one complaining about the argument to WindowTextWidth being a table instead of a string which I wasn't getting before because it was behind the logic gate in the if statement in hunting_window(). I put tar_win instead of tar_win.name by accident. It's just a little strange that I wasn't getting that first error last time was using it. room.players was declared before room = {} in the file and target was declared after hunting_window, where it's used in that comparison.

It's working as intended now. I double-checked to make sure it wasn't trying to compare a number to a string and it wasn't. Now that it's working I tested it both ways and these two statements appear to be interchangeable:

next(target) ~= nil
tlen(target) ~= 0


I guess it was target not being declared before the function that screwed it up to begin with.

Crap and I must have typed something I meant for the MUD into my plugin. Why are the line numbers for the errors in plugins always* wrong?

[EDIT] typo
Top

Posted by Nick Gammon   Australia  (23,120 posts)  Bio   Forum Administrator
Date Reply #3 on Thu 01 Aug 2019 08:47 PM (UTC)
Message
Quote:

Why are the line numbers for the errors in plugins already wrong?


The script as seen by Lua starts after the tag <script> in the plugin. The plugin is not the script. The script is a sub-section of the plugin. So for example:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE muclient [
  <!ENTITY horizontal "y" > 
  <!ENTITY show_time "y" >
]>

<muclient>
<plugin
   name="Icon_Bar"
   author="Nick Gammon"
   id="ede0920fc1173d5a03140f0e"
   language="Lua"
   purpose="Shows a bar of buttons you can click on to do things"
   date_written="2009-02-26 09:00"
   date_modified="2010-05-31 17:22"
   requires="4.40"
   save_state="y"
   version="5.0"
   >
</plugin>

<!--  Script  -->

<script>
<![CDATA]
foo = bar     --> This is line 1 in the plugin


Here you can find the line number of the start of the script (the foo = bar line) and substract that from the error message line to get the "real" line number in the plugin.




Alternatively, if you follow my suggestion here:

http://www.gammon.com.au/forum/?id=14502

Like this:


<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>

<muclient>
<plugin
   name="Some_Plugin"
   author="Nick Gammon"
   id="598bd27e54abc0d4c6ade961"
   language="Lua"
   purpose="Example plugin"
   date_written="2019-07-21"
   requires="5.00"
   version="1.0"
   >
</plugin>

<script>
  dofile (GetPluginInfo (GetPluginID (), 20) .. "Some_Plugin.lua")
</script>
</muclient>


Then all the script is in the "Some_Plugin.lua" file, and the line numbers in errors will then be correct, as they will be from the start of that file.

- Nick Gammon

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

Posted by Nick Gammon   Australia  (23,120 posts)  Bio   Forum Administrator
Date Reply #4 on Thu 01 Aug 2019 08:49 PM (UTC)
Message
Quote:

I had a few places where variables were not declared before the functions that used them, I didn't think Lua cared about that but the only reason I even moved them was because I was getting errors saying that a global variable was nil.


You can assign to global variables anywhere but naturally they won't have a value before that assignment is executed. I mean you wouldn't expect this to work would you?


print (foo)


foo = 3

- Nick Gammon

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

Posted by Kevnuke   USA  (145 posts)  Bio
Date Reply #5 on Fri 02 Aug 2019 10:07 AM (UTC)

Amended on Fri 02 Aug 2019 02:29 PM (UTC) by Kevnuke

Message
Nick Gammon said:

Quote:

Why are the line numbers for the errors in plugins already wrong?


The script as seen by Lua starts after the tag <script> in the plugin. The plugin is not the script. The script is a sub-section of the plugin. So for example:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE muclient [
  <!ENTITY horizontal "y" > 
  <!ENTITY show_time "y" >
]>

<muclient>
<plugin
   name="Icon_Bar"
   author="Nick Gammon"
   id="ede0920fc1173d5a03140f0e"
   language="Lua"
   purpose="Shows a bar of buttons you can click on to do things"
   date_written="2009-02-26 09:00"
   date_modified="2010-05-31 17:22"
   requires="4.40"
   save_state="y"
   version="5.0"
   >
</plugin>

<!--  Script  -->

<script>
<![CDATA]
foo = bar     --> This is line 1 in the plugin


Here you can find the line number of the start of the script (the foo = bar line) and substract that from the error message line to get the "real" line number in the plugin.


I took that into account because I suspected that was the case and I still got a line that made no sense. The only thing on this line is an 'end'.
[string "Plugin: The_System"]:227: unexpected symbol near '.'

Nick Gammon said:

Alternatively, if you follow my suggestion here:

http://www.gammon.com.au/forum/?id=14502

Like this:


<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>

<muclient>
<plugin
   name="Some_Plugin"
   author="Nick Gammon"
   id="598bd27e54abc0d4c6ade961"
   language="Lua"
   purpose="Example plugin"
   date_written="2019-07-21"
   requires="5.00"
   version="1.0"
   >
</plugin>

<script>
  dofile (GetPluginInfo (GetPluginID (), 20) .. "Some_Plugin.lua")
</script>
</muclient>



Do I need to encapsulate my code in Some_Plugin.lua in

<![CDATA[
my random code
]]>

as if it were in the xml file? I'm going to put all of my code into a Lua file, but do I need to change aliases or triggers that use variables in the script code?

[EDIT] Welll that was a dumb mistake. After I finished troubleshooting the problem with target being nil, I made all my global plugin variables local again, except I did it to too many variables, such as..

local system = {}
local system.map = {}

which Lua really doesn't like.
Top

Posted by Nick Gammon   Australia  (23,120 posts)  Bio   Forum Administrator
Date Reply #6 on Sat 03 Aug 2019 03:42 AM (UTC)
Message
Quote:

I took that into account because I suspected that was the case and I still got a line that made no sense. The only thing on this line is an 'end'.


The error can often be on the previous line. For example:


function foo ()
  a = b /
end


Gives this error:


Compile error
World: smaug2
Immediate execution
[string "Immediate"]:3: unexpected symbol near 'end'


That gives a similar error message. The word "end" is OK, but the previous line is not correct.




Quote:

Do I need to encapsulate my code in Some_Plugin.lua in


<![CDATA[
my random code
]]>




No. In the XML file that is needed so that you can use symbols like "<" and ">" without having to convert them into &lt; and &gt; as you normally would in an XML / HTML file.

However once you are executing a Lua file this is no longer being processed as XML. The Lua file can just be straight Lua.

- Nick Gammon

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

Posted by Nick Gammon   Australia  (23,120 posts)  Bio   Forum Administrator
Date Reply #7 on Sat 03 Aug 2019 03:48 AM (UTC)
Message
Quote:

I did it to too many variables, such as..



local system = {}
local system.map = {}


which Lua really doesn't like.


It doesn't make sense to put "local" on the second line. The first line declares a table called "system". The second line creates an item called "map" inside "system" and assigns an empty table to it.

The variable "system" is already local, you don't need to put local in front of the contents of the table.

This works:


local system = {}
system.map = {}


That makes a local table called "system" and inside it is an item called "map" which is an empty table. Since "map" is inside "system" they are both local variables (as indeed anything inside "system" would be). If the parent is local then its children will be local.

The second line is really syntactic sugar for:


system ['map'] = { }


This is an assignment, not a declaration.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
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.


21,212 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.