Converting Scripts to Lua

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.

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.
 

Alpha

New Member
I'm very good at OdyScript (MBSC), but I know nothing about Lua. That being said, I think Lua looks promising, and I'm rather excited to see it incorporated in Odyssey. Could be a nice fresh change.
 

Mycal

Staff member
It's not too hard, its a high level language like vb, but a bit more powerful and a great deal faster. It has some OOP characteristics unlike vb though not sure if I'll make use of that or not. I'm still working out kinks though..
 
Top