Author Topic: [Solved] SCI0 - multiple actors using same changeState  (Read 29297 times)

0 Members and 1 Guest are viewing this topic.

Offline Doan Sephim

[Solved] SCI0 - multiple actors using same changeState
« on: July 02, 2025, 11:48:25 PM »
I am trying to trim some code (for heap purposes) and I have 2 actors who rely on the same base coding for their behaviors, but I have two separate changeStates that operate essentially the same (with some minor difference like x and y positions).
I have had one hell of a time trying to figure out how to make 2 (or more) actors use the same changeState (but at different times).
Is this possible?
My idea was to create a class of Script with a changeState method, to set the script to the actors, but whenever I call the actor's script, it crashes the game.
I'm trying to call it with the following: ((actor1 script?) changeState: 1)
SCI doesn't like that at all! makes the whole program crap its pants and die!
Any insight would be greatly appreciated.
« Last Edit: July 03, 2025, 11:57:21 PM by Doan Sephim »


Artificial Intelligence Competition

Offline lskovlun

Re: SCI0 - multiple actors using same changeState
« Reply #1 on: July 03, 2025, 01:57:37 AM »
Simple. You define your script instance, and then you do:

(actor1 setScript: (myScript new:))

and

(actor2 setScript: (myScript new:))

The actor that a script is attached to is called its client. Note that the script must access its client through the client property exclusively. The final thing to remember is that a script that is used this way must do (self dispose:) in its final state, otherwise you'll leak memory. You may have to add a new final state in which to do that.
« Last Edit: July 03, 2025, 02:15:20 AM by lskovlun »

Offline Doan Sephim

Re: SCI0 - multiple actors using same changeState
« Reply #2 on: July 03, 2025, 08:05:53 AM »
Okay. I have set up a basic script to have a couple actors walking back and forth. If I understand correctly, I cannot manually call an actor to a specific state, but everything has to be accounted for in the Script that the the actors of clients of.
Here's what I have - but after looping through the states twice, the program crashes with "Oops" message.
I tried to keep it as barebones as I could for simplicity, perhaps I left out something important?
Code: [Select]
(instance DeadwoodScript of Script
    (method (changeState newState)
        (= state newState)
        (switch state
            (0
            (if (> (client x?) 139)
            (client setMotion: MoveTo (- (client x?) 60) (client y?) self)
            else
            (self cue:)
)
            )
            (1 (= cycles 10)
                (client setMotion: 0)
            )
            (2
            (if (< (client x?) 120)
            (client setMotion: MoveTo (+ (client x?) 60) (client y?) self)
            else
            (self cue:)
)   
)
            (3 (= cycles 10)
                (client setMotion: 0)
            )
            (4
            (self changeState: 0)
                (self dispose:)
            )
        )
    )
)

Offline lskovlun

Re: SCI0 - multiple actors using same changeState
« Reply #3 on: July 03, 2025, 08:28:53 AM »
OK, so the script is not supposed to finish. In that case, you shouldn't dispose the script until a room change. Add it to (yourRoom newRoom:) instead. Something like:
Code: [Select]
(method (newRoom)
(myScript1 dispose:)
(myScript2 dispose:)
(super newRoom: &rest)
)
or maybe even:
Code: [Select]
(method (newRoom)
((myActor1 script:) dispose:)
((myActor2 script:) dispose:)
(super newRoom: &rest)
)

Offline Doan Sephim

Re: SCI0 - multiple actors using same changeState
« Reply #4 on: July 03, 2025, 08:32:00 AM »
Okay, got it. So don't dispose until later since I'm continually using the script while in the room. Thank you, this has been very helpful!

Offline Doan Sephim

Re: SCI0 - multiple actors using same changeState
« Reply #5 on: July 03, 2025, 08:49:18 AM »
OK, so the script is not supposed to finish. In that case, you shouldn't dispose the script until a room change. Add it to (yourRoom newRoom:) instead. Something like:
Code: [Select]
(method (newRoom)
(myScript1 dispose:)
(myScript2 dispose:)
(super newRoom: &rest)
)
or maybe even:
Code: [Select]
(method (newRoom)
((myActor1 script:) dispose:)
((myActor2 script:) dispose:)
(super newRoom: &rest)
)
Okay, I tested both of these (and none at all) and ran a memory check in the next adjacent room. In every instance the heap memory was the same. Do you suppose the global disposal cleanup is sufficient and these extra methods are superfluous, or am I simply not using them properly and mininterpreting the data?
For clarity, I had tested them in the "instance rm273 of Rm" where I think they belong.
Thanks for troubleshooting with me.

Offline lskovlun

Re: SCI0 - multiple actors using same changeState
« Reply #6 on: July 03, 2025, 05:40:00 PM »
I may be reading the SCI template the wrong way, but as I understood it, dispose: would be called on room scripts and some others, but not on actors. If you have actually tested this, I'm fine with it.

Offline Doan Sephim

Re: [Solved] SCI0 - multiple actors using same changeState
« Reply #7 on: July 03, 2025, 11:58:08 PM »
Sounds good. It certainly doesn't hurt to keep it in, either way, just to be on the safe side.


SMF 2.0.19 | SMF © 2021, Simple Machines
Simple Audio Video Embedder

Page created in 0.048 seconds with 21 queries.