Author Topic: wtf is wrong with this lua script  (Read 2199 times)

0 Members and 1 Guest are viewing this topic.

Offline Shivan Hunter

  • 210
  • FRED needs lambdas!
wtf is wrong with this lua script
Completely new to lua here, my hate for weakly-typed languages has been reaffirmed

Anyway, what exactly is going wrong with this script? I get that something is nil when it shouldn't be, but what is it?

 

Offline AdmiralRalwood

  • 211
  • The Cthulhu programmer himself!
    • Skype
    • Steam
    • Twitter
Re: wtf is wrong with this lua script
Your "On Frame" hook will run even during menus/the mainhall, while the data it relies on isn't set until "On Gameplay Start".
Ph'nglui mglw'nafh Codethulhu GitHub wgah'nagl fhtagn.

schrödinbug (noun) - a bug that manifests itself in running software after a programmer notices that the code should never have worked in the first place.

When you gaze long into BMPMAN, BMPMAN also gazes into you.

"I am one of the best FREDders on Earth" -General Battuta

<Aesaar> literary criticism is vladimir putin

<MageKing17> "There's probably a reason the code is the way it is" is a very dangerous line of thought. :P
<MageKing17> Because the "reason" often turns out to be "nobody noticed it was wrong".
(the very next day)
<MageKing17> this ****ing code did it to me again
<MageKing17> "That doesn't really make sense to me, but I'll assume it was being done for a reason."
<MageKing17> **** ME
<MageKing17> THE REASON IS PEOPLE ARE STUPID
<MageKing17> ESPECIALLY ME

<MageKing17> God damn, I do not understand how this is breaking.
<MageKing17> Everything points to "this should work fine", and yet it's clearly not working.
<MjnMixael> 2 hours later... "God damn, how did this ever work at all?!"
(...)
<MageKing17> so
<MageKing17> more than two hours
<MageKing17> but once again we have reached the inevitable conclusion
<MageKing17> How did this code ever work in the first place!?

<@The_E> Welcome to OpenGL, where standards compliance is optional, and error reporting inconsistent

<MageKing17> It was all working perfectly until I actually tried it on an actual mission.

<IronWorks> I am useful for FSO stuff again. This is a red-letter day!
* z64555 erases "Thursday" and rewrites it in red ink

<MageKing17> TIL the entire homing code is held up by shoestrings and duct tape, basically.

 

Offline Shivan Hunter

  • 210
  • FRED needs lambdas!
Re: wtf is wrong with this lua script
this script has the same issue, and I should have mentioned the crash occurs when entering a mission.

edit: the error makes it sound like the nil is "position" but I don't see how that's nil. Also I can draw "Map.gridcell" outside of that loop, it's only when I start trying to get those coordinates that it crashes
« Last Edit: November 20, 2015, 06:42:34 am by Shivan Hunter »

 

Offline zookeeper

  • *knock knock* Who's there? Poe. Poe who?
  • 210
Re: wtf is wrong with this lua script
Your "On Frame" hook will run even during menus/the mainhall, while the data it relies on isn't set until "On Gameplay Start".

That's not it; the $State condition should take care of that. My guesses would be that the loadTexture call doesn't return a valid handle, or that GridToScreenPosition doesn't return the kind of array assumed by Draw (I wouldn't know, since that return syntax is alien to me).

A great way to check when hooks are run and to make sure things are done in the right order is to use ba.warning() calls here and there with a debug build. Or just begin by checking which line is to blame (I don't trust the reported line numbers to be exact).

 

Offline Shivan Hunter

  • 210
  • FRED needs lambdas!
Re: wtf is wrong with this lua script
The return syntax of Map:GridToScreenPosition is based on this page. If a function has two return values, you can use an array constructor to put both of those values in the array (which is what I do for the position var).

The texture is definitely fine, since I was drawing it before.

[EDIT] So I changed some arrays into separate vars for clarity and got a working result. Now my question is why this works and the other one didn't? Is the function in the array constructor a new feature of lua that FS doesn't implement?
« Last Edit: November 20, 2015, 06:49:46 am by Shivan Hunter »

 

Offline AdmiralRalwood

  • 211
  • The Cthulhu programmer himself!
    • Skype
    • Steam
    • Twitter
Re: wtf is wrong with this lua script
Your "On Frame" hook will run even during menus/the mainhall, while the data it relies on isn't set until "On Gameplay Start".

That's not it; the $State condition should take care of that.
Oh, well, now I feel silly.

The line it's pointing to definitely suggests that position[0] is the nil value it has a problem with... I could guess what to try here, but I think I'll shut up and let somebody who actually knows a thing or two about Lua answer.
Ph'nglui mglw'nafh Codethulhu GitHub wgah'nagl fhtagn.

schrödinbug (noun) - a bug that manifests itself in running software after a programmer notices that the code should never have worked in the first place.

When you gaze long into BMPMAN, BMPMAN also gazes into you.

"I am one of the best FREDders on Earth" -General Battuta

<Aesaar> literary criticism is vladimir putin

<MageKing17> "There's probably a reason the code is the way it is" is a very dangerous line of thought. :P
<MageKing17> Because the "reason" often turns out to be "nobody noticed it was wrong".
(the very next day)
<MageKing17> this ****ing code did it to me again
<MageKing17> "That doesn't really make sense to me, but I'll assume it was being done for a reason."
<MageKing17> **** ME
<MageKing17> THE REASON IS PEOPLE ARE STUPID
<MageKing17> ESPECIALLY ME

<MageKing17> God damn, I do not understand how this is breaking.
<MageKing17> Everything points to "this should work fine", and yet it's clearly not working.
<MjnMixael> 2 hours later... "God damn, how did this ever work at all?!"
(...)
<MageKing17> so
<MageKing17> more than two hours
<MageKing17> but once again we have reached the inevitable conclusion
<MageKing17> How did this code ever work in the first place!?

<@The_E> Welcome to OpenGL, where standards compliance is optional, and error reporting inconsistent

<MageKing17> It was all working perfectly until I actually tried it on an actual mission.

<IronWorks> I am useful for FSO stuff again. This is a red-letter day!
* z64555 erases "Thursday" and rewrites it in red ink

<MageKing17> TIL the entire homing code is held up by shoestrings and duct tape, basically.

 

Offline zookeeper

  • *knock knock* Who's there? Poe. Poe who?
  • 210
Re: wtf is wrong with this lua script
Whoops. Lua arrays are 1-based, not 0-based.

 

Offline Shivan Hunter

  • 210
  • FRED needs lambdas!
Re: wtf is wrong with this lua script
wat

whyyy