• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

[Lua help] Translating lua into english

KingWit

('_')
Joined
Jan 3, 2008
Messages
62
Reaction score
0
Location
Sweden
Well since I'm a newbie on scripting and such there is always things that is hard to figure out by myself. I'm sure there is someone out there that has the same problem, that's why I decided to make this thread.

Before I use any script, I'd like to understand it first to learn it and maybe someday I'll make a similar script or something. But there is a lot of things that I dont understand and I hope some people can help me out.

I'm going to post some random scripts here from OTland and I'd like people who already knows how to script help me out with translating [more explaining] some things for me.

Here is a script from Mock
Code:
--- Perfect refine system by Mock the bear (MTB).
--- Email: [email][email protected][/email]
local gain = {
gainArmor='&p+1',loseArmor='&p-1',
gainShield='&s+#',loseShield='&s-(#+1)',
gainAttack='&a+#+1',loseAttack='&a-(#+1)-1',
gainDefense='&d+#+2',loseDefense='&d-(#+1)-2',
chance='100/((#*(1/(@/2)))*(@/2))',  -- This equation its good to use items 0-7.
--- This equation must need return chance in % (0-100) 100 = always, 0 = never.
maxlvl = 7,
blocked_ids = {2488,8881}
}
-- &a = weapon attack
-- &d = weapon defense
-- &s = shield defense
-- &p = armor defense
-- # = weapon curr level
-- @ = max level
function isArmor(uid) -- Function by Mock the bear.
         uid = uid or 0
         if getItemArmor(uid) > 0 and getItemAttack(uid) == 0 and getItemDefense(uid) == 0 and getItemWeaponType(uid) == 0 then
             return TRUE
         end
         return FALSE
end
function isWeapon(uid) -- Function by Mock the bear.
         uid = uid or 0
         local f = getItemWeaponType(uid)
[COLOR="Red"][SIZE="4"]         if f == 1 or f == 2 or f == 3 then[/SIZE][/COLOR]
             return TRUE
         end
         return FALSE        
end
function isShield(uid) -- Function by Mock the bear.
         uid = uid or 0
         if getItemWeaponType(uid) == 4 then
             return TRUE
         end
         return FALSE        
end

function getWeaponLevel(uid) -- Function by Mock the bear.
   uid = uid or 0
   local name = getItemName(uid)
   local _,_,lvl = string.find(name,'+(%d+)')
   return tonumber(lvl) or 0
end
function doTransform(s,uid) -- Function by Mock the bear.
    local c = string.gsub(s,'@',gain.maxlvl)
    local c = string.gsub(c,'&a',getItemAttack(uid))
    local c = string.gsub(c,'&d',getItemDefense(uid))
    local c = string.gsub(c,'&s',getItemDefense(uid))
    local c = string.gsub(c,'&p',getItemArmor(uid))
    local c = string.gsub(c,'#',getWeaponLevel(uid))
    local q,err = loadstring('return '..c)
    assert(q,err)
    return assert(q())
end
function onUse(cid, item, fromPosition, itemEx, toPosition)
         toPosition.stackpos = 255
         if isInArray(gain.blocked_ids, itemEx.itemid) == TRUE
          or getItemWeaponType(itemEx.uid) > 4
           or (getItemWeaponType(itemEx.uid) == 0
            and isArmor(itemEx.uid) == FALSE)
             or itemEx.itemid == 0 then
                doPlayerSendTextMessage(cid, 24,"You cant refine this item.")
                return TRUE
         end
         if isCreature(itemEx.uid) == TRUE then
            return FALSE
         end
        local level = getWeaponLevel(itemEx.uid)
        local chance = doTransform(gain.chance,itemEx.uid)
        if chance >= math.random(0,100) or item.actionid >= 1000 then
           if level+1 > gain.maxlvl then
              doSendMagicEffect(toPosition, 2)
              return doPlayerSendTextMessage(cid, 24,"Your item is on max level, you can't upgrade it.")
           else
              setItemName(itemEx.uid, getItemNameById(itemEx.itemid)..' +'..(level+1))
              doPlayerSendTextMessage(cid, 24,"Your item has been upgrated to +"..(level+1)..".")
              doSendMagicEffect(toPosition, 12)
              if isArmor(itemEx.uid) == TRUE then
                 local get = doTransform(gain.gainArmor,itemEx.uid)
                 setItemArmor(itemEx.uid,get)
              elseif isWeapon(itemEx.uid) == TRUE then
                  setItemAttack(itemEx.uid, doTransform(gain.gainAttack,itemEx.uid))
                  setItemDefense(itemEx.uid, doTransform(gain.gainDefense,itemEx.uid))              
              elseif isShield(itemEx.uid) == TRUE then
                  setItemDefense(itemEx.uid, doTransform(gain.gainShield,itemEx.uid))  
              end
           end
        else
           
           if level == 0 then
               doPlayerSendTextMessage(cid, 24,"No effect.")
               doSendMagicEffect(toPosition, 2)
           elseif level == gain.maxlvl then
                  doSendMagicEffect(toPosition, 2)
                  return doPlayerSendTextMessage(cid, 24,"Your item is on max level, you can't upgrade it.")
           elseif level > 0 then
               if level == 1 then
                   setItemName(itemEx.uid, getItemNameById(itemEx.itemid))
                   doPlayerSendTextMessage(cid, 24,"Your item back to normal.")
               else
                   setItemName(itemEx.uid, getItemNameById(itemEx.itemid)..' +'..(level-1))
                   doPlayerSendTextMessage(cid, 24,"Your item back to +"..(level-1)..".")
               end
              if isArmor(itemEx.uid) == TRUE then
                 setItemArmor(itemEx.uid,doTransform(gain.loseArmor,itemEx.uid))
              elseif isWeapon(itemEx.uid) == TRUE then
                  setItemAttack(itemEx.uid, doTransform(gain.loseAttack,itemEx.uid))
                  setItemDefense(itemEx.uid, doTransform(gain.loseDefense,itemEx.uid))              
              elseif isShield(itemEx.uid) == TRUE then
                  setItemDefense(itemEx.uid, doTransform(gain.loseShield,itemEx.uid))  
              end
           end
           doSendMagicEffect(toPosition, 9)
        end
     doRemoveItem(item.uid,1)  
     return TRUE

Code:
 if f == 1 or f == 2 or f == 3

This one, Ive never understood that. I mean f? means?
Sometimes I see scripts that is similar but instead of having:

PHP:
f == ~~

It has:

Code:
i = ~~

Anyone got a good explanation?

Ill rep++

Thanks

PS, this thread is to everyone who need helps with understanding things. If you have something to ask, would be good if you post it here so people doesn't have to search around for simple explanation.
 
Last edited:
Code:
local wallPos = {x = 1157, y = 1291, z = 9}
local wallId = 1354

local function reset(leverPos)
	doCreateItem(wallId, 1, wallPos)
	doTransformItem(getTileItemById(leverPos, 1946).uid, 1945)
end

function onUse(cid, item, fromPosition, itemEx, toPosition)	
	if item.itemid == 1945 then
		doRemoveItem(getTileItemById(wallPos, wallId).uid)
		doTransformItem(item.uid, 1946)
		addEvent(event, 1 * 60 * 1000, toPosition)
	end
	return true
end

Actually there is few things in your script that I don't understand:

Code:
[COLOR="Red"]local function reset[/COLOR][COLOR="Olive"](leverPos)[/COLOR]
	doCreateItem(wallId, 1, wallPos)
	doTransformItem([COLOR="DarkSlateBlue"]getTileItemById[/COLOR](leverPos, 1946).uid, 1945)

Those in colours? :S
 
Last edited:
Actually there is few things in your script that I don't understand:

Code:
[COLOR="Red"]local function reset[/COLOR][COLOR="Olive"](leverPos)[/COLOR]
	doCreateItem(wallId, 1, wallPos)
	doTransformItem([COLOR="DarkSlateBlue"]getTileItemById[/COLOR](leverPos, 1946).uid, 1945)

Those in colours? :S

  • local function reset
    See link in Syntax's post for explanation about local functions.​

    [*](leverPos)
    We had to include lever's position because we can't just use item.uid; UIDs change between script callbacks which means we have to get the lever's UID again.​

    [*]getTileItemById
    getThingfromPos is deprecated, which means it's better to use getTileItemById(pos, itemid) in this case. We can be sure that it'll find the lever even if there are other items/players stacked on top of it.​
 
Code:
for k, t in ipairs(dateFormat) do
        local v = math.floor(t[2])
        if(v > 0) then
            table.insert(out, (k < #dateFormat and (#out > 0 and ', ' or '') or ' and ') .. v .. ' ' .. t[1] .. (v ~= 1 and 's' or ''))
        end
    end

Those lines is hard to understand, anyone can explain? xD
 
Back
Top