Alice Community

Alice Community (http://www.alice.org/community/index.php)
-   How do I...? (http://www.alice.org/community/forumdisplay.php?f=16)
-   -   Question with memory (http://www.alice.org/community/showthread.php?t=11733)

thatCarpenterGuy 06-14-2017 07:41 PM

Question with memory
 
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 :confused:

chickentree 06-25-2017 10:35 PM

[QUOTE=thatCarpenterGuy;57565]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 :confused:[/QUOTE]

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:[LIST=1][*]Events run, essentially continuously, in the background. [LIST=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.[*]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 “[B]Do together[/B]” can be used to get around this somewhat, but Alice's performance goes way down.[/LIST][*]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. [LIST=1][*]Using descriptive names for functions and methods it makes the program flow much easier to understand.[/LIST][*]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.[LIST=1] [*]Right click on an event and see if the pop-up menu includes a “[B]change to[/B]” option. [*]“[B]When the world starts, do[/B]“ can be changed to “[B]While the world is running, do[/B]“ which gives you the same functionality as using “[B]While true[/B]“ in a method but with much better performance. [*]Changing the “[B]When a key is pressed, do[/B]“ to “[B]While a key is pressed, do[/B]“ calls its method repeatedly as long as the key is held down rather than only running when the key is released.[*]Look for this option in other events![/LIST][*]Don't freak out about Events that have Before, During and After “Slots”.[LIST=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. [*]Usually only the [B]During[/B] slot has something in it. But the others can be handy at times.[*]During translates to “[B]as long as this event's condition is true, do[/B][/LIST][*]Events like [B]While something is true [/B] and [B]When a variable changes[/B] 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.[*]Event caveats:[LIST=1][*]Any functions or methods called from an event should be as simple as possible so that Alice can complete them quickly and continue. [*]Treat the event method as the inside of your loop. [LIST=1][*]The only things you want in an event method are the actions that would have been done once each time your old “[B]While true[/B]“ construct looped. [/LIST][*]Alice will take care of doing these actions over and over for you as long as the event's condition is true.[/LIST][/LIST]Never forget the cardinal rule of computer programming:
“[U]All programming environments are passive-aggressive.[/U]"

thatCarpenterGuy 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!


All times are GMT -5. The time now is 07:05 PM.

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