Recent Posts

Pages: 1 2 [3] 4 5 ... 10
21
I've actually been planning on trying to figure out how to force the sword to complete its animation before the next attack, so your downside is my upside, haha. I think it can make an attack feel much weightier, and forces strategy if you lock the player into the animation.
22
Wait, I got it! Just had to add hero:unfreeze(). This code works:

Code: Lua
  1. -- ♡ Copying is an act of love. Please copy and share.
  2.  
  3. -- Script that lets you use the sword as a normal item.
  4. -- Note that the sword button cannot be held with this method (for charging or tapping)
  5.  
  6. local item = ...
  7. local game = item:get_game()
  8.  
  9. function item:on_created()
  10.   self:set_savegame_variable("stick")
  11.   self:set_assignable(true)
  12. end
  13.  
  14. function item:on_using()
  15.   local hero = game:get_hero()
  16.   hero:unfreeze()
  17.   hero:start_attack()
  18.   item:set_finished()
  19. end

Thanks Christopho!

EDIT 1:

Note that this approach has some downsides:

1. The item button cannot be held to hold the sword for longer. You'll need to script that if you want it. I did achieve that here with a different type of item.
2. It doesn't seem you can tap the item button to swing the sword very fast. The animation must totally complete before you can press the item button again.

EDIT 2:

Using the held item command approach improves the situation:

Code: Lua
  1. -- ♡ Copying is an act of love. Please copy and share.
  2.  
  3. -- Script that lets you use the sword as a normal item.
  4. -- Note that the sword button cannot be held with this method (for charging or tapping)
  5.  
  6. local item = ...
  7. local game = item:get_game()
  8.  
  9. function item:on_created()
  10.   self:set_savegame_variable("stick")
  11.   self:set_assignable(true)
  12. end
  13.  
  14. function item:on_command_pressed(command)
  15.   local hero = game:get_hero()
  16.   hero:start_attack()
  17.   item:set_finished()
  18. end

This enables me to tap the sword button very quickly and I don't have to unfreeze the hero first. It still does not let me press and hold the sword button, which doesn't seem possible with the current API. Fortunately this is a feature I'm not using in my game and have disabled anyway.
23
I guess it is because the attack is not allowed during the item state. Try to call item:set_finished() just before hero:start_attack().

Ohh, that makes sense! Calling item:set_finished() lets the function continue executing, but strangely the sword is still never swung. This is my code now.

Code: Lua
  1. local item = ...
  2. local game = item:get_game()
  3.  
  4. function item:on_created()
  5.   self:set_savegame_variable("stick")
  6.   self:set_assignable(true)
  7. end
  8.  
  9. function item:on_using()
  10.   print("beep")
  11.   item:set_finished()
  12.   print("boop")
  13.   game:get_hero():start_attack()
  14.   print("blip")
  15. end

Pressing the item button does indeed print:

Code: [Select]
beep
boop
blip

But the sword still doesn't swing. The player doesn't freeze, either. Nothing happens except printing to the console. ???
24
Development / Re: hero:start_attack() not working (when called from an item script)
« Last post by Christopho on October 11, 2018, 09:04:03 pm »
I guess it is because the attack is not allowed during the item state. Try to call item:set_finished() just before hero:start_attack().
25
Development / Re: Wait, I don't understand how "require" works
« Last post by alexgleason on October 11, 2018, 08:36:59 pm »
sol.main.game does not exist yet (i.e. is nil) at the time your script is executed. And your script is executed the first time you require() it, so most likely long before you assign something to sol.main.game.

OMG thank you! It took me a minute to understand what you meant. The menu script itself is executed before sol.main.game is set, but the function call happens after, so it has access to it. Thank you for bringing me clarity. ;D
26
Hi, I was hoping to create an item that triggers the hero's normal attack on_using(). It's simple:

Code: Lua
  1. local item = ...
  2. local game = item:get_game()
  3.  
  4. function item:on_created()
  5.   self:set_savegame_variable("stick")
  6.   self:set_assignable(true)
  7. end
  8.  
  9. function item:on_using()
  10.   print("beep")
  11.   game:get_hero():start_attack() -- Normal sword attack
  12. end

When I equip the item and press the button, the console outputs "beep", and then the player becomes frozen. The sword is not swung. From the console, I can unfreeze the player with: sol.main.game:get_hero():unfreeze().

Any clue why start_attack() isn't working here? I can call sol.main.game:get_hero():start_attack() from the console at any time and it works, but the same thing doesn't work inside this item script. Thanks!
27
Development / Re: Wait, I don't understand how "require" works
« Last post by Christopho on October 11, 2018, 08:27:38 pm »
sol.main.game does not exist yet (i.e. is nil) at the time your script is executed. And your script is executed the first time you require() it, so most likely long before you assign something to sol.main.game.
28
Development / Re: Wait, I don't understand how "require" works
« Last post by alexgleason on October 11, 2018, 07:56:02 pm »
So if the script is also required somewhere else, then instead of running the script a second time, the return values saved from the first time the require command was used will simply be passed along to wherever else the script gets required.

Thank you for clarifying this! I found this thread very helpful.

Variable scope still trips me up. For instance, in this menu script I don't understand why this code doesn't work:

Code: Lua
  1. local menu = {}
  2. local game = sol.main.game -- Use `game` as a shorthand for `sol.main.game`
  3.  
  4. function menu:on_started()
  5.   print(game:get_item_assigned(1):get_name())
  6. end
  7.  
  8. return menu

Error: In on_started: [string "scripts/menus/my_menu.lua"]:5: attempt to index upvalue 'game' (a nil value)

Yet this code works just fine:

Code: Lua
  1. local menu = {}
  2.  
  3. function menu:on_started()
  4.   -- Use sol.main.game directly
  5.   print(sol.main.game:get_item_assigned(1):get_name())
  6. end
  7.  
  8. return menu

Using sol.main.game works great. 👍 (I'm using the boilerplate code which sets sol.main.game within main.lua)

More confusingly, this code also doesn't work.

Code: Lua
  1. local menu = {}
  2. game = sol.main.game -- Use a global variable this time
  3.  
  4. function menu:on_started()
  5.   print(game:get_item_assigned(1):get_name())
  6. end
  7.  
  8. return menu

Error: In on_started: [string "scripts/menus/item_menu.lua"]:5: attempt to index global 'game' (a nil value)

I thought when you require() a Lua script, any global variables created by the script persist throughout the whole codebase. I'm super confused why these don't work. I thought variable scope was the problem, but maybe not?
29
Your projects / Re: Vegan on a Desert Island
« Last post by alexgleason on October 11, 2018, 07:37:36 pm »
Have you played Survival Kids from the gbc? (Gameplay here: https://www.youtube.com/watch?v=eQet3NwtHO4) I played it a little bit and I believe this game might have taken some inspiration from there too, or at least it has some things in common. I never finished it because it was too hard for a casual player like me, though. If you haven't played it, I recommend you to take it a look at it for inspiration and good ideas in your game. :)

Hey, thanks for this!! Incidentally I recognize those palm tree sprites, which I stumbled upon looking for art inspiration, but I never looked into the actual game. I will definitely give it a shot and see if there's anything I can glean from it. ;D
30
Development / Re: Cutscene troubles (as menu)
« Last post by Christopho on October 10, 2018, 01:33:10 pm »
You can add an initialization function to your cutscene table, and store the game from there:
Code: Lua
  1. local cutscene = {}
  2.  
  3. function cutscene:initialize(game)
  4.   cutscene.game = game
  5. end
  6.  
  7. function cutscene:on_started()
  8.  
  9. ...
  10.  
and call this function just before starting your menu.
Pages: 1 2 [3] 4 5 ... 10