Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - lefthandedhero

Pages: [1]
1
Development / Problem with trying to override a game command
« on: November 03, 2023, 07:40:29 PM »
A while back, I created a custom entity called Follower, who exists to follow the hero. Recently, I created a function entity:swap_sprite() that replaces the Follower's current sprite with the hero's current tunic sprite. Here is that code:
Code: [Select]
function entity:swap_sprite()
  -- obtain the hero's current sprite:
  repsprite = hero:get_tunic_sprite_id()
  -- replace the current sprite with resprite:
  entity:remove_sprite()
  sprite = entity:create_sprite(repsprite)
end

I then created another function meant to override the game command "item_2"; this function will do multiple things, but right now, it just calls the entity:swap_sprite() function. I intend to store this function in a separate metatable, but right now, it is inside the Follower.lua file. Here's the code:
Code: [Select]
function game:on_command_pressed(item_2)
  entity:swap_sprite()
return true
end

However, when I try to run the quest, instead of the sprite changing on command, it occurs at the very beginning of the quest, and the hero can no longer use the sword (movement still works). What could be causing this error?


EDIT: To try to figure out what went wrong, I added two lines of code to the swap_sprite() function that changes the hero's current tunic sprite to the follower's sprite (both are using hero tunic sprites of different colours; the hero's sprite is hero/tunic1 and the follower's is hero/tunic3). The modified function is as follows:
Code: [Select]
function entity:swap_sprite()
  -- obtain the hero's current sprite:
  repsprite = hero:get_tunic_sprite_id()

  -- (For testing) replace hero's sprite with follower's
  local sprite_id = sprite:get_animation_set()
  hero:set_tunic_sprite_id(sprite_id)

  -- replace the current sprite with resprite:
  entity:remove_sprite()
  sprite = entity:create_sprite(repsprite)
end

Now, the two characters swap sprites as intended, but instead of only doing so during the item_2 game command, they do so any time any game command is called: when the hero starts moving, they swap sprites (the hero still starts moving); when the hero changes direction, they swap sprites (the hero still changes direction); When I press "c" for the hero to use their sword, they swap sprites and the sword isn't used; etc.

2
Development / How to Change an Entity's Sprite
« on: October 28, 2023, 03:11:41 AM »
I am trying to create a custom state for the hero that represents the hero being in a different form (think something like Wolf Link from Twilight Princess): while the hero is in this custom state, the hero uses a different sprite and a different main attack. I was wondering how to change the hero's sprite.


EDIT: I now realize that I probably phrased this question poorly. I was wondering, for any entity, how to program it to use a specific sprite under certain conditions. I watched the video on sprite and animation files, so I know how to create a sprite. But, once I have created a sprite, how do I tell the engine, "under this specific condition, this entity's sprite is now (a specific sprite)"?

I want to know this so I can change the hero's appearance during the game, but I really want to know how to do this with any entity. For instance, I currently have a custom entity that currently only exists to always follow the hero, and I want their appearance to change whenever the hero's appearance changes.


EDIT2: I think I may have found a solution: using entity:remove_sprite() then entity:create_sprite(), I have tested it, and it seems to work.

3
Development / What does this error mean?
« on: June 01, 2023, 02:39:32 AM »
Recently, I created a script specifically for code related to the hero, that I called hero_meta. Here is the code that's currently in this script:
Code: [Select]
local MAX_BUFFER_SIZE = 48
function hero:on_position_changed(x,y,z)
  local hero = self
  if not hero.position_buffer then hero.position_buffer = {} end
  local hero = self
  local dir = hero:get_sprite():get_direction()
  table.insert(hero.position_buffer, 1, {x=x, y=y, layer=z, direction=dir})

  if #hero.position_buffer > MAX_BUFFER_SIZE then
    table.remove(hero.position_buffer)
  end
end

The purpose of this code is to establish a maximum distance away from the hero and create a table storing the hero's prior movements within that distance.

When I try to run the quest, I keep getting this error:
Quote
Error: In main: scripts/meta/hero_meta.lua:2: attempt to index global 'hero' (a nil value)

What does this error mean? Any idea what is causing it to occur?

4
Development / Need Help With Having an NPC Follow the Hero
« on: May 02, 2023, 07:36:06 PM »
Hello.

I am new to using the Solarus engine. I want to create an NPC that follows the hero as a companion by closely following behind the hero and following the same path as the hero; the type of movement that's used in A Link to the Past when Link escorts Zelda out of Hyrule Castle and when Marin accompanies Link for part of Link's Awakening, and is at as old as the classic snake game genre.

Any suggestions for implementing this in Solarus?


UPDATE 1: After watching some videos of the section of A Link to the Past where Link escorts Zelda out of Hryule Castle for reference, I observed some details of Zelda's movement that may help: upon entering any new area or exiting a set of stairs, Zelda always starts one in-game step behind Link, stays still after Link's first in-game step, then starts moving after Link's second in-game step, with Zelda's steps being the step Link took two steps previously. When Link stops moving, Zelda immediately stops moving at the same time, and when Link resumes moving, Zelda immediately resumes moving.

After observing this, the plan I have created is to essentially store the hero's movements in a linked list of maximum length 1: upon entering a new map section, the linked list will be empty. When the hero moves, their "step" is pushed into the list, and once the list is full, the non-playing character does each step that is "popped" out of the linked list. When the hero stops moving, the non-playing character stops moving.

I know how to create a linked list in Lua, so there are two things I still need:
1. The ideal method of storing each in-game step in the linked list.
2. How to implement, "while (the hero is moving) {}".

Any suggestions for solving those two things?

Pages: [1]