• 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!

Beginner Scripters [dummy proof]

Itutorial

Excellent OT User
Joined
Dec 23, 2014
Messages
2,307
Solutions
68
Reaction score
982
1): MAIN FUNCTIONS

A) Creature Scripts:
Code:
onLogin(cid)
onLogout(cid)
onAdvance(cid, skill, oldLevel, newLevel)
onStatsChange(cid, attacker, type, combat, value)
onDirection(cid, old, current)
onOutfit(cid, old, current)
onSendMail(cid, receiver, item, openBox)
onReceiveMail(cid, sender, item, openBox)
onTradeRequest(cid, target, item)
onTradeAccept(cid, target, item, targetItem)
onJoinChannel(cid, channel, users)
onLeaveChannel (cid, channel, users)
onLook(cid, thing, position, lookDistance)
onThink(cid, interval)
onTextEdit(cid, item, newText)
onReportBug(cid, comment)
onAreaCombat(cid, tileItem, tilePosition, isAggressive)
onPush(cid, target)
onTarget(cid, target)
onFollow(cid, target)
onCombat(cid, target)
onAttack(cid, target)
onCast(cid, target)
onKill(cid, target, lastHit)
onDeath(cid, corpse, deathList)
onPrepareDeath(cid, deathList)

B) GlobalEvents:
Code:
onThink(interval, lastExecution, thinkInterval)
onStartup()
onShutdown()
onRecord(current, old, cid)
onTimer()

C) MoveEvents:
Code:
onStepIn(cid, item, position, lastPosition, fromPosition, toPosition, actor)
onStepOut(cid, item, position, lastPosition, fromPosition, toPosition, actor)
onEquip(cid, item, slot)
onDeEquip(cid, item, slot)
onAddItem(moveItem, tileItem, position, cid)
onRemoveItem(moveItem, tileItem, position, cid)

D) TalkActions:
Code:
onSay(cid, words, param, channel)

F) Spells:
Code:
onCastSpell(cid, var)

When writing a script you will always start with one of these main function. They define what action the player must do for the script to start.
 
MAIN FUNCTIONS CONTINUED:

For example, if you want the player to use an item you would use this main function:
Code:
function onUse(cid, item, fromPosition, itemEx, toPosition)

If you want the player to say something like !spells you would use the main function:
Code:
onSay(cid, words, param, channel)


MAIN FUNCTION VERIABLES:

The veriables are what you see in the parenthesis:
Code:
(cid, words, param, channe)
Code:
(cid, item, fromPosition, itemEx, toPosition)

These variables will be set when the player does the action you want them too.

So, when the player uses the item you set the script for it will find the veriables:
(cid, item, fromPosition, itemEx, toPosition)

cid = creature id (This is a number value)
item = the item used (This is also a number)
fromPosition = the position in which the item was used (This is an array)
itemEx = This is used if the player uses and item on an item. This variable is the item it was used on. (A number value)
toPosition = the position in which the second item was used (array)


Each of the variables are unique values set in the server. This is how the server knows which thing it is calling.


So lets say Bob used the item: AOL and the AOL was on position:
{x = 500, y = 577, z = 7}

The script will only call the veriables needed which are:
(cid, item, fromPosition)

cid = bob's creature id
item = the aol bob used (and not any others)
fromPosition = {x = 500, y = 577, z = 7}


If Bob used an AOL and its in his backpack then the script will get Bob's position because it is on him.

So, fromPosition will be bob's position


Now, if bob used an AOL on a sword. The script would call the veriables needed:
cid = bob's creature id
item = AOL
fromPosition = AOL's position
itemEx = sword
toPosition = sword's position


 
Last edited:
VERIABLES CONTINUED

When the script calls its
variables it isn't anything specific other then telling itself exactly which things its calling out of the server.

So, it knows AOL is the AOL used and not any other AOL. However, it doesn't know that its an AOL.

Same as it knows Bob has a cid and it knows bobs cid, but it doesn't know bob is bob.

If that makes since. This is where general functions come in.

So lets say when bob uses an aol on a sword and we want it to tell him he cannot do that.

The script will begin:
Code:
function onUse(cid, item, fromPosition, itemEx, toPosition)

Now we need to tell the script how to use each variable with general functions

To do this we will start with the function:
Code:
isPlayer(cid)

This is so we can tell the script that cid MUST be a player to continue.

So it will look like this:

Code:
function onUse(cid, item, fromPosition, itemEx, toPosition)
if isPlayer(cid) then

Now it will make sure Bob is a player. If we want only bob to be able to use an AOL on a sword we can use this function:

Code:
getCreatureName(cid)

It will look like this:

Code:
function onUse(cid, item, fromPosition, itemEx, toPosition)
if isPlayer(cid) then
        if getCreatureName(cid) == "Bob" then

Now if cid's name is not equal to Bob then it won't continue.

If you try to type this:

Code:
if cid == "Bob"

It will not work because cid is a number value specific to bob. The script will read:
Code:
if 43315124 == "Bob" then

As you can see 43315124 is not equal to Bob.

This is why you must use general function.

If you type:
Code:
if item == 1445 then

It will also read it wrong. It will read:

Code:
if 411312 == 1445 then

As you can see 411312 is not equal to 1445.

However, if you type:
Code:
if item.itemid == 1445 then

If the items itemid is 1445 then it will be true and the script will continue.
 
Last edited:
GENERAL FUNCTIONS:

Code:
    //get*
    getCreatureHealth(cid)
    getCreatureMaxHealth(cid)
    getCreatureHideHealth(cid)
    getCreatureMana(cid)
    getCreatureMaxMana(cid)
    getCreatureSpeakType(cid)
    getCreatureMaster(cid)
    getCreatureSummons(cid)
    getCreatureOutfit(cid)
    getCreaturePosition(cid)
    getCreatureLookDirection(cid)
    getCreatureName(cid)
    getCreatureSpeed(cid) //TODO
    getCreatureBaseSpeed(cid) //TODO
    getCreatureTarget(cid) //TODO
    getCreatureByName(name)
    getCreatureSkullType(cid)
    getCreatureCondition(cid, condition[, subId]) //TODO
    getCreatureNoMove(cid) //TODO
    getMonsterInfo(name)
    getMonsterHealingSpells(name) //TODO
    getMonsterAttackSpells(name) //TODO
    getMonsterLootList(name) //TODO
    getMonsterSummonList(name) //TODO
    getMonsterTargetList(cid) //TODO
    getMonsterFriendList(cid) //TODO
    getPlayerByNameWildcard(name~[, ret = false]) //TODO
    getPlayerLossSkill(cid) //TODO
    getPlayerLossPercent(cid, lossType) //TODO
    getPlayerGUIDByName(name[, multiworld = false]) //TODO
    getPlayerNameByGUID(guid[, multiworld = false[, displayError = true]]) //TODO
    getPlayerFood(cid)
    getPlayerLevel(cid)
    getPlayerExperience(cid)
    getPlayerMagLevel(cid[, ignoreBuffs = false]) //TODO
    getPlayerSpentMana(cid) //TODO
    getPlayerAccess(cid)
    getPlayerGhostAccess(cid)
    getPlayerSkillLevel(cid, skillId)
    getPlayerSkillTries(cid, skillId) //TODO
    getPlayerTown(cid)
    getPlayerVocation(cid)
    getPlayerRequiredMana(cid, magicLevel) //TODO
    getPlayerRequiredSkillTries(cid, skillId, skillLevel) //TODO
    getPlayerItemCount(cid, itemid[, subType = -1])
    getPlayerSoul(cid)
    getPlayerAccountId(cid) //TODO
    getPlayerAccount(cid) //TODO
    getPlayerIp(cid) //TODO
    getPlayerFreeCap(cid)
    getPlayerLight(cid)
    getPlayerSlotItem(cid, slot)
    getPlayerWeapon(cid[, ignoreAmmo = false]) //TODO
    getPlayerItemById(cid, deepSearch, itemId[, subType = -1]) //TODO
    getPlayerDepotItems(cid, depotid)
    getPlayerGuildId(cid)
    getPlayerGuildName(cid)
    getPlayerGuildRank(cid)
    getPlayerGuildNick(cid)
    getPlayerGuildLevel(cid) //TODO: From here, all bottoms
    getPlayerSex(cid)
    getPlayerStorageValue(uid, key)
    getPlayerGUID(cid)
    getPlayerFlagValue(cid, flag)
    getPlayerCustomFlagValue(cid, flag)
    getPlayerPromotionLevel(cid)
    getPlayerGroupId(cid)
    getPlayerLearnedInstantSpell(cid, name)
    getPlayerInstantSpellCount(cid)
    getPlayerInstantSpellInfo(cid, index)
    getPlayerBlessing(cid, blessing)
    getPlayerStamina(cid)
    getPlayerExtraExpRate(cid)
    getPlayerPartner(cid)
    getPlayerParty(cid)
    getPlayerPremiumDays(cid)
    getPlayerBalance(cid)
    getPlayerMoney(cid)
    getPlayerSkullTicks(cid, type)
    getPlayerRates(cid)
    getPlayerLastLogin(cid)
    getPlayerLastLoginSaved(cid)
    getPlayerAccountManager(cid)
    getInstantSpellInfo(cid, name)
    getPlayersByAccountId(accountNumber)
    getPlayersByIp(ip[, mask = 0xFFFFFFFF])
    getChannelUsers(channelId)
    getPlayersOnline()
    getPartyMembers(lid)
    getAccountIdByName(name)
    getAccountByName(name)
    getAccountIdByAccount(accName)
    getAccountByAccountId(accId)
    getIpByName(name)
    getItemRWInfo(uid)
    getItemProtection(uid)
    getItemDescriptionsById(itemid)
    getItemWeightById(itemid, count[, precise])
    getItemDescriptions(uid)
    getItemWeight(uid[, precise])
    getItemAttack(uid)
    getItemExtraAttack(uid)
    getItemDefense(uid)
    getItemExtraDefense(uid)
    getItemArmor(uid)
    getItemAttackSpeed(uid)
    getItemHitChance(uid)
    getItemShootRange(uid)
    getItemIdByName(name[, displayError = true])
    getItemLevelDoor(itemid)
    getItemWeaponType(uid)
    getFluidSourceType(type)
    getContainerSize(uid)
    getContainerCap(uid)
    getContainerCapById(itemid)
    getContainerItem(uid, slot)
    getDepotId(uid)
    getTileItemById(pos, itemId[, subType = -1])
    getTileItemByType(pos, type)
    getTileThingByPos(pos)
    getTileInfo(pos)
    getTopCreature(pos)
    getClosestFreeTile(cid, targetpos[, extended = false[, ignoreHouse = true]])
    getThingFromPos(pos)
    getThing(uid)
    getThingPos(uid)
    getHouseInfo(id)
    getHouseAccessList(houseid, listid)
    getHouseByPlayerGUID(playerGUID)
    getHouseFromPos(pos)
    getTownId(townName)
    getTownName(townId)
    getTownTemplePosition(townId)
    getTownHouses(townId)
    getWorldType()
    getWorldTime()
    getWorldLight()
    getWorldCreatures(type) //0 players, 1 monsters, 2 npcs, 3 all
    getWorldUpTime()
    getHighscoreString(skillId)
    getVocationInfo(id)
    getGuildId(guildName)
    getGuildMotd(guildId)
    getSpectators(centerPos, rangex, rangey[, multifloor = false])
    getSearchString(fromPosition, toPosition[, fromIsCreature = false[, toIsCreature = false]])
    getWaypointPosition(name)
    getGameState()
    getNotationsCount(accId)
    getBanData(value)
    getBanList(type[, value])
    getBanReason(id)
    getBanAction(id[, ipBanishment])
    getGlobalStorageValue(valueid)
    getExperienceStage(level)
    getConfigFile()
    getConfigValue(key)
    getModList()
    loadmodlib(libName)
    domodlib(libName)
    getLogsDir()
    getDataDir()
    getWaypointList()
    getTalkActionList()
    getExperienceStageList()
   
   

    //set*
    setCreatureMaxHealth(cid, health)
    setCreatureMaxMana(cid, mana)
    setHouseOwner(houseid, ownerGUID)
    setHouseAccessList(houseid, listid, listtext)
    setItemName(uid)
    setItemPluralName(uid)
    setItemArticle(uid)
    setItemAttack(uid, attack)
    setItemExtraAttack(uid, extraattack)
    setItemDefense(uid, defense)
    setItemArmor(uid, armor)
    setItemExtraDefense(uid, extradefense)
    setItemAttackSpeed(uid, attackspeed)
    setItemHitChance(uid, hitChance)
    setItemShootRange(uid, shootRange)
    setCombatArea(combat, area)
    setCombatCondition(combat, condition)
    setCombatParam(combat, key, value)
    setConditionParam(condition, key, value)
    setCombatCallBack(combat, key, function_name)
    setCombatFormula(combat, type, mina, minb, maxa, maxb)
    setConditionFormula(combat, mina, minb, maxa, maxb)
    setGlobalStorageValue(key, newValue)
    setWorldType(type)
 
Last edited:
Code:
    //do*
    doCreatureAddHealth(cid, health[, force])
    doCreatureAddMana(cid, mana)
    doCreatureSetDropLoot(cid, doDrop)
    doCreatureSetSkullType(cid, skull)
    doCreatureSetSpeakType
    doCreatureSetLookDirection(cid, dir)
    doPlayerSetMaxCapacity(cid, cap)
    doCreatureChangeOutfit(cid, outfit)
    doCreatureSay(uid, text, type[, ghost = false[, cid = 0[, pos]]])
    doCreatureSetNoMove(cid, cannotMove)
    doSetCreatureLight(cid, lightLevel, lightColor, time)
    doSetCreatureOutfit(cid, outfit, time)
    doRemoveCreature(cid[, executeLogout = true])
    doMoveCreature(cid, direction)
    doConvinceCreature(cid, target)
    doChallengeCreature(cid, target)
    doChangeSpeed(cid, delta)
    doSummonMonster(name, pos)
    doCreateMonster(name, pos)
    doMonsterChangeTarget(cid)
    doMonsterSetTarget(cid, target)
    doCreateNpc(name, pos)
    doSetMonsterOutfit(cid, name, time)
    doPlayerBroadcastMessage(cid, message[, type])
    doPlayerSetSex(cid, newSex)
    doPlayerSetTown(cid, townid)
    doPlayerSetVocation(cid, voc)
    doPlayerSetStorageValue(uid, key, newValue)
    doPlayerSetGroupId(cid, newGroupId)
    doPlayerSetPromotionLevel(cid, level)
    doPlayerSetStamina(cid, minutes)
    doPlayerSetBalance(cid, balance)
    doPlayerSetExtraExpRate(cid, value)
    doPlayerSetPartner(cid, guid)
    doPlayerRemoveItem(cid, itemid, count[, subtype])
    doPlayerAddExperience(cid, amount)
    doPlayerSetGuildId(cid, id)
    doPlayerSetGuildRank(cid, rank)
    doPlayerSetGuildNick(cid, nick)
    doPlayerAddOutfit(cid,looktype, addons)
    doPlayerRemoveOutfit(cid,looktype, addons)
    doPlayerSetRedSkullTicks(cid, amount)
    doPlayerSetLossPercent(cid, lossType, newPercent)
    doPlayerSetLossSkill(cid, doLose)
    doPlayerAddSkillTry(cid, skillid, n)
    doPlayerAddSpentMana(cid, amount)
    doPlayerAddSoul(cid, soul)
    doPlayerAddItem(uid, itemid[, count/subtype[, canDropOnMap = true]])
    doPlayerAddItemEx(cid, uid[, canDropOnMap = false])
    doPlayerSendTextMessage(cid, MessageClasses, message)
    doPlayerSendChannelMessage(cid, author, message, SpeakClasses, channel)
    doPlayerSendToChannel(cid, targetId, SpeakClasses, message, channel[, time])
    doPlayerAddMoney(cid, money)
    doPlayerRemoveMoney(cid, money)
    doPlayerTransferMoneyTo(cid, target, money)
    doPlayerPopupFYI(cid, message)
    doPlayerSendTutorial(cid, id)
    doPlayerAddMapMark(cid, pos, type[, description])
    doPlayerAddPremiumDays(cid, days)
    doPlayerAddBlessing(cid, blessing)
    doPlayerAddStamina(cid, minutes)
    doPlayerResetIdleTime(cid)
    doPlayerLearnInstantSpell(cid, name)
    doPlayerUnlearnInstantSpell(cid, name)
    doPlayerFeed(cid, food)
    doPlayerSendCancel(cid, text)
    doPlayerSendDefaultCancel(cid, ReturnValue)
    doPlayerSetRate(cid, type, value)
    doPlayerJoinParty(cid, lid)
    doPlayerSendOutfitWindow(cid)
    doPlayerSave(cid[, shallow = false])
    doCreateItem(itemid, type/count, pos)
    doCreateItemEx(itemid[, count/subtype])
    doAddContainerItemEx(uid, virtuid)
    doAddContainerItem(uid, itemid[, count/subtype])
    doChangeTypeItem(uid, newtype)
    doDecayItem(uid)
    doRemoveItem(uid[, count])
    doTransformItem(uid, toitemid[, count/subtype])
    doSetItemActionId(uid, actionid)
    doSetItemText(uid, text[, writer[, date]])
    doSetItemSpecialDescription(uid, desc)
    doSetItemOutfit(cid, item, time)
    doSetItemProtection(uid, value)
    doTileAddItemEx(pos, uid)
    doTileQueryAdd(uid, pos[, flags])
    doAddCondition(cid, condition)
    doRemoveCondition(cid, type[, subId])
    doRemoveConditions(cid[, onlyPersistent])
    doAreaCombatHealth(cid, type, pos, area, min, max, effect)
    doTargetCombatHealth(cid, target, type, min, max, effect)
    doAreaCombatMana(cid, pos, area, min, max, effect)
    doTargetCombatMana(cid, target, min, max, effect)
    doAreaCombatCondition(cid, pos, area, condition, effect)
    doTargetCombatCondition(cid, target, condition, effect)
    doAreaCombatDispel(cid, pos, area, type, effect)
    doTargetCombatDispel(cid, target, type, effect)
    doCombat(cid, combat, param)
    doTeleportThing(cid, newpos[, pushmove = true])
    doCreateTeleport(itemid, topos, createpos)
    doSendMagicEffect(pos, type[, player])
    doSendDistanceShoot(frompos, topos, type[, player])
    doSendAnimatedText(pos, text, color[, player])
    doShowTextDialog(cid, itemid, text)
    doRelocate(pos, toPos[, creatures = true])
    doBroadcastMessage(message, type)
    doWaypointAddTemporial(name, pos)
    doSetGameState(stateId)
    doAddIpBanishment(ip[, length[, comment[, admin]]])
    doAddNamelock(name[, reason[, action[, comment[, admin]]]])
    doAddBanishment(accId[, length[, reason[, action[, comment[, admin]]]]])
    doAddDeletion(accId[, reason[, action[, comment[, admin]]]]])
    doAddNotation(accId[, reason[, action[, comment[, admin]]]]])
    doRemoveIpBanishment(ip[, mask])
    doRemoveNamelock(name)
    doRemoveBanisment(accId)
    doRemoveDeletion(accId)
    doRemoveNotations(accId)
    doSaveServer()
    doReloadInfo(id[, cid])
    doCleanHouse(houseId)
    doCleanMap()
    doRefreshMap()

    //is*
    isCreature(cid)
    isMonster(uid)
    isNpc(uid)
    isPlayer(cid)
    isPlayerPzLocked(cid)
    isItemStackable(itemid)
    isItemRune(itemid)
    isItemMovable(itemid)
    isItemDoor(itemid)
    isItemContainer(itemid)
    isItemFluidContainer(itemid)
    isContainer(uid)
    isCorpse(uid)
    isMovable(uid)
    isSightClear(fromPos, toPos, floorCheck)
    isIpBanished(ip[, mask])
    isPlayerNamelocked(name)
    isAccountBanished(accId)
    isAccountDeleted(accId)
    isInArray({array}, value[, lower = true])
 
Last edited:
Code:
    //others
    registerCreatureEvent(uid, eventName)
    createCombatArea({area}[, {exArea}])
    createConditionObject(type[, ticks[, buff[, subId]]])
    addDamageCondition(condition, rounds, time, value)
    addOutfitCondition(condition, lookTypeEx, lookType, lookHead, lookBody, lookLegs, lookFeet)
    createCombatObject()
    numberToVariant(number)
    stringToVariant(string)
    positionToVariant(pos)
    targetPositionToVariant(pos)
    variantToNumber(var)
    variantToString(var)
    variantToPosition(var)
    canPlayerWearOutfit(cid, lookType, addons)
    executeRaid(name)
    addEvent(callback, delay, ...)
    stopEvent(eventid)
    hasProperty(uid)

    md5(str)
    sha1(str)

    //db table
    db.executeQuery(query)
    db.storeQuery(query)
    db.escapeString(str)
    db.escapeBlob(s, length)
    db.stringComparisonOperator()
    db.lastInsertId()

    //result table
    result.getDataInt(resId, s)
    result.getDataLong(resId, s)
    result.getDataString(resId, s)
    result.getDataStream(resId, s, length)
    result.next(resId)
    result.free(resId)
   

    //bit table
    #bit.cast
    bit.bnot(n)
    bit.band(type, n)
    bit.bor(type, n)
    bit.bxor(type, n)
    bit.lshift(type, n)
    bit.rshift(type, n)
    #bit.arshift
    #bit.ucast
    bit.ubnot(n)
    bit.uband(type, n)
    bit.ubor(type, n)
    bit.ubxor(type, n)
    bit.ulshift(type, n)
    bit.urshift(type, n)
    #bit.uarshift
 
Last edited:
onUse, onSay, onStepIn etc.. are known as an Interface, not a main function.

Here is the google definition of an Interface
Code:
In Object Oriented Programming, an Interface is a description of all functions that an object must have in
order to be an "X". Again, as an example, anything that "ACTS LIKE" a light,
should have a turn_on() method and a turn_off() method.

So basically what this means when applied to tfs servers,
All Action scripts must have an onUse method to be considered an Action script
All Spell scripts must have an onCastSpell / onCast method to be considered a Spell script
All Movement scripts when dealing with player / creature / npc walking on tiles must atleast have an onStepIn or onStepOut method or both to be considered a Movement script.
Etc..

This might seem trivial but when exploring other languages like Java or C++ you can refer back to this lesson and say to yourself.. ahh that is what an Interface is used for :)
 
Last edited:
MAIN FUNCTION VERIABLES:

The veriables are what you see in the parenthesis:
Code:
(cid, words, param, channe)
Code:
(cid, item, fromPosition, itemEx, toPosition)
These are not known as a main function variables, they are known as parameters of the method, they are place holders, telling you the programmer what type of information to pass to the method and must be passed the data in the order specified.


Meaning if the order is cid, item, frompos, itemEx, topos
Then the data you pass to the method should be a value that holds the types needed in the specific order.
They can also be named anything such as c, i, f, x, t this is because their scope ( their life existence ) is local to the method and can't be referenced outside of the method, unless passed to whatever may need it

For instance if we were to use an onUse method
Code:
function onUse(cid, item)
    -- this would generate an error, it will tell you creature not found, because we didn't pass cid to getPlayerHp()
    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "This is my current hp "..getPlayerHp())[/COLOR]
end

function getPlayerHp()
    return getCreatureHealth(cid)
end

A way to remedy this is to create a local function inside of the interface
Code:
function onUse(cid, item)
    local function getPlayerHp()
        return getCreatureHealth(cid)
    end
    -- this won't return an error because cid although it is local to onUse it is global to getPlayerHp()
    -- so we don't need to pass the value to getPlayerHp()
    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "This is my current hp "..getPlayerHp())
end
I think it's great your trying to create a tutorial for people to learn about the tfs framework but you need to get into the meat and potatoes of how it works while using the proper syntax in order for it to be an effective learning tool.


Well best of luck :)
 
Back
Top