Targetting help

Share your scripts and packages with other Mudlet users.
Duugan
Posts: 25
Joined: Fri Aug 26, 2016 4:00 am

Re: Targetting help

Post by Duugan »

Thanks Chrio. I tested it out this morning and it was working like a charm. I even made one for HP (\d+)\(/d+) MP to report my mana. Then I was feeling ambitious and used said mana variables in a practice trigger that uses offensive spells only if I'm over 50% mana.
Then I made an alias that converts ^c (w+) (w+) into spellcast variable so when I manually type it will trigger my fizzle and recast if necessary.
I know I only know a fraction of this stuff, but tinkering with it this far has been really fun.

I find myself staring at my trigger page trying to think of new things to code...

I did have a question though. What is the difference between variable and local variable? If I was running 2 characters and had triggers on both, would they mess up if it wasn't local? Is it some other distinction or irrelevant ?
Last edited by Duugan on Fri Sep 09, 2016 1:05 am, edited 1 time in total.

chrio
Posts: 73
Joined: Mon Aug 22, 2016 11:34 am

Re: Targetting help

Post by chrio »

Yeah, addictive, isn't it? :)

As for local/global, I am not 100% on the details, but I'll try and give an intro and hope that someone jumps in and correct me if i'm wrong :)

Global variables can be accessed and changed from everywhere in that profile. I haven't tested, but I think I have read that you cannot share data between different profiles using variables though.

Local variables, when declared within a function, can not be accessed outside of it, and once the function have ended, the variables are gone, so the function doesn't remember them.

Local variables when declared in a script, but outside of a function, are accessable to the functions within the same script, and retain their values for the session. They are not accessible from outside of that script.

So, if you have this:
Code: [show] | [select all] lua
local v = v or 0   -- if v isn't set, then set it to 0, else we set it to its own value.
function f()
	echo(v .. "\n")
	v = v + 1
end
Then the first call to f() will echo 0, second time 1, third 2 and so on...

But if we do this:
Code: [show] | [select all] lua
function f()
	local v = v or 0
	echo(v .. "\n")
	v = v + 1
end
Then f() will always echo 0, since after f() has returned, v is gone, so the next run it's nil again and thus gets assigned to 0.


Also, you can assign a local variable with the same name as a global variable, so if we have a global variable "myvalue = 42" and have this in a script:
Code: [show] | [select all] lua
function f()
	local myvalue = myvalue -- copy global to our local with same name
	myvalue = myvalue + 1
	echo(myvalue .. "\n")
end
Then it will increase the local variable from 42 to 43, and echo 43, but the global stays the same. So each time we run f() it echos 43 (or one higher than whatever myvalue is when we run it).
edit: This will work with variables, but not with tables. If you assign a local table from a global table, you will not get a copy of the table, but a new reference name to the original (it's just a different name for the table).


Phew, this post ended up a lot longer than I intended, but it's easier to explain with examples. :)
Last edited by chrio on Sat Sep 10, 2016 3:47 pm, edited 1 time in total.

Duugan
Posts: 25
Joined: Fri Aug 26, 2016 4:00 am

Re: Targetting help

Post by Duugan »

Ok Chrio. Thanks again. Those examples did in fact help. I had set up a trigger to eat/drink when I'm hungry/thirsty and it was bugging me that when sleeping, it would trigger and try to run the script when I wasn't awake.
I made 2 more triggers for sleep and awake and set variables accordingly. Then I amended my eat drink script to check if I was sleeping first. If I was,it would see Hungry and Thirsty to true, respectively. I then had my awake trigger check if I was hungry or thirsty and act accordingly.

It wasn't working right and through trial and error I got it working by taking the local tags off. I knew it worked, but wasn't sure why...

If I haven't bugged you enough with all these questions, could you explain how to script something to run or stop another script? For example, if I had am oh crap trigger to heal at low health and I wanted it to turn off other triggers, heal and then restart the others?

chrio
Posts: 73
Joined: Mon Aug 22, 2016 11:34 am

Re: Targetting help

Post by chrio »

Duugan wrote:If I haven't bugged you enough with all these questions, could you explain how to script something to run or stop another script? For example, if I had am oh crap trigger to heal at low health and I wanted it to turn off other triggers, heal and then restart the others?
You pretty much answered this one yourself. Check out enableTrigger/disableTrigger here

User avatar
SlySven
Posts: 1019
Joined: Mon Mar 04, 2013 3:40 pm
Location: Deepest Wiltshire, UK
Discord: SlySven#2703

Re: Targetting help

Post by SlySven »

If I recall correctly for
"Local variables when declared in a script, but outside of a function, are accessible to the functions within the same script, and retain their values for the session. They are not accessible from outside of that script."
they are also accessible to children of that script (or alias, timer, key, ... etc.)

chrio
Posts: 73
Joined: Mon Aug 22, 2016 11:34 am

Re: Targetting help

Post by chrio »

SlySven wrote:they are also accessible to children of that script (or alias, timer, key, ... etc.)
What do you mean by children? In the tree structure?

chrio
Posts: 73
Joined: Mon Aug 22, 2016 11:34 am

Re: Targetting help

Post by chrio »

I noticed that if you have a table and try to make a copy, you just end up with a new reference to that table so that any changes to one also changes the other. Mudlet includes a function ( table.deepcopy ) for when you need a copy of the table to work on. I edited my post above.

This function from stackoverflow and lua-users wiki also discuss this problem.

User avatar
SlySven
Posts: 1019
Joined: Mon Mar 04, 2013 3:40 pm
Location: Deepest Wiltshire, UK
Discord: SlySven#2703

Re: Targetting help

Post by SlySven »

chrio wrote:
SlySven wrote: they are also accessible to children of that script (or alias, timer, key, ... etc.)
What do you mean by children? In the tree structure?
Yep.

Referring back a couple of posts - each "Profile" has it's own Global "Lua State" as it is known - they act independently of and without knowledge of each other. So sharing information means careful work if you are going to use files (or on non-Doze platforms you may well want to check out "pipes")...

In the long term I do have a proposal to build in an intra-profile "interlink" system event that will pass an event raised with an (initially a single, but it may be possible to include more) argument that is sent to all the active profiles in a fixed sequence such that the sender also gets the event but only after all the others have received it (so it knows that the others have seen it by the time it gets it back).

Post Reply