Log In  

Hello everyone! I've been confused for a long time, on how to make multiple pickups items using the same collision function. I want to use the map editor to place them, but give different values to them for example.
A healing potion: +5 health points
A gold coin: +2 money
Instead of all of them just giving +1. Thank you!

P#139551 2024-01-02 21:02 ( Edited 2024-01-02 21:04)

1

You might be able to do this with just mget(). Mget() returns the sprite number for the current map cell, so you could do something like:

item = mget(map_x,map_y)
if item == 1 then
  -- potion sprite
  health += 5
elseif item == 2 then
  -- coin sprite
  money += 2
end
P#139553 2024-01-02 21:57
1

If you require keeping all of this info in the map and flags, you could utilize some flags to represent numbers (even only in relation to another flag) for example if your 2nd flag is for "health pickup" and your third flag for "money pickup" then you could set aside the remaining flags to represent numeric values when either of those flags is set. As flags are bits, you would need to count in binary essentially.

With Flags

Let's look at some theoretical bitfields to explain this (the flag bitfield corresponds to the flags in the editor, for example
is equivalent to 01000011)

So, if 01000000 is a health pickup and 00100000 is a money pickup, we can treat the last 5 bits as a numeric value:
01000001 would be a health pickup worth 1 health,
01000010 would be worth 2 health,
01000011 would be worth 3 health,
01010000 would be worth 16 health,
00100001 would be worth 1 money,
00100100 would be worth 4 money, etc.
Use a decimal to binary converter if the binary values are confusing to you, and be aware that a max of 11111 (in binary) would mean this works only up to a value of 31.

With Map

If you don't want to bother keeping these values in the flags directly, you could incorporate logic into your collision code based on the tile number encountered:

tile=mget(collision.x, collision.y)
if (tile == 17) player.health+=5

This will, of course, mean all pickups using the same sprite will act the same.

With tables

Personally, I use tables to represent things like this; while you have to set the positions in the table, handle collisions in _update and draw the sprites in _draw, you can easily add all sorts of properties to the item:

pickups={
 {
  name="coin",
  x=10,
  y=10,
  sprite=17,
  health=0,
  money=2
 },
 {
  name="gem",
  x=20,
  y=20,
  sprite=18,
  health=0,
  money=5
 },
 {
  name="potion",
  x=30,
  y=30,
  sprite=27,
  health=5,
  money=0
 }
}

or you can even add a function (that you would call on pickup) to the table for more dynamic behavior:

pickups={
 {
  name="star",
  x=10,
  y=10,
  sprite=10,
  pickup=function()
   player.health+=100
   player.money+=500
   player.sprite=42
   player.iframes+=300
   confetti(player.x, player.y)
  end
 }
}

There are certainly other (and quite possibly better!) ways to do this, or combine the flexibility of tables with the ease of placement using the map editor, so I'm excited to hear other ideas on this as well.

I hope this helps!

P#139552 2024-01-02 22:20 ( Edited 2024-01-02 22:21)

[Please log in to post a comment]