Creating temporary variables within aliases/triggers?

Post Reply
Bigglesbee
Posts: 19
Joined: Wed Aug 10, 2022 4:13 pm

Creating temporary variables within aliases/triggers?

Post by Bigglesbee »

Hey guys, can't figure this one out though I'm sure it's basic stuff.

I want to switch a character's gear with an alias based on the current state of the gear setup. The best/easiest way I can think of doing this (with my non-coder skills), is have a permanent variable that gets checked to see what the current gear state is, and then checked to see what it should change to. But: I'm trying to figure out how to do it without having a permanent variable set up in the character's profile, mainly because adding extra permanent variables to every character starts getting tedious since they each have their own profile.

So far, I have:

Code: Select all

if switch == "normal" then sendAll("all the commands to switch gear")
my_weapon = "new weapon state"
switch = "new gear state"
elseif switch == "new gear state" then sendAll("all the commands to switch gear back to normal state")
my_weapon = "normal weapon"
switch = "normal"
end
I need to have that "switch" variable set up and checkmarked in Mudlet for the above to work. The my_weapon variable is just for separate disarm triggers. Is there a way to have the above create a temporary "switch" variable that persists until I close the character profile? Or is there just a better way to go about this kind of thing? Thanks!

Jor'Mox
Posts: 1123
Joined: Wed Apr 03, 2013 2:19 am

Re: Creating temporary variables within aliases/triggers?

Post by Jor'Mox »

To make the variable persistent, but ONLY while the profile is open, you can make a blank script, make sure that script is checkmarked, and create the variable inside that script. Then it will always start with whatever value you give it in the script, without having a permanent variable created for the character.

Also, since this switch variable has only two states, you could instead use a boolean value for it, which is much easier to work with. That would look like this:

Code: Select all

// in the separate script that creates the variable when the profile loads up
switch = false

// in your alias
if not switch then
	sendAll("all the commands to switch gear")
	my_weapon = "new weapon state"
	switch = true
else
	sendAll("all the commands to switch gear back to normal state")
	my_weapon = "normal weapon"
	switch = false
end

Bigglesbee
Posts: 19
Joined: Wed Aug 10, 2022 4:13 pm

Re: Creating temporary variables within aliases/triggers?

Post by Bigglesbee »

Hey thanks for the reply! I have a couple questions if you don't mind:
  • What would be the difference between creating a checkmarked variable "switch" in a single character's profile vs. doing it as a script in that character's profile?
  • Is there a way to make everything in one script or alias, so when I change it up (or copy it over to another character profile), I can find it all in one spot rather than trying to remember to edit/copy over both the script as well as the alias?
Thanks for the help!

Jor'Mox
Posts: 1123
Joined: Wed Apr 03, 2013 2:19 am

Re: Creating temporary variables within aliases/triggers?

Post by Jor'Mox »

The main difference between setting the variable in the script vs having it as a "variable" object is the value permanence between sessions. A variable object will remember its value even when you close Mudlet and reopen it, whereas using a variable declared in a script will instead always have its value set to the same thing each time Mudlet is launched, or that piece of code is run. Also, if you have a lot of them, there is a bit of a convenience to consider by having them in a script, rather than in manually created variable objects, especially if you want to share the same setup between multiple character profiles, since you can just copy/paste a script to between them all in seconds.

As for consolidating things into one place to make it easier to keep track of as changes are made, there are several ways to do this. The first is to use functions created in the script to do all the work for aliases and triggers, so all their code becomes centralized. Like this:

Code: Select all

function switch_gear()
	if not switch then
		sendAll("all the commands to switch gear")
		my_weapon = "new weapon state"
		switch = true
	else
		sendAll("all the commands to switch gear back to normal state")
		my_weapon = "normal weapon"
		switch = false
	end
end
Then your alias just has this code in it: switch_gear()

You could further consolidate things by making the script itself create the alias in the first place, like so:

Code: Select all

if exists("Switch Gear Alias","alias") == 0 then
	permAlias("Switch Gear Alias", "Scripted Aliases", "^switch gear$", [[switch_gear()]])
end

Bigglesbee
Posts: 19
Joined: Wed Aug 10, 2022 4:13 pm

Re: Creating temporary variables within aliases/triggers?

Post by Bigglesbee »

Thanks for the help, I think I sort of get it, a couple questions though:
  • What's the "Scripted Aliases" part of the create alias script, a folder?
  • If I did it the way you described, the function in a script would create the alias, but then the alias I type would be in the separate Alias tab I think? Is there a way to have the alias "be" in the script itself?
  • Same question as the above for the switch variable, too, though I'm starting to think these things have to be in their respective tabs.

Jor'Mox
Posts: 1123
Joined: Wed Apr 03, 2013 2:19 am

Re: Creating temporary variables within aliases/triggers?

Post by Jor'Mox »

The "Scripted Aliases" bit is indeed a folder, with the idea being to make it easier to keep them together in case you needed to delete them or something. It would be created as an alias, just as if you had created it by hand. You can, optionally, create temporary aliases, that are created when the script is run, and go away when the profile is closed, so they don't create anything persistent in the profile, if that is what you would prefer to do. The variable though is just creating a global variable in the scripting language (LUA), no different than any other global variable. That said, since it would only be used inside the script, there is no reason not to use a local variable instead, which you can do by just putting local before it in the very first line when it is created (i.e. local switch = false).

Post Reply