Please login or register.

Login with username, password and session length
Advanced search  

News:

This forum provides RSS feed. To query recent posts use this url. More...


Author Topic: loops unloading actors on scene change?  (Read 2837 times)

0 Members and 1 Guest are viewing this topic.

Diaf

  • Lurker
  • *
  • Karma: 0
  • Offline Offline
  • Posts: 21
    • View Profile
loops unloading actors on scene change?
« on: May 31, 2009, 12:15:09 PM »

In the game I am doing has an actor that follows around the main actor Sam and Max style.

I did a search on the forums and found some cool code in which you create an infinite loop in the second actors script in which it checks for a true boolean variable in an overwritten GoTo method in the first actor's script. If it finds it, it sets that boolean value to false, checks if the second actor is already walking and stops it if it is. It finally calls its own GoTo method with the first actor's targetX and targetY as parameters and then sleeps for 100ms.

This was working great as the second actor would move with the first (as opposed to waiting for the first to finish moving before setting off).

But then I added a second scene. Then whenever I changed to that scene (or back again for that matter) the second actor would appear where I SkipTo()ed him but would not follow anymore.

After about an hour pouring over the code to find what I did wrong I noticed that the actor\secondactor was missing from the top left corner of the debugging window. Seemingly when I changed scene the game unloaded the second actor and its scripts. I messed around a bit more and found that taking out the loop kept everything fine.

Is this a bug, is there some condition I should be setting on a loop to keep it safe during scene changes or is there a better way of having a second actor follow the first where I wont encounter this problem?
Logged

Diaf

  • Lurker
  • *
  • Karma: 0
  • Offline Offline
  • Posts: 21
    • View Profile
Re: loops unloading actors on scene change?
« Reply #1 on: May 31, 2009, 03:20:42 PM »

The temporary way I have gotten around this is to create a new actor called Loopy with a blank .X file with the scale set to 0 and the infinite loop it its script file. I have loaded it into a variable called LoopyActor.
Whenever the ChangeScene() function is called I call Game.UnloadObject(LoopyActor) so it doesn't get destroyed in the change and then call Game.LoadActor3D("actors/Loopy/Loopy.act3d") in the scene_init script of the new scene.

So far this works, but has to be crazily inefficiant. Hopefully there is a better way than this.
Logged

Diaf

  • Lurker
  • *
  • Karma: 0
  • Offline Offline
  • Posts: 21
    • View Profile
Re: loops unloading actors on scene change?
« Reply #2 on: May 31, 2009, 04:07:44 PM »

After some more scripting I found the problem pop up somewhere else when I called the GoTo method of the second actor.

It seems like any method that acts of the location of the second actor causes whatever actor the while loop is in to get removed from running memory.
Logged

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 41
  • Offline Offline
  • Gender: Male
  • Posts: 5683
    • View Profile
    • Dead:Code Site
Re: loops unloading actors on scene change?
« Reply #3 on: June 01, 2009, 03:50:49 PM »

The important thing here is, that if some script is waiting for your actor to finish some lengthy operation (such as actor.GoTo()), and in the meantime other script is requesting a lengthy operation on the same actor, the original script is terminated.
For that reason, you should move the automatic go-to code to a separate "thread", so that only the thread is terminated and the main script keeps running.

Take a look at this article: http://res.dead-code.org/doku.php/resource:triggering_actions_when_actor_is_idle
It demonstrates the concept nicely. The main part of the script never executes the Talk or GoTo commands. Instead it just invokes an event handler (on "idle"), which does the dirty job. If someone else asks the actor to walk or talk, only the event handler is killed and the main script contines to run.
Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave
 

Page created in 0.126 seconds with 24 queries.