Alice Community  

Go Back   Alice Community > Alice 2 > How do I...?

Reply
 
Thread Tools Display Modes
Question with memory
Old
thatCarpenterGuy
Junior Member
 
Status: Offline
Posts: 2
Join Date: Jun 2017
Location: MA
Default Question with memory - 06-14-2017, 07:41 PM

Hello everyone!

I'm working on a game for school and have noticed that I am starting to get some lag, and fear I'm using way too much memory.

My game has a player that constantly moves forward. Along the path they encounter obstacles in the form of various animations. For example, there are dogs pacing back and forth, cars moving, people walking, etc.

As of now I have the various obstacle's individual animation methods in a do together block
Example:

world.playAnimations
do together
car1.animate
car2.animate
walker.animate
dog.animate
etc
Each of these individual methods then looks like this:

While world.playing==true
test if player is within 15 meters car1
then car1 move forward at speed

In an attempt to make every play unique, all of the animations are currently using a select-random-number-between to determine the distance away from the player to begin moving and the object's speed itself.

I'm thinking that this is likely not the proper or most efficient way to tackle this and I'm afraid of making my code any messier than it probable already is. Any suggestions on a better way to handle this? Will removing the random number selections make a difference?

Also on a different but similar note, is there a decent strategy for collision detection using arrays instead of lists? For instance I have a lot of stationary items that need collision detection as well. I have broken them down into organized lists and am using the for all items together code.

Hopefully this makes some sense

Last edited by thatCarpenterGuy; 06-14-2017 at 07:43 PM.
   
Reply With Quote
Old
chickentree
Super Moderator
 
Status: Offline
Posts: 250
Join Date: Dec 2012
Location: Frosno, Ca
Default 06-25-2017, 10:35 PM

Quote:
Originally Posted by thatCarpenterGuy View Post
Hello everyone!

I'm working on a game for school and have noticed that I am starting to get some lag, and fear I'm using way too much memory.

My game has a player that constantly moves forward. Along the path they encounter obstacles in the form of various animations. For example, there are dogs pacing back and forth, cars moving, people walking, etc.

As of now I have the various obstacle's individual animation methods in a do together block
Example:

world.playAnimations
do together
car1.animate
car2.animate
walker.animate
dog.animate
etc
Each of these individual methods then looks like this:

While world.playing==true
test if player is within 15 meters car1
then car1 move forward at speed

In an attempt to make every play unique, all of the animations are currently using a select-random-number-between to determine the distance away from the player to begin moving and the object's speed itself.

I'm thinking that this is likely not the proper or most efficient way to tackle this and I'm afraid of making my code any messier than it probable already is. Any suggestions on a better way to handle this? Will removing the random number selections make a difference?

Also on a different but similar note, is there a decent strategy for collision detection using arrays instead of lists? For instance I have a lot of stationary items that need collision detection as well. I have broken them down into organized lists and am using the for all items together code.

Hopefully this makes some sense
If I understand what you are doing a much better way would be to use events in Alice.
Events have less overhead than a method in Alice and they automagically loop. So don't put an infinite loop in an event handler, don't put any loops in the handler if you can avoid it.

My general blerb for events:
Events:
  1. Events run, essentially continuously, in the background.
    1. Alice is able to schedule and react to events much better than it can to ordinary user methods. This means a movie performs much better using events than if infinite loops are used in a method.
    2. When an infinite loop is encountered in a method it essentially blocks you from using other methods until the loop exits, which it never does. Yes, a “Do together” can be used to get around this somewhat, but Alice's performance goes way down.
  2. If the event condition, or it's handler method, is more than a couple of lines, write a
    separate world method and call it from the event.
    1. Using descriptive names for functions and methods it makes the program flow much easier to understand.
  3. Know your tools, there are several events besides the user input type that are available to you as the programmer and can make problems easier to solve.
    1. Right click on an event and see if the pop-up menu includes a “change to” option.
    2. When the world starts, do“ can be changed to “While the world is running, do“ which gives you the same functionality as using “While true“ in a method but with much better performance.
    3. Changing the “When a key is pressed, do“ to “While a key is pressed, do“ calls its method repeatedly as long as the key is held down rather than only running when the key is released.
    4. Look for this option in other events!
  4. Don't freak out about Events that have Before, During and After “Slots”.
    1. You do not have to use them all. In fact you don't have to use any of them, but then the event becomes kind of pointless.
    2. Usually only the During slot has something in it. But the others can be handy at times.
    3. During translates to “as long as this event's condition is true, do
  5. Events like While something is true and When a variable changes give you as the programmer a lot of control. In your car example you could use the While event to determine when the car should start moving.
  6. Event caveats:
    1. Any functions or methods called from an event should be as simple as possible so that Alice can complete them quickly and continue.
    2. Treat the event method as the inside of your loop.
      1. The only things you want in an event method are the actions that would have been done once each time your old “While true“ construct looped.
    3. Alice will take care of doing these actions over and over for you as long as the event's condition is true.
Never forget the cardinal rule of computer programming:
All programming environments are passive-aggressive."


Mark Henwood
mhenwood@ieee.org
   
Reply With Quote
Old
thatCarpenterGuy
Junior Member
 
Status: Offline
Posts: 2
Join Date: Jun 2017
Location: MA
Default 06-27-2017, 06:16 PM

Thanks for the response Mark.

I appreciate the wealth of knowledge you shared. I have already submitted my game for school. However, I will remember the tips you shared, especially the priority the event handler receives. I wish I had known this before.

The good news is that I was able to simplify much of my code. I also replicated similar objects by moving them to dummy locations and deleted any objects that I didn't deem necessary. The combination of these really made a drastic improvement in the performance of my game. I will be posting it over in the share your world section, so head on over and check it out! Paperboy3D!
   
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



Copyright ©2024, Carnegie Mellon University
Alice 2.x © 1999-2012, Alice 3.x © 2008-2012, Carnegie Mellon University. All rights reserved.