2

First of all: I'm an inexperienced coder and just started reading PiL. I only know a thing or two but I'm fast learning and understanding. This method is really unnecessary but I sort of want to give myself a hard time in order to learn more.

Okay so for testing and for getting to know the language more, I'm trying to grab two different values from two different files and storing them in tables

local gamemap = file.Read("addons/easymap/data/maplist.txt", "GAME")
local mapname = string.Explode( ",", gamemap )
local mapid = file.Read("addons/easymap/data/mapid.txt", "GAME")
local id = string.Explode( ",", mapid )

I'm grabbing two values which in the end are mapname and id

Once I have them, I know that using

for k, v in pairs(mapname)

It will give specific values to the data taken from the file, or at least assign them.

But what I need to do with the both tables is that if there is certain map in the server, check for the value in the table unless the map name is nil and then once having the name, grab the value of that map and match it with the id of the other file.

For example, I have in the maplist.txt file gm_construct and it is the first entry [1] and its corresponding id in mapid.txt lets say it is 54321 and it is also the first entry [1].

But now I must check the server's current map with game.GetMap function, I have that solved and all, I grab the current map, match it with the mapname table and then check for its corresponding value in the id table, which would be gm_construct = 1.

For example it would be something like

local mapdl = game.GetMap()
local match = mapname[mapdl]

if( match != nil )then --supposing the match isn't nil and it is in the table
    --grab its table value, lets say it is 1 and match it with the one in the id table

It is a more complex version of this http://pastebin.com/3652J8Pv

I know it is unnecessary but doing this script will give me more options to expand the script further.

TL;DR: I need to find a function that lets me match two values coming from different tables and files, but in the end they are in the same order ([1] = [1]) in both files. Or a way to fetch a full table from another file. I don't know if a table can be loaded globally and then grabbed by another file to use it in that file.

I'm sorry if I'm asking too much, but where I live, if you want to learn to program, you have to do it on your own, no schools have classes or anything similar, at least not until University, and I'm far away from even finishing High School.

Edit: this is intended to be used on Garry's mod. The string.Explode is explained here: http://wiki.garrysmod.com/page/string/Explode

It basically separates phrases by a designated character, in this case, a comma.

  • fyi, Lua uses `~=` instead of `!=` for inequality comparisons. – greatwolf Jan 26 '15 at 02:15
  • 1
    I don't understand what you are trying to do here that you are having trouble with. What does `string.Explode` return? A table? Comparing elements from a table to each other isn't any different than comparing things that aren't in a table from each other (or from a constant value/nil). – Etan Reisner Jan 26 '15 at 02:24
  • Sorry for not being clear. But the string.Explode is explained here: http://wiki.garrysmod.com/page/string/Explode I'm working mostly on garry's mod lua libraries – Holynanners Jan 26 '15 at 02:28

1 Answers1

1

Okay. If I understand correctly... You have 2 Files with data.

One with Map Names

gm_construct,
gm_flatgrass,
de_dust2,
ttt_waterworld

And One with IDs, Numbers, Whataver (related to the entries at the same position in the Map Names File

1258,
8592,
1354,
2589

And now you want to find the ID of the current Map, right?

Here is your Function

local function GetCurrentMapID()
  -- Get the current map
  local cur_map = game.GetMap()

  -- Read the Files and Split them
  local mapListRaw = file.Read("addons/easymap/data/maplist.txt", "GAME")
  local mapList= string.Explode(",", mapListRaw)
  local mapIDsRaw = file.Read("addons/easymap/data/mapid.txt", "GAME")
  local mapIDs = string.Explode(",", mapIDsRaw)

  -- Iterate over the whole map list
  for k, v in pairs(mapList) do
    -- Until you find the current map
    if (v == cur_map) then
      -- then return the value from mapIDs which is located at the same key (k)
      return mapIDs[k]
    end
  end
  -- Throw a non-breaking error if the current map is not in the Maplist
  ErrorNoHalt( "Current map is not registered in the Maplist!\n" )
end

Code could have errors 'cause I couldn't test it. Pls Comment with error if so.

Source: My Experience and the GMod Wiki

Mischa
  • 1,303
  • 12
  • 24