Search Results

Search found 406 results on 17 pages for 'lua'.

Page 6/17 | < Previous Page | 2 3 4 5 6 7 8 9 10 11 12 13  | Next Page >

  • programming in lua, objects

    - by anon
    Sample code: function Account:new (o) o = o or {} -- create object if user does not provide one setmetatable(o, self) self.__index = self return o end taken from: http://www.lua.org/pil/16.1.html What is the purpose of the: self.__index = self line? And why is it executed every time an object is created?

    Read the article

  • A couple of pattern matching issues with pattern matching in Lua

    - by Josh
    I'm fairly new to lua programming, but I'm also a quick study. I've been working on a weather forecaster for a program that I use, and it's working well, for the most part. Here is what I have so far. (Pay no attention to the zs.stuff. That is program specific and has no bearing on the lua coding.) if not http then http = require("socket.http") end local locale = string.gsub(zs.params(1),"%s+","%%20") local page = http.request("http://www.wunderground.com/cgi-bin/findweather/getForecast?query=" .. locale .. "&wuSelect=WEATHER") local location = string.match(page,'title="([%w%s,]+) RSS"') --print("Gathering weather information for " .. location .. ".") --local windspeed = string.match(page,'<span class="nobr"><span class="b">([%d.]+)</span>&nbsp;mph</span>') --print(windspeed) local condition = string.match(page, '<td class="vaM taC"><img src="http://icons-ecast.wxug.com/i/c/a/[%w_]+.gif" width="42" height="42" alt="[%w%s]+" class="condIcon" />') --local image = string.match(page, '<img src="http://icons-ecast.wxug.com/i/c/a/(.+).gif" width="42" height="42" alt="[%w%s]+" class="condIcon" />') local temperature = string.match(page,'pwsvariable="tempf" english="&deg;F" metric="&deg;C" value="([%d.]+)">') local humidity = string.match(page,'pwsvariable="humidity" english="" metric="" value="(%d+)"') zs.say(location) --zs.say("image ./Images/" .. image .. ".gif") zs.say("<color limegreen>Condition:</color> <color white>" .. condition .. "</color>") zs.say("<color limegreen>Temperature: </color><color white>" .. temperature .. "F</color>") zs.say("<color limegreen>Humidity: </color><color white>" .. humidity .. "%</color>") My main issue is this: I changed the 'condition' and added the 'image' variables to what they are now. Even though the line it's supposed to be matching comes directly from the webpage, it fails to match at all. So I'm wondering what it is I'm missing that's preventing this code from working. If I take out the <td class="vaM taC">< img src="http://icons-ecast.wxug.com/i/c/a/[%w_]+.gif" it'll match condition flawlessly. (For whatever reason, I can't get the above line to display correctly, but there is no space between the `< and img) Can anyone point out what is wrong with it? Aside from the pattern matching, I assure you the line is verbatim from the webpage. Another question I had is the ability to match across line breaks. Is there any possible way to do this? The reason why I ask is because on that same page, a few of the things I need to match are broken up on separate lines, and since the actual pattern I'm wanting to match shows up in other places on the page, I need to be able to match across line breaks to get the exact pattern. I appreciate any help in this matter!

    Read the article

  • LUA: A couple of pattern matching issues

    - by Josh
    I'm fairly new to lua programming, but I'm also a quick study. I've been working on a weather forecaster for a program that I use, and it's working well, for the most part. Here is what I have so far. (Pay no attention to the zs.stuff. That is program specific and has no bearing on the lua coding.) if not http then http = require("socket.http") end local locale = string.gsub(zs.params(1),"%s+","%%20") local page = http.request("http://www.wunderground.com/cgi-bin/findweather/getForecast?query=" .. locale .. "&wuSelect=WEATHER") local location = string.match(page,'title="([%w%s,]+) RSS"') --print("Gathering weather information for " .. location .. ".") --local windspeed = string.match(page,'<span class="nobr"><span class="b">([%d.]+)</span>&nbsp;mph</span>') --print(windspeed) local condition = string.match(page, '<td class="vaM taC"><img src="http://icons-ecast.wxug.com/i/c/a/[%w_]+.gif" width="42" height="42" alt="[%w%s]+" class="condIcon" />') --local image = string.match(page, '<img src="http://icons-ecast.wxug.com/i/c/a/(.+).gif" width="42" height="42" alt="[%w%s]+" class="condIcon" />') local temperature = string.match(page,'pwsvariable="tempf" english="&deg;F" metric="&deg;C" value="([%d.]+)">') local humidity = string.match(page,'pwsvariable="humidity" english="" metric="" value="(%d+)"') zs.say(location) --zs.say("image ./Images/" .. image .. ".gif") zs.say("<color limegreen>Condition:</color> <color white>" .. condition .. "</color>") zs.say("<color limegreen>Temperature: </color><color white>" .. temperature .. "F</color>") zs.say("<color limegreen>Humidity: </color><color white>" .. humidity .. "%</color>") My main issue is this: I changed the 'condition' and added the 'image' variables to what they are now. Even though the line it's supposed to be matching comes directly from the webpage, it fails to match at all. So I'm wondering what it is I'm missing that's preventing this code from working. If I take out the <td class="vaM taC">< img src="http://icons-ecast.wxug.com/i/c/a/[%w_]+.gif" it'll match condition flawlessly. (For whatever reason, I can't get the above line to display correctly, but there is no space between the `< and img) Can anyone point out what is wrong with it? Aside from the pattern matching, I assure you the line is verbatim from the webpage. Another question I had is the ability to match across line breaks. Is there any possible way to do this? The reason why I ask is because on that same page, a few of the things I need to match are broken up on separate lines, and since the actual pattern I'm wanting to match shows up in other places on the page, I need to be able to match across line breaks to get the exact pattern. I appreciate any help in this matter!

    Read the article

  • Lua - Iterate Through Table With nil Values

    - by Tony Trozzo
    My lua function receives a table that is of the array form: { field1, field2, nil, field3, } No keys, only values. I'm trying to convert this to a pseudo CSV form by grabbing all the fields and concatenating them into a string. Here is my function: function ArenaRewind:ConvertToCSV(tableName) csvRecord = "\n" for i,v in pairs(tableName) do if v == nil then v = "nil" end csvRecord = csvRecord .. "\"" .. v .. "\"" if i ~= #tableName then csvRecord = csvRecord .. "," end end return csvRecord end Not the prettiest code by any means, but it seems to iterate through them and grab all the non-nil values. The other table iteration function is ipairs() which stops as soon as it hits a nil value. Is there any easy way to grab all of these fields including the nil values? The tables are various sizes, so I hope to refrain from accessing each part like an array [i.e., tableName[1] through tableName[4]) and just grabbing the nil values that way. Thanks in advance.

    Read the article

  • Lua : Dynamicly calling a function with arguments.

    - by Tipx
    Using Lua, I'm trying to dynamicly call a function with parameters. What I want to have it done is I send a string to be parsed in a way that : 1st argument is a class instance "Handle" 2nd is the function to be called All that is left are arguments "modules" is a a table like { string= } split() is a simple parser that returns a table with indexed strings function Dynamic(msg) local args = split(msg, " ") module = args[1] table.remove(args, 1) if module then module = modules[module] command = args[1] table.remove(args, 1) if command then if not args then module[command]() else module[command](unpack(args)) -- Reference 1 end else -- Function doesnt exist end else -- Module doesnt exist end end When I try this with "ignore remove bob", by "Reference 1", it tries to call "remove" on the instance associated with "ignore" in modules, and gives the argument "bob", contained in a table (with a single value). However, on the other side of the call, the remove function does not receive the argument. I even tried to replace the "Reference 1" line with module[command]("bob") but I get the same result.

    Read the article

  • Lua : Dynamically calling a function with arguments.

    - by Tipx
    Using Lua, I'm trying to dynamically call a function with parameters. I want to send a string to be parsed in a way that: 1st argument is a class instance "Handle" 2nd is the function to be called All that is left are arguments "modules" is a a table like { string=<instance of a class> } split() is a simple parser that returns a table with indexed strings. function Dynamic(msg) local args = split(msg, " ") module = args[1] table.remove(args, 1) if module then module = modules[module] command = args[1] table.remove(args, 1) if command then if not args then module[command]() else module[command](unpack(args)) -- Reference 1 end else -- Function doesnt exist end else -- Module doesnt exist end end When I try this with "ignore remove bob", by "Reference 1", it tries to call "remove" on the instance associated with "ignore" in modules, and gives the argument "bob", contained in a table (with a single value). However, on the other side of the call, the remove function does not receive the argument. I even tried to replace the "Reference 1" line with module[command]("bob") but I get the same result.

    Read the article

  • Lua class instance with nested tables

    - by Anonnobody
    Hello, Simple lua game with simple class like so: creature = class({ name = "MONSTER BADDY!", stats = { power = 10, agility = 10, endurance = 10, filters = {} }, other_things = ... }) creatureA = creature.new() creatureB = creature.new() creatureA.name = "Frank" creatureB.name = "Zappa" creatureA.stats.agility = 20 creatureB.stats.power = 12 -- blah blah blah Non table values are individual to each instance, but table values are shared among all instances and if I modify a stats.X value in one instance, all other instances see the same stats table. Q1: Is my OO implementation flawed? I tried LOOP and the same result occured, is there a fundamental flaw in my logic? Q2: How would you have each instance of creature have it's own stats table (and sub tables)? PS. I cannot flatten my class table as it's a bit more complicated than the example and other parts of the code are simplified with this nested table implementation. Thanks a bunch.

    Read the article

  • Why is lua crashing after extracting zip files?

    - by Brian T Hannan
    I have the following code but it crashes every time it reaches the end of the function, but it successfully extracts all the files and puts them in the right location. require "zip" function ExtractZipAndCopyFiles(zipPath, zipFilename, destinationPath) local zfile, err = zip.open(zipPath .. zipFilename) -- iterate through each file insize the zip file for file in zfile:files() do local currFile, err = zfile:open(file.filename) local currFileContents = currFile:read("*a") -- read entire contents of current file local hBinaryOutput = io.open(destinationPath .. file.filename, "wb") -- write current file inside zip to a file outside zip if(hBinaryOutput)then hBinaryOutput:write(currFileContents) hBinaryOutput:close() end end zfile:close() end -- call the function ExtractZipAndCopyFiles("C:\\Users\\bhannan\\Desktop\\LUA\\", "example.zip", "C:\\Users\\bhannan\\Desktop\\ZipExtractionOutput\\") Why does it crash every time it reaches the end?

    Read the article

  • Lua: Random: Percentage

    - by jargl
    I'm creating a game and currently have to deal with some math.randomness. As I'm not that strong in Lua, how do you think Can you make an algorithm that uses math.random with a given percentage? I mean a function like this: function randomChance( chance ) -- Magic happens here -- Return either 0 or 1 based on the results of math.random end randomChance( 50 ) -- Like a 50-50 chance of "winning", should result in something like math.random( 1, 2 ) == 1 (?) randomChance(20) -- 20% chance to result in a 1 randomChance(0) -- Result always is 0 However I have no clue how to go on, and I completely suck at algorithms I hope you understood my bad explanation of what I'm trying to accomplish

    Read the article

  • Lua - How to use functions from another script

    - by Person
    I'm wondering how to use functions from another script in Lua. For example, say GameObjectUtilities holds functions that many GameObject scripts will use. The Slime (a GameObject) script wants to use a function in GameObjectUtilities. I'm having trouble getting this to work. I've looked here, but I still don't really fully understand. Do I need to create a module or a table to hold the functions in GameObjectUtilities for the functions in it to be used in other scripts? If so, what is the best way to go about this?

    Read the article

  • Lua not producing table of functions (IO API)

    - by ArtOfCode
    I'm working on a basic project in Lua. I've been trying to get data from files using the IO API (as defined here), but when I open a file and give it a handle, it doesn't seem to return a table of functions. The (erroneous bit of) code: local unread = fs.list("email/"..from.."/") local send = "" for _,file in ipairs(unread) do local handle = io.open(file,"r") local text = handle:read("*a") send = send .. text .. "\n" handle.close() fs.delete(file) end The fs you see on the first line is a professional filesystem wrapper round the IO API, not my work and perfectly error-free, so that's not the problem. However, when I try to read the file (handle:read()), it throws "attempt to index nil". Tracing it, it turns out that handle itself is nil. Any ideas?

    Read the article

  • Lua Operator Overloading

    - by Pessimist
    I've found some places on the web saying that operators in Lua are overloadable but I can't seem to find any example. Can someone provide an example of, say, overloading the + operator to work like the .. operator works for string concatenation? EDIT 1: to Alexander Gladysh and RBerteig: If operator overloading only works when both operands are the same type and changing this behavior wouldn't be easy, then how come the following code works? (I don't mean any offense, I just started learning this language): printf = function(fmt, ...) io.write(string.format(fmt, ...)) end Set = {} Set.mt = {} -- metatable for sets function Set.new (t) local set = {} setmetatable(set, Set.mt) for _, l in ipairs(t) do set[l] = true end return set end function Set.union (a,b) -- THIS IS THE PART THAT MANAGES OPERATOR OVERLOADING WITH OPERANDS OF DIFFERENT TYPES -- if user built new set using: new_set = some_set + some_number if type(a) == "table" and type(b) == "number" then print("building set...") local mixedset = Set.new{} for k in pairs(a) do mixedset[k] = true end mixedset[b] = true return mixedset -- elseif user built new set using: new_set = some_number + some_set elseif type(b) == "table" and type(a) == "number" then print("building set...") local mixedset = Set.new{} for k in pairs(b) do mixedset[k] = true end mixedset[a] = true return mixedset end if getmetatable(a) ~= Set.mt or getmetatable(b) ~= Set.mt then error("attempt to 'add' a set with a non-set value that is also not a number", 2) end local res = Set.new{} for k in pairs(a) do res[k] = true end for k in pairs(b) do res[k] = true end return res end function Set.tostring (set) local s = "{" local sep = "" for e in pairs(set) do s = s .. sep .. e sep = ", " end return s .. "}" end function Set.print (s) print(Set.tostring(s)) end s1 = Set.new{10, 20, 30, 50} s2 = Set.new{30, 1} Set.mt.__add = Set.union -- now try to make a new set by unioning a set plus a number: s3 = s1 + 8 Set.print(s3) --> {1, 10, 20, 30, 50}

    Read the article

  • Assistance with Lua functions

    - by Josh
    As noted before, I'm relatively new to lua, but again, I learn quick. The last time I got help here, it helped me immensely, and I was able to write a better script. Now I've come to another question that I think will make my life a bit easier. I have no clue what I'm doing with functions, but I'm hoping there is a way to do what I want to do here. Below, you'll see an example of code I have to do to strip down some unneeded elements. Yeah, I realize it's not efficient in the least, so if anyone else has a better idea of how to make it much more efficient, I'm all ears. What I would like to do is create a function with it so that I can strip down whatever variable with a simple call of it (like stripdown(winds)). I appreciate any help that is offered, and any lessons given. Thanks! winds = string.gsub(winds,"%b<>","") winds = string.gsub(winds,"%c"," ") winds = string.gsub(winds," "," ") winds = string.gsub(winds," "," ") winds = string.gsub(winds,"^%s*(.-)%s*$", "%1)") winds = string.gsub(winds,"&nbsp;","") winds = string.gsub(winds,"/ ", "(") Josh

    Read the article

  • accessing c++ class members with luaplus

    - by cppanda
    i've implemented LuaPlus in my engine eventmanager successfully and really like the flexibility i gained. but i'm still not exactly where i want to by, because i can't link my c++ classes to a lua class. for example i have a Actor class in c++, and i want to be able to create the same class in lua and gain access to members with luaplus, but i can't figure how i can achieve that. Is this actually luaplus built in functionality, or do i have to write my own interface that exchanges data tables between c++ and lua? my current approach would be to fire an event in luascript that creates an new actor class in c++ code, and transfer its id and the data i need to back to lua. when i modify the data i send the modifications back to c++ code again, but i actually thought there's something in luaplus that exposes this functionality already.

    Read the article

  • Lua Alien Module - Trouble using WriteProcessMemory function, unsure on types (unit32)

    - by jefferysanders
    require "alien" --the address im trying to edit in the Mahjong game on Win7 local SCOREREF = 0x0744D554 --this should give me full access to the process local ACCESS = 0x001F0FFF --this is my process ID for my open window of Mahjong local PID = 1136 --function to open proc local op = alien.Kernel32.OpenProcess op:types{ ret = "pointer", abi = "stdcall"; "int", "int", "int"} --function to write to proc mem local wm = alien.Kernel32.WriteProcessMemory wm:types{ ret = "long", abi = "stdcall"; "pointer", "pointer", "pointer", "long", "pointer" } local pRef = op(ACCESS, true, PID) local buf = alien.buffer("99") -- ptr,uint32,byte arr (no idea what to make this),int, ptr print( wm( pRef, SCOREREF, buf, 4, nil)) --prints 1 if success, 0 if failed So that is my code. I am not even sure if I have the types set correctly. I am completely lost and need some guidance. I really wish there was more online help/documentation for alien, it confuses my poor brain. What utterly baffles me is that it WriteProcessMemory will sometimes complete successfully (though it does nothing at all, to my knowledge) and will also sometimes fail to complete successfully. As I've stated, my brain hurts. Any help appreciated.

    Read the article

  • First character uppercase LUA

    - by Tomek
    Hi anyone know if theres a function to make the first character in a word uppercase (like ucfirst in php) and how how i impent it in this? I want keywords[1] to be first letter uppercase It says string.upper does it but i maked the whole word uppercase

    Read the article

  • Lua Programming for PSP: Sending a string, then spliting that string

    - by Trey Andrews
    How do I send a string between 2 PSPs? Here is a test script: Script A Adhoc.init() Adhoc.connect() data1 = "Trey777" data2 = "This is a test!!..." Outdata = "Name"..data1.."text"..data2 function senddata() Adhoc.send(Outdata) end While true do screen.waitVblankStart() screen:flip() end Script B red = Color.new(255,0,0) Adhoc.init() Adhoc.connect() function recevmsg() data = Adhoc.recv() ----What do I here to split the text? ----print name to line '0,10' ----print text to line'0,20' end While true do screen.waitVblankStart() screen:flip() end Each script will be loaded onto a different PSP. One will send and one will receive. I need to know how does it split a string? string.find and sub return numbers, not text

    Read the article

  • Lua SQL: peeking at cursors

    - by NP
    I am using LuaSQL, and query for a result set using con:execute(sql_stmt), which returns a cursor. How do I see if there is at least one row in that resultset, without doing a cursor:fetch to pop that first row?

    Read the article

  • Why use a do-end block in Lua?

    - by Mayron
    I keep trying to find answers for this but fail to do so. I wanted to know, what is the do-end block actually used for? It just says values are used when needed in my book so how could I use this? Do I use it to reduce the scope of local variables by placing a function in a do-end loop and place local variables outside of the function but inside this do-end block and the variables will be seen by the function? But then can the function still be called? Sorry for being very vague. I hope that makes sense. Maybe an illustrated example might be useful ^^

    Read the article

  • Sorting in Lua, counting number of items

    - by Josh
    Two quick questions (I hope...) with the following code. The script below checks if a number is prime, and if not, returns all the factors for that number, otherwise it just returns that the number prime. Pay no attention to the zs. stuff in the script, for that is client specific and has no bearing on script functionality. The script itself works almost wonderfully, except for two minor details - the first being the factor list doesn't return itself sorted... that is, for 24, it'd return 1, 2, 12, 3, 8, 4, 6, and 24 instead of 1, 2, 3, 4, 6, 8, 12, and 24. I can't print it as a table, so it does need to be returned as a list. If it has to be sorted as a table first THEN turned into a list, I can deal with that. All that matters is the end result being the list. The other detail is that I need to check if there are only two numbers in the list or more. If there are only two numbers, it's a prime (1 and the number). The current way I have it does not work. Is there a way to accomplish this? I appreciate all the help! function get_all_factors(number) local factors = 1 for possible_factor=2, math.sqrt(number), 1 do local remainder = number%possible_factor if remainder == 0 then local factor, factor_pair = possible_factor, number/possible_factor factors = factors .. ", " .. factor if factor ~= factor_pair then factors = factors .. ", " .. factor_pair end end end factors = factors .. ", and " .. number return factors end local allfactors = get_all_factors(zs.param(1)) if zs.func.numitems(allfactors)==2 then return zs.param(1) .. " is prime." else return zs.param(1) .. " is not prime, and its factors are: " .. allfactors end

    Read the article

  • Lua-Objective-C bridge on the iphone

    - by John Smith
    I have partially ported the LuaObjCBridge to the iPhone. Most things work but there are still some issues I have to deal with. There are sections where #defines are defined with-respect-to intel or ppc. Is the ARM chip closer to intel or ppc? Here is the most relevant section where most of the defines are: #if defined(__ppc__)||defined(__PPC__)||defined(__powerpc__) #define LUA_OBJC_METHODCALL_INT_IS_SHORTEST_INTEGRAL_TYPE #define LUA_OBJC_METHODCALL_PASS_FLOATS_IN_MARG_HEADER #define LUA_OBJC_POWER_ALIGNMENT #elif defined(__i386__)||defined(__arm__) #warning LuaObjCBridge is not fully tested for use on Intel chips. #define LUA_OBJC_METHODCALL_RETURN_STRUCTS_DIRECTLY // Use this or the code was crashing for me for structs LUA_OBJC_METHODCALL_RETURN_STRUCTS_DIRECTLY_LIMIT #define LUA_OBJC_METHODCALL_USE_OBJC_MSGSENDV_FPRET #define LUA_OBJC_METHODCALL_RETURN_STRUCTS_DIRECTLY_LIMIT 8 #define LUA_OBJC_INTEL_ALIGNMENT #endif For now I added arm with i386, but I could be wrong

    Read the article

  • Lua choose random item from table

    - by Zen
    Seems easy but I just don't get any further: Take this example: local myTable = { 'a', 'b', 'c', 'd' } print( myTable[ math.random( 0, #myTable - 1 ) ] ) Why doesn't it work? Google seems to have no answers on this either

    Read the article

< Previous Page | 2 3 4 5 6 7 8 9 10 11 12 13  | Next Page >