Tutorial; How to Retrieve Values from Within Different Files - HWRM/KarosGraveyard GitHub Wiki

How to Retrieve Values from Within Different Files by Mikali

Retrieving Values

Let's say, for example, you have a file called "MyCustomVariables.lua", and in it you've placed a variable called MySpecialVariable. Let's also say that you have another file called "MyGameRule.lua", and in that file you need to know the value of the variable, MySpecialVariable. Well, you could retrieve the value of that variable by using the dofilepath() function. E.g., if the code below were inserted into "MyGameRule.lua", it would read the contents of "MyCustomVariables.lua" and print the value of MySpecialVariable.

dofilepath("data:MyCustomVariables.lua")
print(MySpecialVariable)

Retrieving Values from Within Tables

In this example we're going to try and retrieve and print the value of the variable, ThingToBuild, for every ship of every race in the build table in "build.lua". We do this by first retrieving the list of races from the races table within "race.lua". Then we traverse the build table in each race's "build.lua" in order to retrieve the value for ThingToBuild.
In the example below, the contents of each race's build table will not interfere with each other from one iteration of the loop to the next, since all processing of the tables is done within the scope of one iteration.
You can insert the following code into the OnInit() function of "deathmatch.lua", and then check the text output in "HW2.log" to see if it works.

-- get the list of races from "race.lua".
dofilepath([data:scripts/race.lua](/HWRM/KarosGraveyard/wiki/data:scripts/race.lua))

-- for every race (for every entry in the "races" table)...
for k, iCount1 in races do

    -- get the name of the race (the very first entry in the table "k") and append it to the file path
    -- in order to get that race's "build.lua".
    dofilepath([data:scripts/building and research/](/HWRM/KarosGraveyard/wiki/data:scripts/building-and-research/) .. races[k][1] .. [/build.lua](/HWRM/KarosGraveyard/wiki//build.lua))

    -- if a "build.lua" for this race exists (if "build.lua" has a table named "build" in it)...
    if build then

        -- for every entry within the "build" table...
        for j, iCount2 in build do

            -- if the entry "j" exists...
            if build[j] then
                -- print the value for "ThingToBuild".
                print(build[j].ThingToBuild)
            end
        end
    end
end

You could then compare the returned values against, for instance, the makeup of a player's fleet.

Discussion:

Related Pages

Tutorials

Comments

Page Status

Updated Formatting? Initial
Updated for HWRM? Initial