Debug... How

This forum is for the Lua scriptable clone of DM/CSB called Dungeon Strikes Back by Sophia. Use DSB to build your own highly customised games.

Moderator: Sophia

Forum rules
Please read the Forum rules and policies before posting. You may Image to help finance the hosting costs of this forum.
Post Reply
User avatar
ian_scho
High Lord
Posts: 2807
Joined: Fri Apr 07, 2006 8:30 am
Location: Zaragoza, Spain

Debug... How

Post by ian_scho »

Greetings

How do you debug in DSB? I'd like a way to spit out a variable or string to 'log.txt', for example, rather than using the console - dsb_write(RGB, string).

Also I don't seem to be able to access the local obj (user data) with the below function. I'm investigating to see how I can override a trolin's AI, for example, outputting the wee beastie to file.


startup.lua

Code: Select all

dbg = {}
function dbg.echo(s)
        dbg.echon(s)
        dbg.echon('\n')
end

function dbg.echon(s)
        -- io.stderr:write(dbg.tostring(s))
        -- io.write(dbg.tostring(s))
        --print(dbg.tostring(s))
        dsb_write({100, 100, 100}, dbg.tostring(s))
end

function dbg.tostring(obj)
        local mt
        local tname
        local o
        if type(obj) == 'userdata' then
                mt = getmetatable(obj)
                o = '['
                if mt and mt.__index then
                        tname = mt.__index.__typename
                        if tname then
                                o = o .. tname
                        end
                end
                if obj.name then
                        if tname and tname == 'WClientWin' then
                                o = o .. ': ' .. dbg.tostring(obj:get_ident())
                        else
                                o = o .. ': ' .. obj:name()
                        end
                end
                return o .. ']'
        elseif type(obj) == 'table' then
                o = '{'
                sep = ''
                for k, v in pairs(obj)
                do
                        o = o .. sep .. dbg.tostring(k) .. '='
                        o = o .. dbg.tostring(v)
                        sep = ', '
                end
                return o .. '}'
        end
        return tostring(obj)
end

-- Print an array
dbg.echo(xp_levelnames)
-- Print an array of arrays (not a table)
dbg.echo(player_colors)
-- Print a const
dbg.echo(NORTH)
-- Print text, capitals only.
dbg.echo('THIS IS A TEST')
-- My Favourite Trolin, serialised
--dbg.echo(tmp) --Doesn't really work, he defeats me (it's called at the end of dungeon.lua, not here, tmp=dsb_spawn("trolin", 1, 2, 8, CENTER)).
--test = dsb_find_arch(tmp)
--dbg.echo(test)
--dbg.echo(test["name"])
User avatar
Sophia
Concise and Honest
Posts: 4307
Joined: Thu Sep 12, 2002 9:50 pm
Location: Nowhere in particular
Contact:

Post by Sophia »

Right now, you can use __log(string). I always meant it more for internal use so it's not exactly the most useful function around but if you can get use out of it, go for it.

The io object doesn't point anywhere useful. Maybe I should look into making its stdout and stderr point to log.txt too, if that's even possible... :)

I'm not sure what you mean by "outputting to a file," because, of course, the AI code is already in monster_ai.lua and the definition of the monster itself is in objects.lua... :)
User avatar
ian_scho
High Lord
Posts: 2807
Joined: Fri Apr 07, 2006 8:30 am
Location: Zaragoza, Spain

Post by ian_scho »

Thanks Sophia, will give it a try..... now. And it works! Sweet.

The sort of things Im looking for are interrogating the variables such as sight, as passed in as a parameter to ai_monster.
User avatar
Sophia
Concise and Honest
Posts: 4307
Joined: Thu Sep 12, 2002 9:50 pm
Location: Nowhere in particular
Contact:

Post by Sophia »

Oh, so you're trying to get around my lack of documentation, in other words. ;)

ai_monster is called by sys_ai_near in base/system.lua, which is invoked by the engine any time there is a monster nearby that needs to make a move. The function is passed the monster's id and sight, which tells whether the monster can actually see the party.
Post Reply