berto
Posts: 20708
Joined: 3/13/2002 From: metro Chicago, Illinois, USA Status: offline
|
Another (private) Dev Forum post: quote:
ORIGINAL: berto From the work-in-progess CSEE How-To: Akin to the on_unit_kill() function is on_unit_reduce(), for example: ------------------------------------------------------------------------------------------------------------------------ function on_unit_reduce (x, y, trackid, pid, name, side, nation, oid, orgname, points, strength, HQ, Leader, loss) -- DO NOT REMOVE if string.find(orgname, "Engineer") ~= nil and side == ISRAELI_SIDE then note(true, "Comment", "Be careful with, be sure to protect your engineer units. You need them to breach the Old City walls.") end end ------------------------------------------------------------------------------------------------------------------------ In this example, if an Israeli engineer unit suffers point reduction, we want to display a pop-up reminding the Israeli player to be careful with such units. A conundrum: How do you determine if a unit is engineers or not? There is a standard CSEE function, counter_type(), like so: ------------------------------------------------------------------------------- FUNCTION counter_type (trackid) DESCRIPTION For the given trackid (counter), returns its type value. INPUTS trackid -- integer; 0 or greater RETURN VALUE Returns a value (not a quoted string!), any of: ARMOR ARTILLERY INFANTRY HEADQUARTERS LEADERS OFFMAP_AIRCRAFT RECON_VEHICLES HELICOPTERS NAVAL TRANSPORTS ANTI_AIRCRAFT RAIL MISC ANTI_TANK SPARE EXAMPLES if counter_type () ~= ARMOR then ... end SEE ALSO ------------------------------------------------------------------------------- But where do you see ENGINEER in that type list? You don't! Unfortunately, there doesn't seem to be any unit ID number range that universally signifies the engineer type. What to do then? With a bit of advanced Lua, we can determine if a given unit is an engineer. You will observe where one of the on_unit_reduce() inputs is the unit's orgname. We can use one of the standard, built-in Lua string library functions, find(), to see if "Engineer" appears anywhere in the unit's orgname. If it does appear, then string.find() returns a non-nil value, and '~= nil' (doesn't equal nil) evaluates to true. So the if condition is satisfied, we have an engineer! Hmm. In future, we might want to determine if other units, in other scenarios, are engineers. How about we define an isengineer() function, in user.lua: ------------------------------------------------------------------------------- function isengineer (trackid) local name = counter_name (trackid) local orgname = counter_orgname (trackid) return string.find(name, "Engineer") ~= nil or string.find(orgname, "Engineer") ~= nil --[[ if string.find(name, "Engineer") ~= nil or string.find(orgname, "Engineer") ~= nil then return true else return false end ]] end ------------------------------------------------------------------------------- Note the use of the --[[ ]] multi-line comment, to demarcate an alternative way to return true or false. This function is superior to our earlier check, is more general, because it checks both the counter name and its orgname to see if either contains the word "Engineer". (We could, if we really wish to, make this case insensitive, so that unit names or orgnames with "engineer" (lower case) would match also. Too, we might look for matches on abbreviations, such as "engr". We must be very careful, however, because we might extend the match set so broadly as to invite false positives.) With the isengineer() function defined, the above code could be redone as ------------------------------------------------------------------------------------------------------------------------ function on_unit_reduce (x, y, trackid, pid, name, side, nation, oid, orgname, points, strength, HQ, Leader, loss) -- DO NOT REMOVE if isengineer (trackid) and side == ISRAELI_SIDE then note(true, "Comment", "Be careful with, be sure to protect your engineer units. You need them to breach the Old City walls.") end end ------------------------------------------------------------------------------------------------------------------------ In fact, this technique is so powerful that we could define (in user.lua) all manner of custom is*() functions, such as: isjeep () isrclr () ismortar () ishowitzer () ismachinegun () ishorse () iscamel () and on and on. Nifty, huh?
Attachment (1)
< Message edited by berto -- 12/7/2017 8:00:48 PM >
_____________________________
|