ID:2925162
 
(See the best response by Kozuma3.)
Code:
mob/pet
icon = 'pet.dmi'

var
mob/player/owner

var/list/spells = list()
var/list/spell_unlocks = list()

New()
..()

spell_unlocks = list(
3 = list(new /obj/spell/Heal),
5 = list(new /obj/spell/Antidote),
8 = list(new /obj/spell/Blaze),
10 = list(new /obj/spell/Sleep)
)
spells = list() // Initialize the spells list
client << "spell_unlocks initialized: [spell_unlocks]"

// Proc to handle leveling up
proc/levelUp()

// Debug message to check level and spell unlocks
client << "Checking for spell unlocks at level [level]."

// Check if new spells are unlocked at this level
if(level in spell_unlocks)
client << "Unlocking spells for level [level]."
for(var/obj/spell/spell in spell_unlocks[level])
spells += spell
client << "<i><b>[name]</b> has learned [spell.name]!</i>"
// Debugging each spell learned
client << "Spells list: [spells]"
else
client << "No spells to unlock at level [level]."


Problem description:

I'm having an issue where the pet is not learning spells at the defined level in a list. I have debugged all day and haven't figured it out. Any information would be grand.
Best response
You would need to set 3,5,8,10 to strings or have empty elements between the levels in the list.

spell_unlocks = list(
"3" = list(new /obj/spell/Heal),
"5" = list(new /obj/spell/Antidote),
"8" = list(new /obj/spell/Blaze),
"10" = list(new /obj/spell/Sleep)
)


mob/pet/proc/levelUp()
var level_check = "[level]"
if(spells_unlocks.Find(level_check))
for(var/obj/spell/spell in spells_unlock[level_check])
;


Reading it over, wouldn't you want to output those messages to owner instead of client?
In response to Kozuma3
Kozuma3 wrote:
You would need to set 3,5,8,10 to strings or have empty elements between the levels in the list.

spell_unlocks = list(
> "3" = list(new /obj/spell/Heal),
> "5" = list(new /obj/spell/Antidote),
> "8" = list(new /obj/spell/Blaze),
> "10" = list(new /obj/spell/Sleep)
> )

mob/pet/proc/levelUp()
> var level_check = "[level]"
> if(spells_unlocks.Find(level_check))
> for(var/obj/spell/spell in spells_unlock[level_check])
> ;

Reading it over, wouldn't you want to output those messages to owner instead of client?

You're 100% correct. Thank you for the help, the skills are now being added. Have an amazing day!
Associative list keys can't be numbers unless you use alist(), which is new in 516 and still not available to you unless you are in the alpha program.

Those index numbers are being compiled as strings, so you need to use strings to get at the values.
In response to Ter13
Ter13 wrote:
Associative list keys can't be numbers unless you use alist(), which is new in 516 and still not available to you unless you are in the alpha program.

Those index numbers are being compiled as strings, so you need to use strings to get at the values.

That's actually great to know. I absolutely overlooked the smallest detail by not making the numbers into strings. I wonder how often that happens to people in general.

I am glad a feature in 516 was a mistake I made. I'll look into alist() and see what it's all about.
Alpha testing of 516 is open. We've got a channel for distributing the builds set up in BYONDiscord right now if you request access of and are approved by Lummox.

We also pipe the forums, games hub, and news directly into the discord, collaborate on finding and reporting engine bugs, refining feature requests, and do live help on the regular.



Come visit us.