Freeing Olders Slice?

Make games! Discuss those games here.

Moderators: Bob the Hamster, 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:

Freeing Olders Slice?

Post by sheamkennedy »

In my game, I currently have a slug-like character whom has a trail of slime which forms behind him as he walks. I have accomplished this using an autorun script which places "slime" slices at the characters position every tick.

What I want to do is free the oldest slime slice once the slugs slime trail reaches about 20 slices... This is what I've tried so far, but it does not seem to work:

Code: Select all

  if (slice is valid (slime) && count >= 20) then(
    free slice (first child (slime))
    count := 0
  )
count := count + 1
  
I have a counter in a while loop to count whether the amount of slimes is exceeded. Unfortunately I can't figure out how to properly free the oldest slime slice. I'm assuming what I currently have doesn't work because the "first child" is deleted once, but cannot be deleted the next time around. Or perhaps I'm just using the wrong function. Hopefully someone can shine some light on this.
Last edited by sheamkennedy on Fri Apr 25, 2014 12:26 am, edited 1 time in total.
⊕ 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
Gizmog
Metal King Slime
Posts: 2615
Joined: Tue Feb 19, 2008 5:41 am

Post by Gizmog »

I could be wrong, but Count := 0 looks like the problem to me. The way I read it, that means it's gonna create a slice each step or tick, 1,2,3..18,19, 20! Stop, free the oldest slice! And then reset the counter to 0 and start counting to 20 again, so at the end of 40 ticks you're gonna have 38 slices, at the end of 60 you're gonna have 57 and so on. If you're doing this in a while loop, that means that in... like 7 or 8 seconds you're gonna be over 100 slices and it'd be pretty hard to notice one or two missing.

You might try the Child Count command, and get rid of that Count := 0 stuff so that it's always deleting the 20th slime. [/url]
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 »

Gizmog wrote:I could be wrong, but Count := 0 looks like the problem to me. The way I read it, that means it's gonna create a slice each step or tick, 1,2,3..18,19, 20! Stop, free the oldest slice! And then reset the counter to 0 and start counting to 20 again, so at the end of 40 ticks you're gonna have 38 slices, at the end of 60 you're gonna have 57 and so on. If you're doing this in a while loop, that means that in... like 7 or 8 seconds you're gonna be over 100 slices and it'd be pretty hard to notice one or two missing.

You might try the Child Count command, and get rid of that Count := 0 stuff so that it's always deleting the 20th slime. [/url]
Good point about removing the count := 0

I'm still having trouble understanding child count. I rewrote the script the way in which I thought it would work in the while loop:

Code: Select all

  if (slice is valid (slime) && count >= 10) then(
    free slice (child count(slime))
  )
count := count + 1
The way I understand this is:
-the while loop ignores this "if statement" until count is >= 10.
-once count is >=10 (meaning 10 slime slices are trailing behind the slug), the slime slice child at child count should be destroyed.
-child count will equal 10 the first time around, thus child 10 should be destroyed.

I am still getting an error saying "freeslice: invalid slice handle" perhaps you or someone else might know the problem.
⊕ 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
Gizmog
Metal King Slime
Posts: 2615
Joined: Tue Feb 19, 2008 5:41 am

Post by Gizmog »

What I was thinking was more like...

Code: Select all

Count := ChildCount (Slime)

if (Count >= 10)
then (
        freeslice (FirstChild (Slime))
       )

The invalid slice handle is, presumably, because in your latest example, FreeSlice (ChildCount) is going to return the number of children the slice has, which isn't a slice handle (but might accidentally coincide with one). As for why it would be doing that before? I have no idea, we'd probably need to see the part of the script where you load your slices to determine that.
Last edited by Gizmog on Fri Apr 25, 2014 1:22 pm, edited 2 times in total.
User avatar
sotrain515
Red Slime
Posts: 59
Joined: Wed Dec 26, 2012 3:23 pm

Post by sotrain515 »

I would also add that you may be having a problem depending on what the variable "slime" refers to in your original script. Since you are using "first child (slime)" and now "child count (slime)" that implies that slime refers to some kind of container to which you are parenting a bunch of slime sprite children. Is this accurate?

From how you are describing it, I would have guessed you would have been parenting these slime sprites to a map layer. That way they would appear underneath the slug as they generate.

If that is the case, you would want to modify your scripts to point at e.g. "first child (lookup slice(sl:map layer4))" which would interact with the slice children of map layer 4 (just an example, you would want to choose whichever layer is "right underneath" your heroes/npcs).
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 »

sotrain515 wrote:If that is the case, you would want to modify your scripts to point at e.g. "first child (lookup slice(sl:map layer4))" which would interact with the slice children of map layer 4 (just an example, you would want to choose whichever layer is "right underneath" your heroes/npcs).
I'm still having trouble... You are right, I have the slime slices being placed below the character on mapr layer 0. Here's the full script so everyone can get a better look at what I may be doing wrong.

Code: Select all

plotscript, Map Auto Run 4, begin

variable(collection, sl, slime, slParent, count, sliceNum)

count := 0

While ( current map == current map ) do(

  collection := load slice collection(0)
  slParent := lookup slice(sl:map layer0)
  sliceNum := 1
  sl := lookup slice(sliceNum, collection)
  slime := lookup slice(2, collection)
  
    #This creates a ring of light around the slug
	put slice (sl, camera pixel x, camera pixel y)
	#This creates a trail of mucus behind the slug
	put slice (slime, camera pixel x, camera pixel y)
	set parent (sl, slParent)
	set parent (slime, slParent)
	move slice above (slime, sl) 
    wait
    free slice (sl)

    #This checks if the amount of mucus slices exceeds 10, and frees the exceeding slice
    count := child count(slime)
    if (count >= 10) then(
      free slice (first child (lookup slice(sl:map layer0)))  
    )
)
end
⊕ 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
sotrain515
Red Slime
Posts: 59
Joined: Wed Dec 26, 2012 3:23 pm

Post by sotrain515 »

Okay, first I'm going to rewrite your code how I would do it and then explain the differences. I'll put a ### before changes I made.

Code: Select all

plotscript, Map Auto Run 4, begin

###
variable(sl, slime, slParent, count)

count := 0

While ( current map == current map ) do(

  slParent := lookup slice(sl:map layer0)

  ###
  sl := load slice collection(whatever)
  slime := load slice collection(whatever+1)
 
  #This creates a ring of light around the slug
  put slice (sl, camera pixel x, camera pixel y)

  #This creates a trail of mucus behind the slug
  put slice (slime, camera pixel x, camera pixel y)
  set parent (sl, slParent)
  set parent (slime, slParent)

  move slice above (slime, sl)
  wait
  free slice (sl)

  #This checks if the amount of mucus slices exceeds 10, and frees the exceeding slice
  ###
  count := child count(slParent)
  if (count >= 10) then(
     free slice (first child (slParent)) 
  )
)
end
Okay, so I eliminated the "collection" and "sliceNum" variables. Why did I do this? Well, you were using the collection in an interesting way, basically loading it and then splitting it into its component parts (a ring of light and a slime trail) and parenting each of those separately to the map layer. This is fine EXCEPT that you are actually abandoning an invisible slice container each time you do this. It may not actually matter that much but these things will never be garbage collected (until a map change I believe) and it would be better to do without them.

Thus I changed your code to load two separate slice collections (that you will now have to create... sorry about that) called "whatever" and "whatever+1" in the code that represent the ring of light and slime respectively. If these were just individual sprites, you can replace "load slice collection" with "load walkabout sprite" and/or "load large enemy sprite" or whatever rather than creating new collections.

The only other change I made was basically changing your "count := child count(slime)" to "count := child count(slParent)", slParent here referring to your chosen map layer.

Something to keep in mind is that this code will have to be modified if other stuff is being parented to map layer 0 at any time since that would of course change the slice children count and/or could result in non-slime-slices getting deleted.

Oh and disclaimer: I wrote all this stuff freehand without my compiler handy so you may have to clean up my syntax somewhat. I'm generally a "write it and let the compiler do the debugging" kind of programmer....
TMC
Metal King Slime
Posts: 4101
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

Yes, generally best to parent everything to a dedicated container slice which is itself parented to map layer 0 so that your scripts don't go haywire if you add any other slices.

"put slice (sl, camera pixel x, camera pixel y)" isn't right; this will place the slice at the top left corner of the screen. You want something like
put slice (sl, hero pixel x(me), hero pixel y(me))

Also, if you use a door and change to a different map then the "free slice (sl)" line will cause a script error or misbehave. Either rearrange your script so that the wait is at the bottom of the while loop, or add a line after the while like so:
if (current map == map:whatever) then (break)
User avatar
sotrain515
Red Slime
Posts: 59
Joined: Wed Dec 26, 2012 3:23 pm

Post by sotrain515 »

TMC wrote:"put slice (sl, camera pixel x, camera pixel y)" isn't right; this will place the slice at the top left corner of the screen. You want something like
put slice (sl, hero pixel x(me), hero pixel y(me))
This occurred to me later on but that must be why he is using a slice collection. If he centered the sprites within the slice collection and then loaded it, the sprites would stay centered on the screen I believe (with the slice collection container's top left corner matching the screen's top left corner).
Last edited by sotrain515 on Mon Apr 28, 2014 3:51 pm, edited 1 time in total.
Post Reply