PDA

View Full Version : What is strval?


SyncBD
04-29-2009, 03:01 PM
What is the purpose of the function strval()? According to VCO wiki, it takes a string as a parameter and returns a number. So I suppose it is to be used when one wants to convert a string representing a number into a real number. For example strval("33") should be 33.

Ok, well that's fairly straight forward, but why is it used with PlayerID or KillerID? As far as I know, both PlayerID and KillerID are numbers, not strings. And what's even more confusing is that there are functions that use getPlayerName(PlayerID) and some other functions that use getPlayerName(strval(PlayerID)). Now what on earth is the point in this?

Mantis
04-29-2009, 07:39 PM
strval is used in functions such as getPlayerName(strval(getPlayerID))
It would be used to find the users name using ID. It's kinda hard to explain, and I may be explaining wrong, it's late.
Just think of it like /area 10 [Pretend 'Mantis' is ID 10]
The script would look through the playerpool and get the name of ID 10 and return my name.

If I'm wrong, bah!

ReGeX
04-29-2009, 08:09 PM
It converts a string parameter into an integer data type. For example,


local MarkerID = {} -- Array

function onPlayerConnect(PlayerID)
-- MarkerID[PlayerID] = 0 -- using this would crash, because PlayerID is of string data type
MarkerID[strval(PlayerID)] = 0 -- this works and will not crash
end

SyncBD
04-30-2009, 06:02 AM
Are you really sure PlayerID is a string? I used this line:
outputChatBox("The type of PlayerID variable is " .. type(PlayerID),255,255,255,255)
in three functions (onPlayerDeath, onPlayerConnect and a custom function) and they all displayed "The type of PlayerID variable is number".

Mex
04-30-2009, 08:05 AM
PlayerID is a number ;P

All the parameters sent after that are Strings.

For Example:
/sethp mex 200
funtion sethp(PlayerID, Player2, Amount)
-- PlayerID is type number
-- Player2 is type string
-- Amount is type string
-- You'll need to use strval() or tonumber() on Amount to convert it to a number.
end

ReGeX
04-30-2009, 03:35 PM
In callbacks especially, it is not a "number". When using arrays, you should always use strval to ensure that it is a numerical data type.