Spells based on items equipped

Ask and answer questions about making games and related topics. Unrelated topics go in that other forum.

Moderators: marionline, SDHawk

Post Reply
User avatar
guo
Metal Slime
Posts: 749
Joined: Fri Dec 04, 2009 9:12 pm

Spells based on items equipped

Post by guo »

Has anyone tried linking individual skills to items equipped? i'm looking to only allow each hero a max 4 skills/attacks based on equipment. eg, you "equip" a fireball item on a hero to allow them to use it in battle, and cant't see the spell if the item isn't equipped.

What would a script to add/remove skills in this manner look like?

Regards
vvight.wordpress.com
User avatar
Bird
Slime Knight
Posts: 227
Joined: Thu Jan 26, 2012 2:19 pm
Location: Germany

Post by Bird »

Use an on-close script for your menu. In this script, you have to input two long lists: One with all the "learnable" spells and one with the equipment-to-spell-relations.

Code: Select all

plotscript,close,begin
# Use it as "On-close script" under "Edit Menus" in Custom. Beware that the commands 
#"forget spell" and "teach spell" take the spell number plus 1 as argument. 
# So in this example, the spells #36 and #216 are used.
# These spells have to be in the heros spell list as "Learned from an item".
variable (a)
variable (b)
# Erase the whole list of temporary spells for all heros
for (a,0,3,1) do(
  forget spell (a,36 + 1)
  forget spell (a,216 + 1)
)
# Add spells back, if the right item is equipped
for (a,0,3,1) do (     # Loops through all heros in active party
  for (b,1,5,1) do (   # Loop for all equipment slots
    # Item #11 learns spell #36
    if (check equipment (a,b) == 11) then (teach spell (a,36 + 1))  
    # Item #201 learns spell #216
    if (check equipment (a,b) == 201) then (teach spell (a,216 + 1)) 
  )
)
end
Although there is one mistake, when using this as the main menus on-close script. It doesn't update the spell lists immediately. So if you change your equipment and have a direct look at the spell list, it's still the old configuration (you would have to close the main menu first).
If the equipment menu would be defined as it's own menu with this as the on-close script, it should work.
Last edited by Bird on Sun May 03, 2020 10:35 am, edited 1 time in total.
User avatar
SwordPlay
Chemical Slime
Posts: 966
Joined: Sun Jan 22, 2017 9:32 am
Location: London, England
Contact:

Post by SwordPlay »

you can already make commands appear conditionally based on a tag in the heroes main battle menu. its really cool! probably
"Imagination. Life is your creation."
User avatar
guo
Metal Slime
Posts: 749
Joined: Fri Dec 04, 2009 9:12 pm

Post by guo »

Thanks Bird! I'll look into that script. Might be a bit of work ahead.

Swordplay: I considered that, but then only one hero needs the item equipped for the spell to be usable for all heroes.
vvight.wordpress.com
User avatar
guo
Metal Slime
Posts: 749
Joined: Fri Dec 04, 2009 9:12 pm

Post by guo »

Code: Select all

plotscript,close,begin
# Use it as "On-close script" under "Edit Menus" in Custom. Beware that the commands
#"forget spell" and "teach spell" take the spell number plus 1 as argument.
# So in this example, the spells #36 and #216 are used.
# These spells have to be in the heros spell list as "Learned from an item".
variable (a) #hero num
variable (b) #spell num
variable (c) #equip slot
variable (d) #item num

# Erase the whole list of temporary spells for all heros
for (a, 0, 3, 1) do(
	for (b, 0, 150, 1) do(
		forget spell (a, b + 1)
	)
)

# Add spells back, if the right item is equipped
for (a, 0, 3, 1) do (     # Loops through all heros in active party
	for (c, 1, 5, 1) do (   # Loop for all equipment slots
		for (d, 0, 150, 1) do( 
			# Item #d learns spell #b+1
			if (check equipment (a, c) == d) then (teach spell (a, b + 1)) 
		)
	)
)
end
How does this look? I plan to have items & spells 0-150 reserved for the equippable spells, with the item number and spell number matching up for ease of use.
Last edited by guo on Fri May 08, 2020 2:52 am, edited 1 time in total.
vvight.wordpress.com
User avatar
Bird
Slime Knight
Posts: 227
Joined: Thu Jan 26, 2012 2:19 pm
Location: Germany

Post by Bird »

Maybe we have to use "hero by slot (a)" instead of "a" to pick the four heros in the active party, but only for the commands "teach spell" and "forget spell", not for "check equipment". I just looked at my scripts and that did work at least.

My computer would get performance issues there, because the triple-for-do has to check 4 x 5 x 150 cases, which results in 3000 cases! Sometimes, these big scripts makes the engine say, that a script is stuck in an infinite loop.

I used the very unelegant way of dividing the script into more parts, which were split by a "wait (1)" to give the old age computer at least some hope, that it is actually finishing something. In that case, 4 x 300 different cases were too much to handle at once. But that was with the 2013 engine.

Code: Select all

variable (a)
variable (b)
for (a,0,3,1) do (
  for (b,0,120,1) do (teach spell (hero by slot (a),b))
  wait (1)
  for (b,121,240,1) do (teach spell (hero by slot (a),b))
  wait (1)
  for (b,241,300,1) do (teach spell (hero by slot (a),b))
  wait (1)
  give experience (a,30)	
)
Does someone else know, if that route is still required with everything up-to-date?
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

There is a bug in the script: it does "teach spell (a, b + 1)" but b is not set! I fixed that below.

Hmm. Calling "check equipment" 3000 times is trivial, even on a very old computer. But Bird is right that "teach spell" is a slow command, since it reads (all!) hero data from disk every time. That's something I want to stop doing at some point, along with the many other places where hero data is read from disk. But at least starting from Fufluns loading hero data should be faster than before (however it also got much slower at some point after 2013 when we changed the hero file format).

But that script can be modified so there is no worry about speed, by replacing those two "for" loops with one that only calls teachspell as necessary:

Code: Select all

for (a, 0, 3, 1) do (     # Loops through all heros in active party
   for (b, 0, 150, 1) do(  # Loop through spells/items
      variable (have item)
      have item := false
      for (c, 1, 5, 1) do (   # Loop through all equipment slots
         if (check equipment (a, c) == b) then (have item := true)
      )
      if (have item) then (
         if (knows spell (a, b + 1) == false) then (teach spell (a, b + 1))  # Avoid calling "teach spell"
      ) else (forget spell (a, b + 1))
   )
)
Last edited by TMC on Fri May 08, 2020 3:35 pm, edited 2 times in total.
User avatar
guo
Metal Slime
Posts: 749
Joined: Fri Dec 04, 2009 9:12 pm

Post by guo »

That's way better, thanks TMC
vvight.wordpress.com
User avatar
Bird
Slime Knight
Posts: 227
Joined: Thu Jan 26, 2012 2:19 pm
Location: Germany

Post by Bird »

TMC wrote:But at least starting from Fufluns loading hero data should be faster than before (however it also got much slower at some point after 2013 when we changed the hero file format).
I just tested it and can confirm, what you wrote. 250 repeated cases of "get item" or 50 repeated cases of "add hero" for example were a problem too huge for the old engine of 2013, but in the current one it only makes the screen freeze for less than a second. No script hang-ups. Good job there!
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

You might see "add hero" get slower if you upgrade the .rpg using Custom. game.exe now actually use far more work loading the hero data than before, but won't close and reopen the data files every time you call "get item" or "add hero", so Windows itself is doing much less work, and that's probably what matter most (unless you use Mac or Linux, which are designed for fast re-opening of files).
Last edited by TMC on Tue May 12, 2020 3:03 am, edited 1 time in total.
User avatar
guo
Metal Slime
Posts: 749
Joined: Fri Dec 04, 2009 9:12 pm

Post by guo »

All working wonderfully!
Now I need a way to run the on close script when the equip menu is closed (or some kind of fancy workaround). Perhaps special script "Menu Action"? Leads me to ask whether on close scripts could be implemented for special screens?

Also, would there be any other places I'd want this script called? eg. add/remove/swap hero, load game, after battle, map autorun.

Cheers.
Last edited by guo on Tue May 12, 2020 10:36 pm, edited 1 time in total.
vvight.wordpress.com
User avatar
Bird
Slime Knight
Posts: 227
Joined: Thu Jan 26, 2012 2:19 pm
Location: Germany

Post by Bird »

Let's change the plan. We don't use that big script as a close on menu script. I renamed everything here.

Code: Select all

plotscript,menuopens,arg,begin
# This script has to be used for the menu as "Run Script" and has to be
# passed an argument, which can be done at "Extra Data", depending
# on, which menu should be opened by this script here.
if (arg == 1) then (equip menu)
if (arg == 2) then (team menu)
temporary spells checking
main menu
end

script,temporary spells checking,begin
# Beware that the commands "forget spell" and "teach spell" take
# the spell number plus 1 as argument. These spells have to be
# in the heros spell list as "Learned from an item".
variable (a) #hero num
variable (b) #spell num
variable (c) #equip slot
variable (d) #item num

# Erase the whole list of temporary spells for all heros
for (a, 0, 3, 1) do(
   for (b, 0, 150, 1) do(
      forget spell (a, b + 1)
   )
)

# Add spells back, if the right item is equipped
for (a, 0, 3, 1) do (     # Loops through all heros in active party
   for (c, 1, 5, 1) do (   # Loop for all equipment slots
      for (d, 0, 150, 1) do(
         # Item #d learns spell #b+1
         if (check equipment (a, c) == d) then (teach spell (a, b + 1))
      )
   )
)
end
Use Run script "menuopens" instead of the equip menu and the team menu for example. Put "Extra data 0" to 1 or 2, as this is the argument that you pass to that script. Look at the script: If the argument is 1, it opens the equip menu. If it is 2, it opens the team menu. Of course there could be more options added here.
Use the bitset "Close menu when activated", as this removes the main menu from the screen. But you surely want the main menu back, when you close the equip or the team menu, right? Therefore the script activates the main menu once again after that. I'd use the bitset "Remember selection when reopening" for the main menu, because it keeps track of what you have chosen last time.
Post Reply