[resolved] instance methods?

Started by Poe, January 08, 2012, 04:58:17 am

Previous topic - Next topic

Poe

January 08, 2012, 04:58:17 am Last Edit: January 21, 2012, 03:59:03 am by Poe
in xp, i made a new class just like events and common_events that gets defined in game_map, and there are multiple instances. it seems the variables of all instances can be set to their own seperate values just fine, but when one instance runs a method, all instances stop running methods until that first instance is done. aren't methods and variables essentially the same thing? it was my understanding that each instance of a class has its own set of methods? what am i neglecting to consider here?

Blizzard

January 08, 2012, 06:49:26 am #1 Last Edit: January 08, 2012, 06:52:00 am by Blizzard
A variable contains some form of data while a method is a procedure of commands that does something.
Each instance of a class (which means each actual object created) has their own values of the variables, but they call technically call the same methods. Still, two instances calling the same method still handle different data so you can basically assume that each instances calls "its own version" of the method just to be less confused.

All that code is sequential. If you runs all instances through a loop and call a method, then they are all called one after another. The first one is executed, then the loop continues, then the same method is executed with the next instance.

An alternative how to look at methods is that you consider them simply to be functions that take an implicit "self" argument which is the instance, because in the underlying system that's exactly what's happening.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Poe

January 08, 2012, 09:22:10 am #2 Last Edit: January 08, 2012, 09:39:31 am by Poe
if i understand correctly, you're saying the second instance only calls the same method after the first instance (or object if that's a better term) is finished with it, even though they call the same method seperately? but events run methods simultaneously don't they? two events can start a move_route for example at the same time. or does it only seem that way?

to be concrete: the class is an AI sorta thing and runs on top of existing events. one of the definitions calls a seperate pathfinding script, then runs a delay method in itself until the move_route is completed. but when a second object tries to do this while a first is already running, the first one stops and waits until the more recent call is finished, and then resumes. yet if i call the pathfinding script outside of the class, they both seem to run at the same time. or is that because there's no delay method involved?

if all is sequential, how would you ever delay a script that has multiple instances?O_O

Blizzard

Quote from: Poe on January 08, 2012, 09:22:10 am
if i understand correctly, you're saying the second instance only calls the same method after the first instance (or object if that's a better term) is finished with it, even though they call the same method seperately?


Yes.

Quote from: Poe on January 08, 2012, 09:22:10 am
but events run methods simultaneously don't they? two events can start a move_route for example at the same time. or does it only seem that way?


It only seems that way. During the update of one frame (usually between two Graphics.update calls) everything is updated sequentially and then all the results are rendered.
There is a borderline case where you can test this. Put two events walking at the same time onto the same tile. The event that gets updated first will be the one that actually gets on that tile while the second one won't be able to walk on that tile as the previous event already "marked" that tile to be impassable.

Quote from: Poe on January 08, 2012, 09:22:10 am
to be concrete: the class is an AI sorta thing and runs on top of existing events. one of the definitions calls a seperate pathfinding script, then runs a delay method in itself until the move_route is completed. but when a second object tries to do this while a first is already running, the first one stops and waits until the more recent call is finished, and then resumes. yet if i call the pathfinding script outside of the class, they both seem to run at the same time. or is that because there's no delay method involved?


It's because there is no delay. RMXP has an event command for delaying movement exactly for that purpose. When you run a move route and then another one right after, the second one will override the first one if there is no delay in between. Yours is basically a similar case.

Quote from: Poe on January 08, 2012, 09:22:10 am
if all is sequential, how would you ever delay a script that has multiple instances?O_O


What exactly do you mean? Usually people implemented some kind of internal delay mechanism or just override the old commands.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Poe

well, as long as the delay method is being run, another instance can't delay because the method is still in use right? so basically if one instance starts delaying, every other instance runs aground until it's done.

Poe

January 09, 2012, 04:10:27 pm #5 Last Edit: January 09, 2012, 04:11:43 pm by Poe
i've tried making update call the delay method for 1 frame, while its wait count is > 0, but that just gave incredible lag and still doesn't allow the other instance to access any methods. which makes sense i guess. i've made some attempt without the frames.times{} to no avail, it's always going to need a frame of waiting or the code will resume. i've checked how wait time in events is done but that way it still can't delay scripts mid-code AND let another instance run some other method.

i'd appreciate any insights on delaying methods you could offer.:)

Blizzard

I'm really not sure anymore what you are trying to do. xD
If I understand right, you don't want the whole code to run at once. Then simply make it work in steps. e.g. Let it calculate 20 nodes in one step, no more and store the result. in the next frame, it will calculate another 20 nodes and so on. This is how I did it in Blizz-ABS to reduce lag from path finding.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

ForeverZer0

Graphics.update


40.times { Graphics.update } # Delay for one second


This will effectively freeze things for a few seconds. You can also call things like the map update, etc. so that things don't freeze like that, but it asking for bugs.  In all reality, the fact that you require such a thing likely means that you need to back to the drawing board on your method, and make it work differently.
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

Poe

i was beginning to think that. :facepalm:

thanks guys.

Poe

another question, but still on topic:

can ANYTHING happen simultaneously? like methods from different classes, modules...

for example would there a point in speed/efficiency/accessibility to making a seperate class to update AI's, as opposed to running it all as part of Game_Map update?

Blizzard

No. In reality all these things happen sequentially.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Poe