Need help making a combat function

I would use Pixies CombatLib but It doesn't have WIS as an attribute and uses dice(dice are cool but I don't want them in my game).

I'm making a function to handle combat, I made a simple one for just one enemy in one room but I want the new one to handle all enemies in every room.
My code looks like this right now(I haven't set it all up just the STR melee attack and the run away).

mobs = ScopeVisibleNotHeld()
mobs = FilterByType(mobs, "Mob")
combatmenu = NewObjectList()
list add (combatmenu, "mobs")
ShowMenu ("Fight!", combatmenu, true) {
if (result = "Slime") {
list add (choices, "STR Melee Attack")
list add (choices, "AGL Melee Attack")
list add (choices, "Ranged Attack")
list add (choices, "Black Magic")
list add (choices, "White Magic")
list add (choices, "Run Away")
ShowMenu ("What would you like to do?", choices, true) {
if (result = "STR Melee Attack") {
msg ("You attack the slime!")
Slime.HP = Slime.HP - game.pov.STR
msg (+game.pov.STR+ " damage done! ")
}
else if (result = "AGL Melee Attack") {
msg ("You attack the slime!")
Slime.HP = Slime.HP - game.pov.AGL
msg (+game.pov.AGL+ " damage done! ")
game.pov.health = game.pov.health - mobs.DMG
game.pov.HP = "" +game.pov.health+ " / " +game.pov.maxhealth+ ""
msg ("The enemy attacks!")
combat
}
else if (result = "Ranged Attack") {
msg ("You shoot the slime!")
Slime.HP = Slime.HP - game.pov.AGL
msg (+game.pov.AGL+ " damage done! ")
}
else if (result = "Black Magic") {
}
else if (result = "White Magic") {
}
else if (result = "Run Away") {
if (RandomChance(75)) {
msg ("You got away!")
ClearScreen
}
else {
msg ("You fail to get away!")
game.pov.health = game.pov.health - mobs.DMG
game.pov.HP = "" +game.pov.health+ " / " +game.pov.maxhealth+ ""
msg ("The enemy attacks!")
combat
}
}
}
}
}

I'm not sure what I'm doing wrong but the error is
Error running script: Unable to cast object of type 'System.String' to type 'TextAdventures.Quest.Element'.

So if anyone could help and give some tips I'd appreciate it.


Does it tell you which line the error is in?
When does it generate the error? As soon as combat starts, or after you choose an option from the menu?


As soon as the function is called

attack Slime
Error running script: Unable to cast object of type 'System.String' to type 'TextAdventures.Quest.Element'.


Ah

list add (combatmenu, "mobs")

You're trying to add the string "mobs" to an object list. Object lists can only contain objects.

I think what you want is likely combatmenu = mobs - because the variable mobs is already a list.
Although there's really no point doing that, because you never need this combatmenu variable.

Just change the first menu to:

ShowMenu ("Fight!", mobs, true) {

Because you already have a listy called mobs; there's no need to make another list. You can call it combatmenu if you prefer, but just use one name for it


oh ok thanks


you only need a copy/2nd list, if you want to preserve the (original/1st) list, and thus using the copy/2nd list, to add/remove items as desired


briefly deep copying vs shallow copying (extra reading/pictures for help in understanding lists/arrays and copying them):

https://www.cs.utexas.edu/~scottm/cs307/handouts/deepCopying.htm (if the scripting syntax is too alien/different than that of quest's, just look at the pictures)


to copy (true copy: deep copy) a list, an example:

create ("example_object")

example_object.original_list = NewStringList ()

list add (example_object.original_list, "red")
list add (example_object.original_list, "blue")
list add (example_object.original_list, "yellow")

----------

example_object.copy_list = NewStringList ()

foreach (item, example_object.original_list) {
  list add (example_object.copy_list, item)
}

// you can't just do this (shallow copying):

example_object.copy_list = example_object.original_list

because you're just storing the location (memory address) of the original list into the 'example_object.copy_list' Attribute, which means that if you use the 'example_object.copy_list' and changing something with it, you're just changing something with the original list, as there is no copy list

and then you got to use the 'foreach' to actually add/copy each of the items from the original list into the copy list as well

---------

// and then you can mess with the copy list (leaving the original list un-changed):

list add (example_object.copy_list, "green")
list add (example_object.copy_list, "orange")
list add (example_object.copy_list, "purple")

list remove (example_object.copy_list, "red")

This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums