Announcement: Be excellent to each other.


Caravel Forum : Caravel Boards : General : Neat Script Tricks (Share yours?)
New Topic New Poll Post Reply
Poster Message
Xindaris
Level: Smitemaster
Avatar
Rank Points: 1593
Registered: 06-13-2015
IP: Logged
icon Neat Script Tricks (+2)  
I recently learned, from a bug report of all things, a really neat trick to use very relatively few lines of code to have a character move in the direction it's oriented toward. It's demonstrated in this loop and works because of cool math, and is way more efficient than my old way of having a Label for each individual direction I want a character to be able to move in:
Click here to view the secret text


This got me curious, what other neat scripting tricks are there that aren't too well-known, or at least not mentioned in prominent places? Have you figured out any nifty script shortcuts that do things faster than the more obvious way?

____________________________
109th Skywatcher

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

01-14-2016 at 10:02 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: 487
Registered: 03-22-2012
IP: Logged
icon Re: Neat Script Tricks (0)  
I try to cram all NPCs that will never speak or be visible into the northwest corner of the room, all facing northwest. This keeps the numbers relating to their position as small as possible.

Also, if your hold has custom characters and a global script for something like a multi-room acheivement, hide that global script in the default script for your custom character, instead of making a character with a name like "Acheivement tracker 1". For instance, if I was charge of TSS, I would've had the script for managing the overmap be the default script for the Critic.
01-16-2016 at 06:20 PM
View Profile Send Private Message to User Show all user's posts Quote Reply
skell
Level: Legendary Smitemaster
Avatar
Rank Points: 3771
Registered: 12-28-2004
IP: Logged
icon Re: Neat Script Tricks (0)  
averagemoe wrote:
Also, if your hold has custom characters and a global script for something like a multi-room acheivement, hide that global script in the default script for your custom character, instead of making a character with a name like "Acheivement tracker 1". For instance, if I was charge of TSS, I would've had the script for managing the overmap be the default script for the Critic.
Why? I think multiple scripts broken into multiple characters are much easier to manage and, potentially, fix.

____________________________
My website
01-16-2016 at 06:39 PM
View Profile Send Private Message to User Send Email to User Visit Homepage Show all user's posts High Scores This architect's holds Quote Reply
hyperme
Level: Smitemaster
Avatar
Rank Points: 1220
Registered: 06-23-2006
IP: Logged
icon Re: Neat Script Tricks (+1)  
I think it's an attempt to save on memory use?

Which is a terrible trade off. In the end, it's much better to put things where they belong, because someone might have to use them. Also ints take up the same amount of memory regardless of their value, so the 'keep numbers small' thing won't really help.

____________________________
[Insert witty comment here]
Qzvlkx?
01-16-2016 at 08:45 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: 487
Registered: 03-22-2012
IP: Logged
icon Re: Neat Script Tricks (0)  
skell wrote: Why? I think multiple scripts broken into multiple characters are much easier to manage and, potentially, fix.

I mean giving the script an avatar and sprite that can be used as an actor in the hold, so that the list of custom characters isn't cluttered with things whose only purpose is to run a script.

____________________________
The internet is no place to act like a wild animal.
01-17-2016 at 02:49 AM
View Profile Send Private Message to User Show all user's posts Quote Reply
skell
Level: Legendary Smitemaster
Avatar
Rank Points: 3771
Registered: 12-28-2004
IP: Logged
icon Re: Neat Script Tricks (+1)  
averagemoe wrote:
I mean giving the script an avatar and sprite that can be used as an actor in the hold, so that the list of custom characters isn't cluttered with things whose only purpose is to run a script.
I still say it's perfectly fine to have custom characters whose only purpose is to run a script. There are only two cons to this:
1. Your list of characters is bigger (which is not a problem because a few additional characters, properly named, don't make a difference).
2. There are more characters running in the room at the same time (which again is not a problem because you'd need to add a few thousand of them to experience any performance issues)

which aren't real problems. On the other hand if you go this way you ensure to have better separation of concerns which increases readability of your scripts both for yourself (if you ever need to come back to the project or it's a hold with a long development period) or people who might be curious how things works. Additionally you don't have to worry if you can place a given visible NPC in a room because you know their script is either missing or will never have side effects.

____________________________
My website
01-17-2016 at 10:20 AM
View Profile Send Private Message to User Send Email to User Visit Homepage Show all user's posts High Scores This architect's holds Quote Reply
Nuntar
Level: Smitemaster
Avatar
Rank Points: 4862
Registered: 02-20-2007
IP: Logged
icon Re: Neat Script Tricks (+1)  
5.1 adds the "Natural target" feature, which allows custom monsters to behave like normal monsters around decoys, stalwarts and soldiers*, and timeclones, and to respect player stealth and invisibility. The following script shows how to use this feature to get your monster moving:
  Imperative 3
  Imperative 7
Label loop
  Get natural target Regular monster
  If ... 
        Wait until var "_ReturnX" = _MyX
     If ... 
           Wait until var "_ReturnY" = _MyY
        Wait 0
     If End 
  If End 
  Set var "_MyScriptX" = _ReturnX
  Set var "_MyScriptY" = _ReturnY
  Move to 0,0,0,1
  Set var "_MyScriptX" = -9999
  Set var "_MyScriptY" = -9999
  Go to loop


*However, stalwarts and soliders still won't target custom monsters!

____________________________
50th Skywatcher

[Last edited by Nuntar at 11-27-2018 12:30 AM]
01-17-2016 at 05:39 PM
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 : Caravel Boards : General : Neat Script Tricks (Share yours?)
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.