Announcement: Why not try our official idea generator for inspiration when making puzzles?


Caravel Forum : DROD Boards : Architecture : A (More) Comprehensive MyScript Primer and Reference
New Topic New Poll Post Reply
Poster Message
Xindaris
Level: Smitemaster
Avatar
Rank Points: 1588
Registered: 06-13-2015
IP: Logged

File: Orientations to Numbers.PNG (2.3 KB)
Downloaded 1181 times.
License: Public Domain
icon A (More) Comprehensive MyScript Primer and Reference (+10)  
1. What is MyScript?

Often when designing a custom element, scripted behavior, or even just in trying to check a challenge, one finds oneself wishing they could point to a location or rectangle of locations relative to some object whose own location may change during gameplay. The normal behavior of a Build or Generate Entity command, for example, or the Wait for Entity/Item At commands, only allow one to specify a single static tile or group of tiles, and do not allow for this kind of dynamic selection of a location. Fortunately, there is still a way to do it. This is exactly what the _MyScript variables are for! And they can do even more than this, generating or building or checking for some undocumented or dynamically-selected items or entities, just for starters. But _MyScripting is a slightly arcane art in DROD scripting, requiring a great deal of care not to make your element break within a couple of turns, or crash the game, or other horrible things.

2. How do I use MyScript?

The basic format of a code involving MyScript injection is as follows:
  Set var "_MyScript*" = 0
  (Whatever command uses the _Myscript override)
  Set var "_MyScript*" = -9999
Here "*" can be X, Y, W, H, or F. For location-based commands, X and Y specify the X and Y locations for the command to point at. If you want to specify a rectangle, X and Y give the script the upper left corner of the rectangle, W tells the script how for to the east the desired rectangle goes (with W = 0 indicating "just the one tile", W = 1 indicating "the tile and the one to the east of it", and so on), and H similarly says how far to the south. For commands not based on location, or not entirely based on location, X, Y, W, and H can mean various other things; F is also used to mean various other things besides location just in general.

As an example, if I wanted a character to check the 3x3 square with itself at the center for the presence of a Roach, the code would look like this:

Click here to view the secret text

Notice that I set _MyScriptX and _MyScriptY to the upper left corner of the 3x3 square by moving 1 to the north and 1 to the west of the character's current position (which is always _MyX, _MyY), and then set H and W both to 2 to specify that you want a 3x3 rectangle (err, square). It is helpful while doing any kind of location-based commands to remember that North and West are subtraction, South and East are addition.

You may wonder why I'm setting all of the relevant variables to -9999 at the end. This is because for any MyScript variable, the value -9999 translates as "do not override". MyScript variables affect all kinds of commands, such as the Wait command, and failing to set them back to -9999 as soon as you're done with them will frequently result in unintended behavior. So remember to always, ALWAYS reset your MyScripts to -9999. If there's something buggy going on with your scripted character and you don't know why, this should be the first thing you check for before coming to anyone else for help, because even among experienced script-writers it's the most probable cause for something to go wrong.

3. MyScript and Orientations

When you set or check the orientation of an entity (that is, which way it's facing) by messing with _O or _MyO, or using a command such as Get Entity Direction, it's helpful to know what number means what direction. That correspondence is as follows, where X is the entity's location:

A good way to remember this is to just write down the numbers from 0 to 8 in a left-to-right, top-to-bottom order arranged into 3 rows of 3 numbers each. What about 4, which is in the center? 4 is treated as "no direction" and usually doesn't mean anything and crashes stuff. It's mainly there as a placeholder so that the math works out for all of the other numbers.

One reason that orientations are numbered in this way (or at least, a good justification for it) comes in the form of a few handy tricks you can do with this. For example, when you want to reverse the orientation of an entity, you don't have to go through detecting which of the 8 orientations it has to figure out what the opposite is. You can just take _MyO = 8 - _MyO. Check it with the chart above if you don't believe me! Better still, if you want a character to just move in the direction it's facing, there is a very simple way to do this as well, using _MyScript:
        Set var "_MyScriptX" = (_MyO % 3) - 1
        Set var "_MyScriptY" = (_MyO / 3) - 1
        Move 0,0,0,0

Simple! And it all works because of the math involved in X, Y, and O variables. A slight modification of this will allow you to teleport an entity to the location 1 tile in the direction of its orientation instead, in case you don't want a turn-stopping command quite so soon:
        Set var "_MyScriptX" = _MyX + (_MyO % 3) - 1
        Set var "_MyScriptY" = _MyY + (_MyO / 3) - 1
        Teleport to 0,0
As a final note, any command involving orientations may also take 9 to mean "rotate CW" and 10 to mean "rotate CCW" where those statements make sense.

4. The Promised Reference

What follows is a list of everything I could dig up about how various commands are affected by the different _MyScript* variables. Some of this is taken from other pre-existing threads, some of it from my own testing or memory, but at any rate it should be helpful for anyone hoping to make use of this powerful tool.

A list of commands for which X and Y specify the X, Y location for the command to be applied to:
Click here to view the secret text


A list of commands for which X, Y, W, and H specify the rectangle in which the command is applied:
Click here to view the secret text


A list of commands whose only parameter is changed by setting X
Click here to view the secret text


Generate Entity takes this value of _MyScriptH to mean this entity; "Wait for/while entity type" takes the same values of _MyScriptF to mean the same entity (based on this thread):
Click here to view the secret text


Build/Build Marker/Wait for Item at take this value of _MyScriptF to mean this item type (based on this thread):
Click here to view the secret text


The meaning of the value _MyScriptF in the Wait for Entity command:
Click here to view the secret text


Other Notes:
-_MyScriptW determines the orientation of the generated entity for Generate Entity.
-_MyScriptF changes the attack type of Attack Tile.
-Generate Entity takes _MyScriptW as the orientation of the newly-generated entity
-Game Effect's Direction, Sound Effect option, and which effect it is can be specified by W, H, and F in some order, but I don't know which one.
-Flashing Message can have its color altered by X.
-Face towards like guard and Move to probably have which entity type the facing or moving toward is aimed at by _MyScriptX, with the Forbid turning and Single step options affected by some others among Y, W, H, and F.
-In DROD RPG, Speech Custom (which is also displayed as "Speech at") can have its location dynamically determined the same way as Attack Tile, etc., but my testing indicates this does not work in normal DROD. It's possible I'm just doing it wrong, however.
-When setting the relative positions of things, be wary of going outside room bounds! This means look out for any X or Y values less than 0, an X value bigger than 37, a Y value bigger than 31, or a W or H value that will push the rectangle beyond the edge of the room. If you crash DROD trying to check for or build something, this may be a culprit.
-The two previous references on MyScript stuff can be found here and there, but the one that's there is for DROD RPG and will not be completely accurate to what happens in regular DROD.

____________________________
109th Skywatcher

Here are some links to Things!
Click here to view the secret text


[Last edited by Xindaris at 07-13-2024 04:47 AM]
10-15-2017 at 09:58 PM
View Profile Send Private Message to User Show all user's posts High Scores This architect's holds Quote Reply
Romain673
Level: Goblin
Avatar
Rank Points: 17
Registered: 08-18-2017
IP: Logged
icon Re: A (More) Comprehensive MyScript Primer and Reference (0)  
(I have tried it and it worked : i have created four wall next to a player. Ty.
But it take 3x4 lines for doing that.)

Is there a (simple) way to create multiple object without using 4 lines (or more) for each ?
I tried to create object at (5, 4, 5, 4) instead of (0, 0, 0, 0) and setting _MyScriptX at _X-5 (or +5) but it doesn't seem to work.
It seem that I really need to change each variable one by one.


[Last edited by Romain673 at 10-18-2017 06:11 PM]
10-18-2017 at 06:11 PM
View Profile Send Private Message to User Show all user's posts This architect's holds Quote Reply
Xindaris
Level: Smitemaster
Avatar
Rank Points: 1588
Registered: 06-13-2015
IP: Logged
icon Re: A (More) Comprehensive MyScript Primer and Reference (0)  
For Generate Entity you have no choice but to make a new command for each individual entity you want to make. You can put it in a loop if there's some sensible ordering to how the entities are supposed to be generated, but that's about it.

For Build (and Build Marker) commands, anything other than a rectangle must be broken down into rectangles and built one rectangular command at a time, which I'm guessing is what you're asking about. If what you want is 4 lines around the player's position forming a box then there is basically no better way than to run four separate build commands, one for each line.

And yes, in general it's necessary to change each myscript variable individually. But there are two small optimizations possible, assuming I understood what you're trying to do:
-If both horizontal walls have the same width, you can build them both by setting _MyScriptX, W, and H to the correct things and then only changing _MyScriptY in between the two build commands. The same can be done for the vertical walls, changing only X in between the two build commands.
-If the walls are always going to be the same height and width, you can actually give the static, pre-myscript-injection commands the desired height and width for each, and only change X and Y between the commands to ensure that the correct "upper left corner" is being pointed at, leaving H and W as "don't override" so that it just reads what's already in the static command.

____________________________
109th Skywatcher

Here are some links to Things!
Click here to view the secret text


[Last edited by Xindaris at 10-19-2017 01:23 AM]
10-19-2017 at 01:20 AM
View Profile Send Private Message to User Show all user's posts High Scores This architect's holds Quote Reply
Xindaris
Level: Smitemaster
Avatar
Rank Points: 1588
Registered: 06-13-2015
IP: Logged
icon Re: A (More) Comprehensive MyScript Primer and Reference (+1)  
Update note:
hyperme recently pointed out that _MyScriptX has no effect on the Imperative command, contrary to what I for some reason thought. That's been edited out of that list.
Also, I needed to know how to set the orientation of a generated entity dynamically and discovered it wasn't listed here. It's _MyScriptW. I have now added that information to the "Other Notes" section.

____________________________
109th Skywatcher

Here are some links to Things!
Click here to view the secret text


[Last edited by Xindaris at 10-27-2018 05:43 PM]
10-27-2018 at 05:42 PM
View Profile Send Private Message to User Show all user's posts High Scores This architect's holds Quote Reply
averagemoe
Level: Smiter
Avatar
Rank Points: 492
Registered: 03-22-2012
IP: Logged
icon Re: A (More) Comprehensive MyScript Primer and Reference (+1)  
I figured out another use for the mathematical properties of the meanings of the _O value.

Set an arbitrary variable to _O%2. If the player is facing any orthogonal direction, the result will be 1. If the player is facing diagonally, it will be 0.

Admittedly, I don't see a lot of reasons for a script to check this, but it takes up less space than checking four directions individually.

____________________________
The internet is no place to act like a wild animal.
02-04-2019 at 06:14 AM
View Profile Send Private Message to User Show all user's posts Quote Reply
Dying Flutchman
Level: Smiter
Avatar
Rank Points: 406
Registered: 01-27-2017
IP: Logged

File: DirectionDemo.hold (2.3 KB)
Downloaded 61 times.
License: Public Domain
icon Re: A (More) Comprehensive MyScript Primer and Reference (+3)  
Some additional thoughts on directions that might interest some of you. I have attached a demo hold that's edit for all, so you can check out the scripting.

X- or y-component
Click here to view the secret text


Orthogonal or diagonal
Click here to view the secret text


Rotation
Click here to view the secret text


Main axis
Click here to view the secret text


____________________________
Autocorrect is not my friend. Apologies for the typos.

[Last edited by Dying Flutchman at 02-05-2019 03:04 PM]
02-05-2019 at 01:02 PM
View Profile Send Private Message to User Show all user's posts This architect's holds Quote Reply
hyperme
Level: Smitemaster
Avatar
Rank Points: 1185
Registered: 06-23-2006
IP: Logged
icon Re: A (More) Comprehensive MyScript Primer and Reference (+1)  
Setting _MyO to 9 or 10 doesn't rotate the character because _MyO gives access to the underlying object direction. It only accepts values in [0, 8] because those are only values that map to directions. Setting _MyO to 9 or 10 (or any other number) makes about as much sense as setting it to 'wubba'.

If you want a slightly more compact rotation function, GameConstants.h has the function that the actual DROD game uses to rotate things. It's not immediately convertable to DRODscript, since it uses a switch statement, but it's probably possible to extract some common numeric properties for each pair of orientations.

____________________________
[Insert witty comment here]
Qzvlkx?
02-07-2019 at 05:05 PM
View Profile Send Private Message to User Show all user's posts High Scores This architect's holds Quote Reply
Xindaris
Level: Smitemaster
Avatar
Rank Points: 1588
Registered: 06-13-2015
IP: Logged
icon Re: A (More) Comprehensive MyScript Primer and Reference (+1)  
!Correction!

I don't know why, but the notes at the very end claimed that _MyScriptH changes the direction of "Generate Entity"; this is incorrect. H changes what entity gets generated; W is what changes the direction. I've fixed it now.

____________________________
109th Skywatcher

Here are some links to Things!
Click here to view the secret text

03-30-2024 at 05:56 PM
View Profile Send Private Message to User Show all user's posts High Scores This architect's holds Quote Reply
Kalin
Level: Master Delver
Avatar
Rank Points: 216
Registered: 01-25-2016
IP: Logged
icon Re: A (More) Comprehensive MyScript Primer and Reference (+1)  
So, I was trying to find a way for scripts to detect adders, and discovered that not only were they missing from the dropdown in the script editor, they (and the other snakes) were missing from this thread too. But a couple values in the "entity type" list were still unknown, so maybe they were for the different snakes?

Click here to view the secret text


____________________________
he/him
~170th Skywatcher
05-02-2024 at 06:36 PM
View Profile Send Private Message to User Show all user's posts Quote Reply
Xindaris
Level: Smitemaster
Avatar
Rank Points: 1588
Registered: 06-13-2015
IP: Logged
icon Re: A (More) Comprehensive MyScript Primer and Reference (0)  
Nice work, I've added those to the list.

____________________________
109th Skywatcher

Here are some links to Things!
Click here to view the secret text

05-02-2024 at 08:26 PM
View Profile Send Private Message to User Show all user's posts High Scores This architect's holds Quote Reply
Xindaris
Level: Smitemaster
Avatar
Rank Points: 1588
Registered: 06-13-2015
IP: Logged
icon Re: A (More) Comprehensive MyScript Primer and Reference (0)  
Apparently, averagemoe had the answer in the post twice previous some time ago: https://forum.caravelgames.com/viewtopic.php?TopicID=38386&page=0#428384

In addition, 29 means "NPC"! So I'll add that to the first post too.

____________________________
109th Skywatcher

Here are some links to Things!
Click here to view the secret text

07-13-2024 at 04:47 AM
View Profile Send Private Message to User Show all user's posts High Scores This architect's holds Quote Reply
New Topic New Poll Post Reply
Caravel Forum : DROD Boards : Architecture : A (More) Comprehensive MyScript Primer and Reference
Surf To:


Forum Rules:
Can I post a new topic? No
Can I reply? No
Can I read? Yes
HTML Enabled? No
UBBC Enabled? Yes
Words Filter Enable? No

Contact Us | CaravelGames.com

Powered by: tForum tForumHacks Edition b0.98.8
Originally created by Toan Huynh (Copyright © 2000)
Enhanced by the tForumHacks team and the Caravel team.