Hard Light Productions Forums

Modding, Mission Design, and Coding => The Modding Workshop => Topic started by: Shivan Hunter on November 20, 2015, 06:09:52 am

Title: wtf is wrong with this lua script
Post by: Shivan Hunter on November 20, 2015, 06:09:52 am
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?
(http://i.imgur.com/4XmTslG.png)
Title: Re: wtf is wrong with this lua script
Post by: AdmiralRalwood on November 20, 2015, 06:32:20 am
Your "On Frame" hook will run even during menus/the mainhall, while the data it relies on isn't set until "On Gameplay Start".
Title: Re: wtf is wrong with this lua script
Post by: Shivan Hunter on November 20, 2015, 06:38:32 am
this script (http://i.imgur.com/8ZNpBOI.png) 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
Title: Re: wtf is wrong with this lua script
Post by: zookeeper on November 20, 2015, 06:41:13 am
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).
Title: Re: wtf is wrong with this lua script
Post by: Shivan Hunter on November 20, 2015, 06:45:36 am
The return syntax of Map:GridToScreenPosition is based on this page (http://www.lua.org/pil/5.1.html). 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 (http://i.imgur.com/gb6x3k5.png). 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?
Title: Re: wtf is wrong with this lua script
Post by: AdmiralRalwood on November 20, 2015, 06:49:26 am
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.
Title: Re: wtf is wrong with this lua script
Post by: zookeeper on November 20, 2015, 07:15:02 am
Whoops. Lua arrays are 1-based, not 0-based.
Title: Re: wtf is wrong with this lua script
Post by: Shivan Hunter on November 20, 2015, 09:52:11 pm
wat

whyyy