[0.5.0]comparison to values extracted from a file

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
rhezalouis
Party member
Posts: 100
Joined: Mon Dec 07, 2009 10:27 am
Location: Indonesia
Contact:

[0.5.0]comparison to values extracted from a file

Post by rhezalouis »

hi, nice to meet all of you. :ultrahappy: [has this been a proper introduction?]

i put flexible key assignment to a program by putting the key values on a text file and extract them into a table tKey using the code below:

Code: Select all

tKey = ()
function tKey.fLoad()
    counter = 0;
    for vLine in love.filesystem.lines(tMacro.keyfileName) do
        if string.find(vLine, "><", 1 , 1) == 1 then break; end -- end of file indicator, don't read
        if string.find(vLine, "--", 1 , 1) == nil then           -- not a comment
            tKey[counter] = vLine;
            counter = counter + 1;
        end
    end
end
the function tKey.fLoad() is called at callback load() and the table tKey is used in the callback keypressed(key):

Code: Select all

function keypressed(key)
    if (key == tKey[0]) or (key == tKey[1]) then
        fNextState(); --go to the next program state
    end
end
the expression (key == tKey[0]) is not working. could anyone provide me a way to put the extracted value on an expression?

notes: i have read a previous post about "User Define keys in a game" http://love2d.org/forum/viewtopic.php?f ... 02&start=0; please note that my question is not about the user-defined keys but on how to put the extracted values in an expression [is it possible without using complicated loadstring() or am i thinking in a too complicated way?].

thank you for your time. :ultrahappy:
Aargh, I am wasting my posts! My citizenshiiiip... :o
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: [0.5.0]comparison to values extracted from a file

Post by Robin »

I suppose the strings that are extracted are "left", "right", "a", "0", "space" and things like that.
If so, it's rather simple. Constants are directly in the love table, and key constants are prefixed by key_:

Code: Select all

love.key_left
Which means this should work:

Code: Select all

tKey[counter] = love["key_"..vLine] -- works since t.hi == t["hi"]
Help us help you: attach a .love.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: [0.5.0]comparison to values extracted from a file

Post by kikito »

Howdy!

What are the values of tKey[0] and tKey[1]? Can you print them somewhere? print('tKey[0]: [' .. tKey[0] .. ']') will show them on the log.

Also, have you thought about coding your settings directly in a lua file?. Your file could be called "tKey.lua", and look like this:

Code: Select all

  tKey = { 'a', 'b' }
This will make tKey[0] = 'a' and tKey[1] = 'b' after you invoke this:

Code: Select all

  doFile('tKey.lua')
Which in my opinion is as simple as it can get.

Or even better, give the keys a proper name instead of 0s and 1s:

Code: Select all

  tKey = { fire = 'a', jump = 'b' }
Now tKey.fire = 'a' and tKey.jump='b'. You can then do:

Code: Select all

function keypressed(key)
    if (key == tKey.jump) then
        fNextState(); --go to the next program state
    end
    if (key == tKey.fire) then
        fThrowAFireball(); -- or something else
    end
end
When I write def I mean function.
User avatar
rhezalouis
Party member
Posts: 100
Joined: Mon Dec 07, 2009 10:27 am
Location: Indonesia
Contact:

[RLC]responses

Post by rhezalouis »

howdy!

ah, sorry, i forgot to mention the values in the file. i use this keyList.txt [the quotes are experimental]:

Code: Select all

--
love.key_return
"love.key_space"
><
and the extraction returns these values correctly, so that the variable printing yields:
  • tKey[0] = love.key_return
  • tKey[1] = "love.key_space"
throwing the whole name would cease it from being a key constant from the table love[]. my code seems to cause the assignments to be:

Code: Select all

tKey[0] = "love.key_return"
tKey[1] = "\"love.key_space\""
  • @robin: huaaa, i did not notice that the constant is a table under the name "love". i thank you very much. you are right, i have changed the text file to:

    Code: Select all

    --
    return
    space
    ><
    and uses your

    Code: Select all

    tKey[counter] = love["key_"..vLine];
    so that the assignment is correct:

    Code: Select all

    tKey[0] = love["key_return"]
    --or
    tKey[1] = love.key_space
    and the comparison works fine. these chunks are quite tricky, aren't they? :ultrahappy:
    i thought using this modular-list scheme would simplify modification of the programme. do you think that this external file would ease the saving process or something else?
  • @kikito: thank you so much for your kind response.
    i have put these keys in a separate file; but it is formatted as a list of keys, so that no lua is required in that file. i'll try to name the indexes of the key using another LÖVE-LuaStringScript combo. that's a good advice. :megagrin:
Aargh, I am wasting my posts! My citizenshiiiip... :o
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 193 guests