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.

Show posts Menu

Topics - lefthandedhero

In my code, I wrote the "game:on_command_pressed(command)" event to override the built-in behaviour of the sword command. However, when I run my game, the "game:on_command_pressed(command)" event is never called and the built-in behaviour is used.

I put the event in its own metatable called "game_meta.lua" (which is in a file called "meta") and, in features.lua, I wrote, "require("scripts/meta/game_meta")".

Why is the engine not calling the event, and how do I get the engine to call the event?

EDIT: It turns out that I accidentally wrote the wrong variable name in my code; I accidentally wrote "if action == "attack"" instead of "if command == "attack"".
I recently started making an item and one thing about this item is that, when the hero obtains the item, an entity appears near the hero. I saw that all item scripts have "local map = item:get_map()" near the top of the script, so, in my item:on_obtained() function, I wrote, "local hero = map:get_hero()" so that I could then obtain the hero's position for knowing where to place the entity. However, when I tested my code by having the hero obtain the item, I got this error message:

"attempt to index upvalue 'map' (a nil value)"

What does this error mean?

EDIT: I fixed the problem by adding, "local map = item:get_map()" to the function.
Development / Question About Using 8-Bit Graphics
January 14, 2024, 06:33:06 PM
If I understand correctly, the default art assets in the game's engine are 16-bit. I'm planning on using 8-bit NES-like art made using Pro Motion (the pixel art tool that the Shovel Knight team used), and I was wondering if there's anything I need to keep in mind in regards to using 8-bit sprites, tiles, etc. in this engine.
Development / How to Receive an Item From an NPC
December 15, 2023, 03:13:11 AM
I watched the tutorial videos on equipment items and I understand how to have an item be found in a chest or dropped when enemy is defeated. However, I was wondering how to have an item be obtained by interacting with NPC.
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:

function entity:swap_sprite()
  -- obtain the hero's current sprite:
  repsprite = hero:get_tunic_sprite_id()
  -- replace the current sprite with resprite:
  sprite = entity:create_sprite(repsprite)

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:

function game:on_command_pressed(item_2)
return true

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:

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()

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

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.

EDIT: I looked at some default examples of the use of similar events, such as on_key_pressed() and I realized my error; I stated "game:on_command_pressed(item_2)" when I should have stated "game:on_command_pressed(command)" and then stated: if command = "item_2"...
Development / How to Change an Entity's Sprite
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.
Development / What does this error mean?
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:

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

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:
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?

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?