• 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 Calling variable from other script

Materion

New Member
Joined
Feb 18, 2012
Messages
81
Reaction score
2
Hello, there is a way to call a variable from other script into one im working on ? For example i have variable that holds on which lvl my character have spell for example fire_bolt_lvl= 1. And i have npc that teach my character new levels of that spell so i have to call that fire_bolt_lvl variable in npc script to fire_bolt_lvl += 1 when character have enough cash. But my question is how to do that, how can i let other script change variable in another ? Or at least make variable get value of other variable from other script.
 
Hmm, I will try it :). It would be nice to do that, cause i would not have to make 5 scripts to make 5 lvls of that fire bolt spell and then use doPlayerLearnInstantSpell and doPlayerUnlearnInstantSpell every time a character learns next level of it ;d. And i would use it in many other scripts when interaction between some events have to occur.
 
Thanks ! You know i started to read lua functions again and i get to same conclusion i can use that storage functions, but what when i have to use some more complicated data structures like tables ? Or when i have to check OTHER player storage value ? I know i can write tables with for loop into some next storagevalues but tables are dynamical so i should left some space in storage values for next data if table is going to be bigger or delete data when table gets smaller.
 
I believe Tarjei found a work-around for that type of problem... I'll search up his thread, give me a minute.

*EDIT*: Tarjei's method to save arrays in a single storage value

As for your concern about getting a different player's storage value, you simply find a way to get his cid, and then you use that cid in conjuction with getPlayerStorageValue(cid, key)

A great example would be talkactions:
Lua:
function onSay(cid, words, param)
	if param ~= '' then
	    local key, pid = 12345, getPlayerByNameWildcard(param)
        local otherplayersstoragevalue = getPlayerStorageValue(pid, key)
		doCreatureSay(cid, "The key "..key.."s value on player "..getCreatureName(pid).." equals "..otherplayersstoragevalue..".", TALKTYPE_MONSTER)
	else
	    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Wrong specified param(s).")
		doSendMagicEffect(getThingPos(cid), CONST_ME_POFF)
	end
    return true
end
 
Last edited:
Just use a storage value which holds the players spell level.
-1 = not learned. If he learns the spell gets eet to 1 and then each time increase it by one.

You can just base the spell damage on that storage by using callbacks.
 
I have another question - can I use function onEquip(cid, item, slot) and function onDeEquip(cid, item, slot) inside action scripts ? I mean i wanna make action script for a bow when someone equip it it will change player storage value to 1 and when he deequip it that storage value will change to 0. I'm trying to do it, but it don't work.

Lua:
function onEquip(cid, item, slot)
	if(item.itemid == 2456) then
		setPlayerStorageValue(cid,9001,1)
	end
	return true
end

function onDeEquip(cid, item, slot)
	if(item.itemid == 2456) then
		setPlayerStorageValue(cid,9001,0)
	end
	return true
end

And spell script:
Lua:
function onCastSpell(cid, var)
	local czy_to_luk = getPlayerStorageValue(cid, 9001)
	if czy_to_luk == 1 then
		doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_RED, "IT WORKS !!")
	end
	return true
end
 
Just check if the player has this item in left or right hand in the script.
I don't know why the movement does not work, maybe wrong registration in movements.xml
 
I did it not in movement folder i put that script in action folder. Okay so if i put it in movement folder and register it in movements.xml what slot i have to write ? I only see ring, feet etc. I dont see any slot like weapon or right hand or left hand.
<movevent type="DeEquip" itemid="8904" slot="shield" event="function" value="onDeEquipItem"/> for example.

script look like this:
Lua:
function onEquip(cid, item, slot, uid)
	local skill_level = getPlayerStorageValue(cid, 9000)
	if item.itemid == 2456 then
		if skill_level == 1 then
			setItemShootRange(item.itemid, 1)
		elseif skill_level == 2 then
			setItemShootRange(item.itemid, 1)
		elseif skill_level == 3 then
			setItemShootRange(item.itemid, 1)
		elseif skill_level == 4 then
			setItemShootRange(item.itemid, 1)
		end
	end
	return true
end
 
Last edited:
You have to register Equip and DeEquip.
slot can be "hand" or "shield", I guess event should be "script" and value the file name.
 
Ok my script looks now like this:
Lua:
function onEquip(cid, item, slot, uid)
	local skill_level = getPlayerStorageValue(cid, 9000)
	local original_item_range = getItemShootRange(item.itemid)
	setPlayerStorageValue(cid,9001,original_item_range)
	if item.itemid == 2456 then
		if skill_level == 1 then
			setItemShootRange(item.itemid, 4)
		elseif skill_level == 2 then
			setItemShootRange(item.itemid, 5)
		elseif skill_level == 3 then
			setItemShootRange(item.itemid, 6)
		elseif skill_level == 4 then
			setItemShootRange(item.itemid, 7)
		end
	end
	return true
end

And another one for deequip:
Lua:
function onDeEquip(cid, item, slot)
        local original_item_range = getPlayerStorageValue(cid, 9001)
        if item.itemid == 2456 then
                setItemShootRange(item.itemid, original_item_range)
        end
        return true
end

But when i try to equip a bow i got an error message in server console :
mJkqByV.png


Why I got this error, what am I doing wrong ? I can't use getItemShootRange(uid) inside movements scripts ?
 
Last edited:
Ok i got how to do it ! I have another question - I wanna make spell that assasin class become completly invisible - like GM. There is function /ghost for gm to make him invisible, there is a way to write script that will use it somehow ?
 
Last edited:
Normally you can't do what you first asked: You can load a variable from another file, but tfs doesn't let you change its value that would be the same for the original file and where you are loading it. I've even tried with _G in the past with no results. The only way I saw it possible is adding some lua environment stuff
 
OK i made it work !! Omg I'm so happy ! But i still have doubts how to make some player fully invisible - like GM can enter ghost mode in tfs - let other players enter it by using some spell. I can make my own access like some character class have his own access for example access = 7?
 
Last edited:
Back
Top