Function within a function

User avatar
Vadi
Posts: 5035
Joined: Sat Mar 14, 2009 3:13 pm

Re: Function within a function

Post by Vadi »

lua_isnumber is for the C api... you want if type(thing) == "number"

Yetzederixx
Posts: 186
Joined: Sun Nov 14, 2010 5:57 am

Re: Function within a function

Post 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.

tarrant
Posts: 49
Joined: Thu Apr 15, 2010 10:36 pm

Re: Function within a function

Post 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?

Yetzederixx
Posts: 186
Joined: Sun Nov 14, 2010 5:57 am

Re: Function within a function

Post 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.

Post Reply