Page 2 of 2

Re: Function within a function

Posted: Tue Nov 30, 2010 7:32 pm
by Vadi
lua_isnumber is for the C api... you want if type(thing) == "number"

Re: Function within a function

Posted: Tue Nov 30, 2010 11:38 pm
by Yetzederixx
Multi purpose ammobuy alias

Code: Select all

Pattern: ^ammobuy\s?(\w*)\s?(\d*)$

Usage 1: ammobuy type num -- buys num of type
Usage 2: ammobuy type     -- buys up to ammoUpperLimit of this type
Usage 3: ammobuy num      -- buys more of last type bought
Usage 4: ammobuy          -- buys up to ammoUpperLimit of last type bought
Code: [show] | [select all] lua
local numToBuy = -1

-- process matches[2]
if not tonumber(matches[2]) and matches[2] ~= "" then
   typeOfAmmoToBuy = matches[2]
elseif tonumber(matches[2]) then
   numToBuy = matches[2]
end

-- process matches[3] if necessary
if matches[3] ~= "" then 
   numToBuy = matches[3]
elseif numToBuy == -1 then 
   numToBuy = ammoUpperLimit - ammo
end

ammo = ammo + numToBuy -- may want to trigger this off a sucessful buy instead of here
ammoBuy("stbuy" .. typeOfAmmoToBuy, numToBuy)
Obviously you can change the word ammobuy to whatever you want. Also this requires the previously defined ammoBuy function.

Re: Function within a function

Posted: Wed Dec 01, 2010 8:30 am
by tarrant
thanks!! i haven't had the chance to try it yet but it looks awesome.

although, i need to ask, why did you set the local variable to -1 at the start? is it just an arbitrary number?

Re: Function within a function

Posted: Wed Dec 01, 2010 8:35 am
by Yetzederixx
Yes, just something definitive to test against later on. You could of set it to nil, false, whatever you want, but I figured that sense it was going to be a number at some point that I would keep it a number throughout, and 0 always tests true in Lua unlike other languages.