Trouble with create NPC

Ask and answer questions about making games and related topics. Unrelated topics go in that other forum.

Moderators: marionline, SDHawk

Post Reply
User avatar
sheamkennedy
Liquid Metal Slime
Posts: 1110
Joined: Mon Sep 16, 2013 9:29 pm
Location: Tama-shi, Tokyo, Japan
Contact:

Trouble with create NPC

Post by sheamkennedy »

In my game I have 3 NPCs each of which is a heart (health) for my character in the game. When the character gets shot these NPCs disappear one by one. When none remain and the character is shot then they die. This all works fine.

I now am attempting to make a HealthUp item. When you step on it, it replenishes one health. Since my existing script checks how many heart NPCs remain to determine if you are dead I figured I could just create more heart NPCs to take the place of lost ones. My scripts are as follows:

Code: Select all

script,healthUp,begin
  if((NPC X(0) == NPC X(18)) && (NPC Y(0) == NPC Y(18))) then(
    play sound (9, false, true)
    recoverHealth1
    embiggen now
    destroy NPC (18)
    
  ) else if ((NPC X(3) == NPC X(18)) && (NPC Y(3) == NPC Y(18))) then(
    play sound (9, false, true)
    recoverHealth2
    embiggen now
    destroy NPC (18)
  )  
end

script, health1, begin
  if (get NPC ID (21) > 0) then (
    destroy NPC (21)
    play sound (10, false)
  ) else if (get NPC ID (20) > 0) then (
    destroy NPC (20)
    play sound (10, false)
  ) else if (get NPC ID (19) > 0) then (
    destroy NPC (19)
    play sound (10, false)
  )        
end 

script, recoverHealth1, begin
  if (get NPC ID (19) == 0) then (
    create NPC (19, 0, 9, down)
    #play sound (11, false)
  ) else if (get NPC ID (20) == 0) then (
    create NPC (20, 1, 9, down)
    #play sound (11, false)
  ) else if (get NPC ID (21) > 0) then (
    create NPC (21, 2, 9, down)
    #play sound (11, false)
  )        
end  

The HealthUp script is being called in an autorun while loop every tick to see if it's conditions are true. In my game the Health items make a sound when picked up but no heart NPC is created, nor does the player last longer. Is there something I'm missing here?
⊕ P E R S O N A L M U S I C: https://open.spotify.com/album/6fEo3fCm5C3XhtFRflfANr
� C O L L A B M U S I C: https://dustpuppets.bandcamp.com/releases
User avatar
Mogri
Super Slime
Posts: 4668
Joined: Mon Oct 15, 2007 6:38 pm
Location: Austin, TX
Contact:

Post by Mogri »

Glib response: Use slices for this instead.

Less glib response: What are NPCs 0 and 3? What's recoverHealth2? Would it make more sense to change the NPCs into empty hearts instead of destroying them?

Actual response: You should really be using slices for UI/HUD elements. It's reasonably easy to do, too. Let's suppose you're using walkabout set 13 for your heart graphic. The script would look like this:

Code: Select all

global variable, begin
  1, heart 1
  2, heart 2
  3, heart 3
  4, life
end

# Somewhere at the start of the game, you want to set life to 3. This is a much better method than figuring out how many NPCs are on the screen.

script, initialize health bar, begin
   # Call this script once, at the start of your game
   heart 1 := load walkabout sprite(13)
   heart 2 := load walkabout sprite(13)
   heart 3 := load walkabout sprite(13)
   # Place the sprites on the screen
   put sprite(heart 1, 5, 5)
   put sprite(heart 2, 25, 5)
   put sprite(heart 3, 45, 5)
end

script, gain health, begin
   if &#40;life < 3&#41; then &#40;life &#58;= life + 1&#41;
   update health bar
end

script, lose health, begin
   life &#58;= life - 1
   update health bar
   if &#40;life <= 0&#41; then &#40;something&#41; # do your death handling here
end

script, update health bar, begin
  if &#40;life < 1&#41; then &#40;set slice visible&#40;heart 1, false&#41;&#41; else &#40;set slice visible&#40;heart 1, true&#41;&#41;
  if &#40;life < 2&#41; then &#40;set slice visible&#40;heart 2, false&#41;&#41; else &#40;set slice visible&#40;heart 2, true&#41;&#41;
  if &#40;life < 3&#41; then &#40;set slice visible&#40;heart 3, false&#41;&#41; else &#40;set slice visible&#40;heart 3, true&#41;&#41;
end
I could write this more elegantly, but this should be easy to understand. If you have any questions on what's going on here, please ask!
Last edited by Mogri on Fri Feb 20, 2015 8:59 pm, edited 1 time in total.
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

In addition, the following makes no sense:

Code: Select all

if &#40;get NPC ID &#40;19&#41; == 0&#41; then &#40; 
get NPC ID takes an npc reference as an argument, and returns the ID number of that NPC, or -1 if the reference is invalid. 19 isn't a valid npc reference. NPC references are always returned by commands (like NPC at spot), never hard-coded into your script. Maybe you meant to use "NPC copy count".
Last edited by TMC on Sat Feb 21, 2015 2:08 am, edited 1 time in total.
User avatar
sheamkennedy
Liquid Metal Slime
Posts: 1110
Joined: Mon Sep 16, 2013 9:29 pm
Location: Tama-shi, Tokyo, Japan
Contact:

Post by sheamkennedy »

Thanks both of you. I'll keep you're concept in mind next time Mogri, it seems a lot easier. Thanks to TMC I was able to see that my code could work if I simply replaced:

(get NPC ID (19) == 0)

with

(get NPC ID (19) == -1)

so I did that for sake of ease.
⊕ P E R S O N A L M U S I C: https://open.spotify.com/album/6fEo3fCm5C3XhtFRflfANr
� C O L L A B M U S I C: https://dustpuppets.bandcamp.com/releases
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

Frankly, it's an engine bug that "get NPC ID (19) == -1" works (though we shouldn't change it now). I would write "npc copy count (19) == 0" instead; that's much clearer.
Post Reply