Mycal
Staff member
I've mentioned before that I will be converting all scripts from OdysseyScript (which is similar to VBScript) to Lua several times. I wanted to show what this meant. Lua is a very easy to use language, though it does take some time getting used to. For most scripts, it will do a 1:1 conversion but add comments that show that an alternative may be available. Here is a sample script that was very basic that am using for conversion comparison.
This is a playertell script, that basically finds a user named "bigred" and if he is online, sends the playertell to him as well. It also plays a sound to indicate a message was received to all parties.
Here is the converted Lua code:
Note that there is a comment on the for loop that indicates a function may be able to replace this loop. So the function could actually be shorted to the following.
Another thing to note, scripts will now use their real name as the function name instead of just main for every script as shown above. As I convert more and more complex scripts, I'll be able to fill out this topic with example conversions so you can get a good feel of the language.
This is a playertell script, that basically finds a user named "bigred" and if he is online, sends the playertell to him as well. It also plays a sound to indicate a message was received to all parties.
Code:
Function main(player as long, targetplayer as long, message as string) AS Long
dim a as long, b as long,c as long,soundnum as long
soundnum = 3
for a = 1 to getmaxusers
if strcmp(getplayername(a),"bigred") then
if not strcmp(getplayername(player),"bigred") and not strcmp(getplayername(targetplayer),"bigred") then
playermessage(a,strcat(getplayername(player),strcat(" has told ",strcat(getplayername(targetplayer),strcat(" ", message)))),6)
PlayCustomWav(a,soundnum)
end if
end if
next a
PlayCustomWav(player,soundnum)
PlayCustomWav(targetplayer,soundnum)
main = continue
End Function
Here is the converted Lua code:
Code:
function playertell(player, targetplayer, message)
local soundnum = 3
local a
for a = 0,getmaxusers(),1 do -- can likely be replaced with 'local a = findPlayer(playerName)'
if getPlayerName(a) == "bigred" then
if not ( getPlayerName(player) == "bigred" or getPlayerName(targetplayer) == "bigred" ) then
playermessage(a, getPlayerName(player) .. " has told " .. getPlayerName(targetplayer) .. " " .. message, 6)
playCustomWav(a, soundnum)
end
end
end
playCustomWav(player, soundnum)
playCustomWav(targetplayer, soundnum)
return true
end
Note that there is a comment on the for loop that indicates a function may be able to replace this loop. So the function could actually be shorted to the following.
Code:
function playertell(player, targetplayer, message)
local soundnum = 3
if not ( getPlayerName(player) == "bigred" or getPlayerName(targetplayer) == "bigred" ) then
local a = findPlayer("bigred")
playermessage(a, getPlayerName(player) .. " has told " .. getPlayerName(targetplayer) .. " " .. message, 6)
playCustomWav(a, soundnum)
end
playCustomWav(player, soundnum)
playCustomWav(targetplayer, soundnum)
return true
end
Another thing to note, scripts will now use their real name as the function name instead of just main for every script as shown above. As I convert more and more complex scripts, I'll be able to fill out this topic with example conversions so you can get a good feel of the language.