Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: lua newbie question  (Read 1451 times)

Bo-Rufus CMVII

  • Bay Watcher
    • View Profile
lua newbie question
« on: March 12, 2014, 04:14:23 am »

In the following expression, v is a dorf:

Code: [Select]
v.inventory[i].item.isArmor(v.inventory[i].item)
For OO programming I would expect to leave the parentheses empty, but if I do that for this and a lot of othe functions I've played with, the script bombs off complaining about too few arguments.  The above works, but it's very redundant, so I suspect there is a more natural way to invoke the function in lua.  Any suggestions?
« Last Edit: March 12, 2014, 04:16:04 am by Bo-Rufus CMVII »
Logged

Warmist

  • Bay Watcher
  • Master of unfinished jobs
    • View Profile
Re: lua newbie question
« Reply #1 on: March 12, 2014, 05:43:11 am »

In the following expression, v is a dorf:

Code: [Select]
v.inventory[i].item.isArmor(v.inventory[i].item)
For OO programming I would expect to leave the parentheses empty, but if I do that for this and a lot of othe functions I've played with, the script bombs off complaining about too few arguments.  The above works, but it's very redundant, so I suspect there is a more natural way to invoke the function in lua.  Any suggestions?
In lua the shorthand (they call it syntactic sugar) for X.func(X) is X:func(). Also function definitions work too:
Code: [Select]
function x.func(self)

end
--OR
function x:func()
--can use self here as "this" in C++
end

Bo-Rufus CMVII

  • Bay Watcher
    • View Profile
Re: lua newbie question
« Reply #2 on: March 13, 2014, 12:07:11 am »

Thanks.  Makes the code look cleaner.

Another one, if you or anyone else has the time:

v is again a dwarf.  After ensuring that v.job.current_job isn't nil, I do
Code: [Select]
jobtype = v.job.current_job['job_type']
    if jobtype == df.job_type.Sleep then
        print('sleep')
    elseif ... more of the same
But I really don't want to do it for 230 job types.  However, it looks like there should be a simple way to do this for any job type in a single statement, because df.job-types.xml has stuff like this:
Code: [Select]
        <enum-item name='Sleep'>
            <item-attr name='caption' value='Sleep'/>
            <item-attr name='type' value='LifeSupport'/>
        </enum-item>
I'm guessing that there's some way I can access the 'caption' attribute for jobtype, regardless of what jobtype is, but I haven't been able to guess the syntax for fetching an item-attr.

Any hints?

Logged

Warmist

  • Bay Watcher
  • Master of unfinished jobs
    • View Profile
Re: lua newbie question
« Reply #3 on: March 13, 2014, 06:32:41 am »

Yes there are enum attributes in dfhack. They are accessible with 'attrs' value. I think it was:
Code: [Select]
df.job_type.attrs[jobtype].caption
--and
df.job_type.attrs[jobtype].type

One minor nitpick:
Code: [Select]
jobtype = v.job.current_job['job_type'] -- no reason to do this, when you can:
jobtype = v.job.current_job.job_type

Bo-Rufus CMVII

  • Bay Watcher
    • View Profile
Re: lua newbie question
« Reply #4 on: March 13, 2014, 03:30:06 pm »

Nitpicks are good.  I'm still guessing at the syntax half the time, trying to translate what I see in the dfhack .cpp and .xml files into lua (which I don't know to begin with).
Logged