Author Topic: Small keys  (Read 4181 times)

Dragon_noir

  • Newbie
  • *
  • Posts: 26
    • View Profile
Small keys
« on: May 19, 2015, 07:23:30 pm »
How do you enable small keys on a map?

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1179
    • View Profile
Re: Small keys
« Reply #1 on: May 19, 2015, 08:44:55 pm »
This can be done in Lua.
If your map is called "my_map", you can make an item "small_key_my_map" that has an amount (the number of small keys), and set this item as required on each locked door of this map.

Dragon_noir

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Small keys
« Reply #2 on: May 19, 2015, 09:21:55 pm »
I don't really understand, I'm just beginning to learn how to program.

Do I need to create a new item for each key I want in a map?(I can't seem to be able to create a key without this line of code

item:set_savegame_variable("")
)

this is the code for small keys i have now (from Zelda DX)

local item = ...

function item:on_created()

  item:set_shadow("small")
  item:set_brandish_when_picked(false)
  item:set_sound_when_picked("picked_small_key")
  item:set_savegame_variable("possession_small_key")
end

function item:on_obtaining(variant, savegame_variable)

  item:get_game():add_small_key()
end


Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1179
    • View Profile
Re: Small keys
« Reply #3 on: May 19, 2015, 10:00:39 pm »
Zelda Mystery of Solarus DX has a system of small keys different from was I was suggesting. In ZSDX there is only one "small_key" item for the whole game, and the Lua scripts detects in what current dungeon we are to increment the correct savegame variable.

What you can do is a small key item for each dungeon. For example in dungeon 1, you can make several treasure chests containing the item "dungeon_1_small_key".
Your small key item script should do something like item:set_amount_savegame_variable("dungeon_1_small_key_amount") instead of item:set_savegame_variable("possession_small_key"). Because this is an item with an amount: you want to save an integer (the number of small keys), not a boolean (whether the player has an item).

(I should do a tutorial about small keys :))

Dragon_noir

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Small keys
« Reply #4 on: May 19, 2015, 10:29:00 pm »
Ok I tried it , but now I'm getting this error message:

Error: In create_door: bad argument #1 to ? (Bad field 'opening_condition' (equipment item 'small_key' is not saved))

, and my doors do not appear on the map.

This is my small key code now:

local item = ...

function item:on_created()

  item:set_shadow("small")
  item:set_brandish_when_picked(false)
  item:set_sound_when_picked("picked_small_key")
  item:set_amount_savegame_variable("dungeon_small_key_amount")
end

function item:on_obtaining(variant, savegame_variable)

  item:get_game():add_small_key()
end

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1179
    • View Profile
Re: Small keys
« Reply #5 on: May 20, 2015, 08:50:05 am »
For doors to work with an item, the possession state of the item also needs to be saved (maybe I should change that, actually). So in item:on_created(), try to also add item:set_savegame_variable("dungeon_small_key_possession").

And remove the item:on_obtaining() function, it is not needed if the door directly checks the item. (ZSDX is not a great example for small keys, it does more complicated things for historical reasons.)

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1179
    • View Profile
Re: Small keys
« Reply #6 on: May 20, 2015, 09:04:27 am »
Actually I think I forgot something important here...
We need two items: one for the small key counter, and one for the obtainable small keys. Like we do for rupees: there is an rupee item and a rupee bag item.
So for dungeon 1, they would be called "dungeon_1_small_key_counter" and "dungeon_1_small_key"

dungeon_1_small_key_counter.lua:
Code: [Select]
local item = ...

function item:on_created()
  item:set_savegame_variable("dungeon_1_small_key_counter_possession")
  item:set_amount_savegame_variable("dungeon_1_small_key_counter_amount")
end

dungeon_1_small_key.lua:
Code: [Select]
function item:on_created()
  item:set_shadow("small")
  item:set_brandish_when_picked(false)
  item:set_sound_when_picked("picked_small_key")
end

function item:on_obtaining(variant, savegame_variable)

  local counter = item:get_game():get_item("dungeon_1_small_key_counter")
  counter:set_variant(1)
  counter:add_amount(1)
end

Still not tested, sorry. I will prepare a tutorial on this.
« Last Edit: May 20, 2015, 09:25:52 am by Christopho »

Dragon_noir

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Small keys
« Reply #7 on: May 20, 2015, 01:59:51 pm »
Tested it, but it doesn't work :/

I also can't get the key counter to appear on screen.

I'll just wait for a tutorial .

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1179
    • View Profile
Re: Small keys
« Reply #8 on: May 20, 2015, 02:07:51 pm »
Is there an error message?

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1179
    • View Profile
Re: Small keys
« Reply #9 on: May 20, 2015, 03:42:36 pm »
In the meantime you can always reproduce what is done in Zelda Mystery of Solarus DX. At least we know it works :)

Dragon_noir

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Small keys
« Reply #10 on: May 20, 2015, 04:05:07 pm »
No, i don't have an error message.

It works fine for 1 key.
But if I pick up 2 keys and open a door I have no keys left. It's like it doesn't save the amount of keys I have, and as I can't get the HUD to show the small keys counter, I don't really know what's happening.

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1179
    • View Profile
Re: Small keys
« Reply #11 on: May 20, 2015, 04:12:41 pm »
Can you send us your quest data?

Dragon_noir

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Small keys
« Reply #12 on: May 20, 2015, 04:20:06 pm »
Sure, where do you want me to send it?

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1179
    • View Profile
Re: Small keys
« Reply #13 on: May 20, 2015, 04:31:44 pm »
As you want, you can host it with google drive or dropbox for example.

Dragon_noir

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Small keys
« Reply #14 on: May 20, 2015, 04:46:51 pm »
https://drive.google.com/file/d/0B6qvF6HbdSMHWlFqeXJHR0VseUU/view?usp=sharing

Never used google drive before. i hope the link works .