Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lua function "dfhack.gui.makeAnnouncement" does not work. #999

Closed
milochristiansen opened this issue Sep 30, 2016 · 15 comments
Closed

Lua function "dfhack.gui.makeAnnouncement" does not work. #999

milochristiansen opened this issue Sep 30, 2016 · 15 comments

Comments

@milochristiansen
Copy link
Contributor

Due to the way the position argument is handled this function always fails with an error. This issue also affects the other announcement functions that take a position.

@lethosor
Copy link
Member

lethosor commented Sep 30, 2016

dfhack.gui.makeAnnouncement(df.announcement_type.CITIZEN_DEATH, {D_DISPLAY=true}, xyz2pos(1,2,3), 'test', COLOR_GREEN)

xyz2pos(x,y,z) is equivalent to {x=x,y=y,z=z}, so this should also work:

dfhack.gui.makeAnnouncement(df.announcement_type.CITIZEN_DEATH, {D_DISPLAY=true},{x=1,y=2,z=3}, 'test', COLOR_GREEN)

There's also a more complicated example in plugins/lua/siege-engine.lua or hack/lua/plugins/siege-engine.lua.

@lethosor lethosor added the lua label Sep 30, 2016
@milochristiansen
Copy link
Contributor Author

I tried {x=x,y=y,z=z}, and it didn't work.

@lethosor
Copy link
Member

What other arguments did you pass?

@milochristiansen
Copy link
Contributor Author

dfhack.gui.makeAnnouncement(df.announcement_type.CANCEL_JOB, df.announcement_flags.D_DISPLAY, unit.pos, msg, COLOR_LIGHTRED)

When that didn't work:

dfhack.gui.makeAnnouncement(df.announcement_type.CANCEL_JOB, df.announcement_flags.D_DISPLAY, {x = unit.pos.x, y = unit.pos.y, z = unit.pos.z}, msg, COLOR_LIGHTRED)

The error message indicated the position was the problem.

@milochristiansen
Copy link
Contributor Author

I also added a new = true in there after the first try failed (just in case), but no luck.

@lethosor
Copy link
Member

Your flags argument is incorrect. df.announcement_flags is a bitfield, so you can't pass an integer. What exactly is the error you're getting, anyway? I just get

(interactive):1: Cannot write field (global).makeAnnouncement(): complex object.

I did get this, but only when I mixed up pos2xyz and xyz2pos:

./hack/lua/dfhack.lua:187: attempt to index a number value (local 'pos')

That's just a result of passing something to pos2xyz() incorrectly.

@milochristiansen
Copy link
Contributor Author

Something like your first error, but following the stack trace took me to the line where the position is assigned. Maybe the line number was off?

@lethosor
Copy link
Member

I'm not quite sure what you mean. I thought you were passing unit.pos directly into makeAnnouncement().
Anyway, does fixing the flags argument work?

@milochristiansen
Copy link
Contributor Author

I'm not quite sure what you mean. I thought you were passing unit.pos directly into makeAnnouncement().

I tried that first, then I tried creating my own position table, neither one worked. The first time I got the error I followed the stack trace into native code and it took me to the line where the position was assigned, so I assumed that was what was causing the problem.

I can't check to see if fixing the flags works, because I don't have DF on this computer... I can test it and get back to you on tuesday.

@lethosor
Copy link
Member

I can't see any information in the Lua stack trace that would allow you to track down the exact line in question in the C++ code. And even if you could, the C++ function would never actually be called, because the Lua API catches invalid arguments before trying to call native code.

@milochristiansen
Copy link
Contributor Author

Umm... What? The stack trace I got had line numbers and filenames for the Lua code, then more for native code (DFHack, not DF) that Lua had called into. A bit of looking found these files in the DFHack source distribution. I have no idea what stack traces you get, but the ones I get have all kinds of useful information. I ended up coding my own version of this function in pure Lua using the C++ version as a guide.

@lethosor
Copy link
Member

This is what I got with your attempt (in the interactive interpreter):

(interactive):1: Cannot write field (global).makeAnnouncement(): complex object.
stack traceback:
    [C]: in field 'makeAnnouncement'
    (interactive):1: in main chunk
    [C]: in function 'safecall'
    ./hack/lua/dfhack.lua:401: in function 'dfhack.interpreter'
    ../dfhack/scripts/lua.lua:74: in local 'f'
    ./hack/lua/dfhack.lua:562: in function 'dfhack.run_script_with_env'
    (...tail calls...)

Even if you did compile DFHack with debugging information and got Lua stack traces to show line numbers of native code, you wouldn't see anything in the makeAnnouncement() function in library/modules/Gui.cpp because it was never called.

@milochristiansen
Copy link
Contributor Author

Well somehow I did, I have no idea how, but I did.

... Weird, but that's what happened.

Anyway, I'll have more information next tuesday.

@milochristiansen
Copy link
Contributor Author

I was wr... I was wro... ahem You were right.

@lethosor
Copy link
Member

lethosor commented Oct 4, 2016

To be fair, I had to look up that example, and I didn't even expect {D_DISPLAY=true} to work at first. Bitfields in Lua are friendlier than in C++, apparently.

I might see if it's possible to give better error messages for stuff like this when I get a chance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants