In a recent Steam thread, Lee hinted at using the entity name property to pass a variable list for use in GG so I set about making something up to test it out.
In short, this code is a framework for passing a var list via entity name and is parsed to break it into multiple varName's and varValue's so you can use it in code. Examples for use might be tree shade and height etc., weight of objects... personal name of entities... etc. You name it.
EG: A barrel that has a weight of 200kg and a strength of 50 might be named "b1 w200 s50". This parser will seperate the "variable list" into
b1
w200
s50
and subsequently broken further into
varName= "b" varValue ="1"
varName= "w" varValue="200"
varName="s" varValue="50"
From this you can write a simple function or if statement etc. to determine what you want to do.
eg:
if varName == "b" then
doBarrelCode(varNum)
end
This type of hack has been on my list for a long time and although I had a rough entity grabbing script previously I wasn't happy with performance so I rewrote a version making better use of LUA string functions and I think I have a done it the fastest and most efficient way possible. (always happy to be corrected)
NOTES
1) This parser does follow a reasonable naming convention (the options are infinite right?) but I tested it with both one and many "options" and values.
2) This is a coders piece of script to be torn apart and utilised, not a PnP script for non-coders sorry.
-- CODE BY !nullptr (aka SisterMatic) - please credit - not fussed - just don't call it your code :)
-- OVERVIEW
-- In a Steam discussion recently (May 2015) Lee (Bamber) posed the idea of using the entity name as a variable "carrier" and parsing the name
-- to have access to entity "variables". Since not having a variable list to play with has been a bugbear of mine I thought it was
-- time to code something up to do just what Lee suggested. After a couple of hours I managed to hack this together.
-- Herein is a working "entity name variable passer parser".
-- USAGE
-- For this example I used barrels (see images) and attached this script to it (make sure it is active)
-- Use a name for the barrel with each variable in the NAME properties seperated by non-alpha numeric chars. This was tested using space, colon and hyphen.
-- EG: b1:h20:s7 or b1 h20 s7 or b1-h20-s7 etc...Other combos should work OK, I leave that to the scripter
-- In this example I have used b1-h100-s20 as a barrel name. (see images for others) This will give me 3 variable names with 3 different values (as you can see).
-- NOTE: A bag of chips could be chips20
-- 95% of the code in this example is taken up with display text so you can see the workings.
-- I have included how you might call a function based on the variable name including the value passed
-- KNOWN ISSUES
-- Not so much an issue as something to watch for. if you have 2 (or more) entities within range of PlayerDist then each entity will be queried simultaneously.
-- you will need to isolate/seperate/distance the entities if you do now want this.
-- Where is has value and this is an example of it, I intend to rewrite my tree shade script and I can combine the values of multiple entities without carrying or
-- querying a tree array - which is how I previously did it.
my_object_name = {}
function objtest_init_name(e,name)
my_object_name[e] = name
strIDX = {}
end
function objtest_main(e)
PlayerDist = GetPlayerDistance(e)
-- if you shorten the distance you are less likely to get "reads" from multiple entities. (see known issues)
if GetPlayerDistance(e) <= 150 then
parseObj(my_object_name[e])
end
end
function parseObj(s)
-- set array
local tbl = {}
-- set array index
local tblIDX = 1
local y = 10 -- debug display position only - can delete
-- capture all sub parts of the string seperated by **NON** alphanumeric - eg: spaces or punctuation etc can be used as delimiters
-- tested with colon (:) and space ( ) and hyphen (-) and works fine
for i in string.gmatch(s, "%w+") do --Where "%w+" is the alphanumeric string pattern.
tbl[tblIDX] = i
tblIDX=tblIDX+1
end
-- IMPORTANT NOTE: tblIDX is 1 more than it should be due to last call in loop. Decrement to avoid confusion
tblIDX = tblIDX - 1
-- ****************
-- debug - display the strings we have
TextCenterOnX(50,y,1,"We have "..tblIDX.." var strings derived from the entity name of "..s)
for i=1, tblIDX, 1 do
y=y+3
TextCenterOnX(50,y,1,tbl[i])
end -- debug display
-- ****************
-- set these in case we do not get a return from string.match
local varName = ""
local varValue = ""
y = y + 5 -- debug display only
for i=1, tblIDX, 1 do
varName = string.match(tbl[i], "%a+") -- capture the alpha
varValue = string.match(tbl[i], "%d+") -- capture the numeric
-- THE EXAMPLE PART NOW WE HAVE THE STRING SUBSETS
-- We are using a barrel with the name set as **b7-h100-s20**. Other barrels are different and show accordingly.
--**************** THE PLAY AREA ******************
if varName == "b" then
TextCenterOnX(50,y,1,"This is a BARREL. It's number is "..varValue)
y=y+3
-- make an example function call for this entity
doBarrel(varValue,y) -- pass y just for formatting purposes on text display
y=y+3
elseif varName == "h" then
TextCenterOnX(50,y,1,"This is the 2nd variable of our barrel. It's value is "..varValue)
y=y+3
elseif varName == "s" then
TextCenterOnX(50,y,1,"This is the 3rd variable of our barrel. It's value is "..varValue)
y=y+3
else -- display everything not queried
TextCenterOnX(50,y,1,"My variable name is "..varName.. " with a value of "..varValue.. ". Cool huh?")
y=y+3
end -- play area
end -- for
end -- func parseObj
-- example function call
function doBarrel(varValue,y)
TextCenterOnX(50,y,1,"This is the BARREL FUNCTION CALL. The value passed is "..varValue)
end -- doBarrel
Development/ Gaming Rigs
Sys 1: i7-4770 (3.5)/16Gb/128 SSD/3Tb/970gtx/2 x 23, 1 x 27 LCD - Sys 2: i7/8Gb/670gtx/1.5Tb/1 x 23 LCD - Sys 3: Amd Quad/8Gb/645gtx/1Tb/30" LCD