lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


2009/1/9 Roger Durañona Vargas <luo_hei@yahoo.es>:
> Well, this is an first draft of the script:
>
> -- @param q Npc line number
>
> local dialog = {}
>
> if (q=="root") then
>        --we insert all answers
>        table.insert(dialog,{"Yes sir, Im ready!","yes"})
>        table.insert(dialog,{"I really dont like you giving me
> orders","no"})
>        table.insert(dialog,{"I wouldnt call it a skirmish. Was a really
> bloodbath if you ask me.","middle"})
>        --this is the npc line
>        line="You are one of those rookies that survived yesterday's
> skirmish. I have a mission for you, are you ready for more action?"
>        count=3 -- how many choices we have
>        return line, count, ta
>
>    elseif (q=="no") then
>         line="I really wasn't asking you. Go see the smith and get a
> decent armor and sword. Then come back to me"
>
>         end
>
> (it is unfineshed of course).
> Basically, what I dont know now is how to get the table in the host app.
> I have checked get_table documentation but cant clearly underestand how
> to read all those values.

I'm assuming ta==dialog. Also you don't need to return count, since
you can simply measure the length of the dialog table.

So if you have on top of the stack the dialog table, here is how you
can access its content:

/* dialog is on top of the stack */
int index = lua_gettop(L);
/* compute length of dialog table */
int nchoices = lua_objlen(L, index);
for (int i=0; i<nchoices; ++i) {
  /* get dialog subtable (Lua is 1-based) */
  lua_rawgeti(L, index, i+1);
  /* first index in subtable is the answer text */
  lua_rawgeti(L, -1, 1);
  const char* text = lua_tostring(L, -1);
  /* second index is the answer id */
  lua_rawgeti(L, -2, 2);
  const char* id = lua_tostring(L, -1);
  /* pass the data to the engine */
  engine_set_dialog_choice(i, text, id);
  /* pop the temporary values from the stack, after they are no longer used */
  lua_pop(L, 3); /* id, text, subtable */
}