[solved] including define file in a script

Started by js, Sat 03/09/2022 16:21:27

Previous topic - Next topic

js

Hello,

I was annoyed to put ID of item for doing comparisons in script.
The ID is error prone, it could change, etc.

I could compare the item name, but that's even more error prone and i have to get its translation, and it must be awfully slow.

So my idea was to define macro, with item name and ID.

My script parse the xml of Game.agf and input what it has found.
For example:

Code: ags

#define iCup 1
#define iKey 2


Now, i need to include this in each character scripts. Is there an include_file() function or something like that ?

Yes, i could do copy/paste, or modifying the scripts by appending generate define at the beginning (with another script), but that doesn't sound like a good idea.

Thank you.


For those interested, the extraction script (it's lua and you need to install lua-expat module)

Code: ags

#!/usr/bin/env lua

-- Archlinux users: community/lua-expat
-- doc: https://lunarmodules.github.io/luaexpat/manual.html#introduction
local lxp_totable = require("lxp.totable")

local function read_file(path)
    local hFile = assert(io.open(path, "rb"))
    local data = hFile:read "*a"
    hFile:close()
    return data
end

local function parse_table(t)
  for k,v in pairs(t) do
    if type(v) == "table" then
      parse_table(v)
    else
      if v == "InventoryItem" then
        local item = lxp_totable.torecord(t)
          print(string.format("#define %s %d",item.Name,item.ID))
      end
    end
  end
end

local xml_data = read_file("Game.agf")
local data = lxp_totable.parse(xml_data)
parse_table(data)

Crimson Wizard

#1
Quote from: js on Sat 03/09/2022 16:21:27
I was annoyed to put ID of item for doing comparisons in script.
The ID is error prone, it could change, etc.

Usually you don't have to use numeric IDs at all for comparison, you can use script names for that. These are names like iCup etc. For each item you create in game there's a automatically generated pointer variable of same name, that may be used for comparisons or for accessing that item.

For example, if you have inventory items with script names iCup and iKey, then you can do:
Code: ags

if (player.ActiveInventory == iCup) {
    // do something
} else if (player.ActiveInventory == iKey) {
    // do something else
}


Even if you must use a numeric ID somewhere, you can access the ID property from the same pointer variable instead of typing the number itself, for example:
Code: ags

int item_id = iCup.ID;



Quote from: js on Sat 03/09/2022 16:21:27
Is there an include_file() function or something like that ?

No, AGS script compiler does not support that at the moment, but if still necessary, you could create a dummy script at the top of the list, and then print your results right into the script's header file perhaps.

js

Great ! Another useless script written  :-D

Thank you for mentionning script names. As you have already guessed, i didn't knew them.

SMF spam blocked by CleanTalk