Computer Science Canada

Procedures?!?!?!?!

Author:  Chaoskiller [ Mon Nov 02, 2009 12:36 am ]
Post subject:  Procedures?!?!?!?!

What is it you are trying to achieve?
I am trying to make an rpg with a constantly moving enemy, as well as being able to move and attack.


What is the problem you are having?
I need to know how to make it so that the enemy moves off to the side constantly while the main procedure is working at the same time. (e.g. I want to be able to move and attack at the same time as the enemy moving)

Describe what you have tried to solve this problem
i have created a procedure and even tried getting rid of it but im not sure what to do.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)

Turing:


<procedure EnemyRobot
    Sprite.SetPosition (enemyrobot, robotx, roboty, false)
    Sprite.Show (enemyrobot)
    for n : 1 .. Enemy_robot
        Sprite.Animate (enemyrobot, Enemyrobot ((n div 1) mod Enemy_robot + 1), robotx, roboty, false)
        delay (50)
    end for
end EnemyRobot>



Please specify what version of Turing you are using
4.1.1

Author:  Tony [ Mon Nov 02, 2009 1:15 am ]
Post subject:  Re: Procedures?!?!?!?!

Chaoskiller @ Mon Nov 02, 2009 12:36 am wrote:
... at the same time as ...

In procedural programming, there is no such thing as "at the same time". One thing happens, then another thing happens. If both of those things happen fast enough, then it might seem as if they are happening together.

code:

loop
   move_player()
   move_enemy()
   delay()  % this is the only delay you should have in your program
end loop

Author:  Euphoracle [ Mon Nov 02, 2009 8:04 am ]
Post subject:  RE:Procedures?!?!?!?!

Just out of curiosity, what's the point of dividing an integer by 1?

Author:  Chaoskiller [ Mon Nov 02, 2009 9:56 am ]
Post subject:  Re: RE:Procedures?!?!?!?!

Euphoracle @ Mon Nov 02, 2009 8:04 am wrote:
Just out of curiosity, what's the point of dividing an integer by 1?


I'm not too sure, I dont undertsand everything all i know is it doesnt work without that. Also i think it means that you are dividing the "for" by 1 if i was to make it div 8 the frames would appear faster and skip a lot.

Tony how is it possible to make 2 things happen at once then?
I need a delay in my animation in order for it to look normal so i cant make them happen as fast as possible.

Author:  Superskull85 [ Mon Nov 02, 2009 11:09 am ]
Post subject:  RE:Procedures?!?!?!?!

You could use the concept of threads to do two things at once. However, Turing provides poor support for such concepts, and it is really not needed for what you want to do.

If you expand a bit on what Tony outlined:

Turing:
loop
   move_player()
   if enemy_in_range then
      player_attack (enemy)
   end if

   move_enemy()
   if player_in_range then
      enemy_attack (player)
   end if

   delay()  % this is the only delay you should have in your program
end loop

The execution of the procedures does not happen at the same time. However, if they are efficient enough (ei. not taking too long to execute) than you shouldn't see too much delay between move and attack. Smile

Author:  Euphoracle [ Mon Nov 02, 2009 11:54 am ]
Post subject:  Re: RE:Procedures?!?!?!?!

Chaoskiller @ Mon Nov 02, 2009 9:56 am wrote:
Euphoracle @ Mon Nov 02, 2009 8:04 am wrote:
Just out of curiosity, what's the point of dividing an integer by 1?


I'm not too sure, I dont undertsand everything all i know is it doesnt work without that. Also i think it means that you are dividing the "for" by 1 if i was to make it div 8 the frames would appear faster and skip a lot.

Tony how is it possible to make 2 things happen at once then?
I need a delay in my animation in order for it to look normal so i cant make them happen as fast as possible.


Rather than having your animation in the procedure doing the "thinking" for that particular thing, have a procedure that draws all the animations as another procedure. In fact, you can use this method to sync the animations so frame 1 is animated to frame to in unison with all animations. Think as such:

Turing:

var AnimFrame := 0
var AnimFrameMax := 25
var ..your other vars here.. := whatever

proc EnemyRobots
    %ai stuff

    %movement stuff
    robotx := somevalue
    roboty := somevalue

    %maybe attack, do a 'lil dance
end EnemyRobots

proc Animation
    AnimFrame = (AnimFrame + 1) mod (AnimFrameMax + 1) % keeps it in [0, AnimFrame]
    DrawRobots
    DrawPlayer %write these \/
    DrawSomethingElse
    DrawSomeMoreStuff
end Animation

proc DrawRobots
    Sprite.BlahBlahblah(Blah) %your other sprite stuff excluding the animation
    Sprite.Animate(enemyrobot, Enemyrobot(AnimFrame), robotx, roboty, false)
end DrawRobots

%game loop
loop
    Player %this is your player stuff like movement etc
    EnemyRobots %this is your robot stuff like movement, ai, whatnot

    Animation
    Time.DelaySinceLast(20) %this delay will keep ALL animations at a good speed
end loop


EDIT: I should explain Time.DelaySinceLast; basically, if it takes longer than 1ms to draw everything, it will delay less with a max delay of 20 and a min delay of 0. This makes it look constant by updating the screen every 20 rather than every 20 + processing time (which makes it look mad choppy)

Author:  Chaoskiller [ Mon Nov 02, 2009 4:00 pm ]
Post subject:  RE:Procedures?!?!?!?!

a lil confused about the whole process thing u did but the time delaysincelast is more clear to me now

Author:  Euphoracle [ Mon Nov 02, 2009 10:12 pm ]
Post subject:  RE:Procedures?!?!?!?!

Oh, proc is just shorthand for procedure.

Author:  Chaoskiller [ Mon Nov 02, 2009 11:00 pm ]
Post subject:  Re: RE:Procedures?!?!?!?!

Euphoracle @ Mon Nov 02, 2009 10:12 pm wrote:
Oh, proc is just shorthand for procedure.


ooh omg it makes so much more sense. I was putting in process instead and was getting all these errors lmao Smile. Is this the only way to do 2 things at once?

Author:  Euphoracle [ Tue Nov 03, 2009 10:33 am ]
Post subject:  RE:Procedures?!?!?!?!

Well, you're not doing more than once thing at once, but you are doing everything one step at a time in unison rather than doing one thing, then the next, and the next, etc.

This is the only good way of doing two things at once that doesn't cause problems and inconsistencies.

Author:  Chaoskiller [ Tue Nov 03, 2009 10:51 pm ]
Post subject:  RE:Procedures?!?!?!?!

ugh i hate this its so dumb how u cant do 2 things at once without problems. Or else it would be so easy to do things

Author:  Tony [ Tue Nov 03, 2009 11:11 pm ]
Post subject:  RE:Procedures?!?!?!?!

No, it would be very difficult. See http://en.wikipedia.org/wiki/Concurrency_%28computer_science%29

Author:  Superskull85 [ Tue Nov 03, 2009 11:33 pm ]
Post subject:  Re: RE:Procedures?!?!?!?!

Chaoskiller @ Tue Nov 03, 2009 10:51 pm wrote:
ugh i hate this its so dumb how u cant do 2 things at once without problems. Or else it would be so easy to do things

One question, why do you think you need two processes running at once? It seems to me that all you are doing is moving a sprite around the screen, and, if a certain condition comes true, executing an attack animation immediately after. If you have coded your program efficiently you should be able to create the illusion that the attack and walk animations are happening at the same time.

Author:  Tony [ Wed Nov 04, 2009 12:16 am ]
Post subject:  RE:Procedures?!?!?!?!

If you are to use View.Update it's not even an "illusion", as the entire frame would render only after every element is updated. In such a way, everything would be drawn to screen "at the same time".

Author:  Euphoracle [ Wed Nov 04, 2009 8:55 am ]
Post subject:  Re: RE:Procedures?!?!?!?!

Chaoskiller @ Tue Nov 03, 2009 10:51 pm wrote:
ugh i hate this its so dumb how u cant do 2 things at once without problems. Or else it would be so easy to do things


If I ask you to list the alphabet backwards, forwards, and provide me the first 27 terms of Posted Image, might have been reduced in size. Click Image to view fullscreen....


Can you do it? Well yeah, of course you can, one at a time, step by step. you'd figure out Z, then A, then whatever the first term would be given values for n and x and move to the next step of each problem. The computer has a brain like you do. It's very good at figuring things out, but it can only do one thing at a time. It can't multitask instantaneous, just like we can't, but it can if you do it the way you would yourself. This is why the computer cannot do everything at the same time. Sure, you can write everything up in different threads and have them execute at roughly the same time but therein lies the problem.

Imagine you couldn't control whether you were going to get the first letter of the alphabet second. Let's say you wrote down Z and Y immediately, then A then the first term (going back to my example above). You're out of sync. Now, if you continue at this pace or a regular pace, you'll finish the alphabet first. This is no problem to you but a computer doesn't know what to do when something goes wrong. It won't even know something went wrong. When you ask Turing to do everything at once, it does it in whatever order it wants and it doesn't ensure anything happens first, last, or at all. It's possible that your background image can draw over your "robots" if you ask the computer to manage the order. You really don't want to let the computer decide the order of doing things for you, because if it's doing it wrong it is a complete and utter nightmare to find and fix the bug.

So yes, you can get it to do two things at once, but similar to how your mind works, it will work erratically and probably cough up something you didn't want... and some blood.

Author:  Kharybdis [ Wed Nov 04, 2009 3:06 pm ]
Post subject:  RE:Procedures?!?!?!?!

You can do it with Turing by using Forks but that's just horrible and leads to the problems described above.

Author:  S_Grimm [ Wed Nov 04, 2009 4:33 pm ]
Post subject:  Re: RE:Procedures?!?!?!?!

Kharybdis @ Wed Nov 04, 2009 3:06 pm wrote:
You can do it with Turing by using Forks but that's just horrible and leads to the problems described above.


forks do not allow simultaneous running of two different procedures, which is what he wants.


My advice has already been stated by others, but ill write it here anyway:

code:

loop

movement of player

movement of enemy

attack of player

attack of enemy

draw

endloop %only loop in your program


everything that happens BEFORE the draw command will seem to be simultaneously executed on the screen. so for all intents and purposes, it will happen simultaneously. besides, with a high FPS, you will never notice the difference in the time it takes the computer to process the commands.

Author:  Chaoskiller [ Fri Nov 06, 2009 4:05 pm ]
Post subject:  RE:Procedures?!?!?!?!

hmm ill try what u wee doing although what i was trying to get at is. I want an enemy to patrol in the background off to the side while the player is inputting attacks and movements. When i do it the enemy does its move and then once its finished i am able to input something.

Author:  Tony [ Fri Nov 06, 2009 4:13 pm ]
Post subject:  RE:Procedures?!?!?!?!

If you are asking about non-blocking IO, then use Input.KeyDown

Author:  Chaoskiller [ Fri Nov 06, 2009 5:28 pm ]
Post subject:  RE:Procedures?!?!?!?!

I already am using Input.Keydown for the player to move the sprite around and attack with him. But i still dont know how to make an enemy move at the same time as the player inputting something

Author:  Tony [ Fri Nov 06, 2009 5:43 pm ]
Post subject:  RE:Procedures?!?!?!?!

But if you are using Input.KeyDown, I don't understand how that is a problem that you are still having. I think it's time for you to post some new code.

Author:  Chaoskiller [ Fri Nov 06, 2009 6:01 pm ]
Post subject:  RE:Procedures?!?!?!?!

Hmm ok well just letting you know my knowledge of this program is limited because im only half way through a semester in computer science. Although i am very far ahead of my class because of these tutorials on here. ok well i think its because i have my Input.KeyDown in a loop. After the Input.KeyDown commands it does a procedure which animates a sprite. I think the problem is it waits for the procedure to be done before continueing with the loop therefore not letting me to input something while the procedure is running

Author:  Tony [ Fri Nov 06, 2009 6:09 pm ]
Post subject:  RE:Procedures?!?!?!?!

You've starting to get the right idea. If you have loops inside of your procedures, it becomes Blocking, which makes everything else stop and wait.

To fix the problem, you'd have to remove that loop.

You already have one "main loop", as shown in all suggested code samples above. You could use that in place of adding more loops. For example, instead of having a procedure with a loop that runs for 100 steps, you rewrite the procedure to perform just 1 step, and then call that procedure for the next 100 steps of the main loop.

It might take some thought to figure this setup out.

Author:  Chaoskiller [ Fri Nov 06, 2009 6:11 pm ]
Post subject:  RE:Procedures?!?!?!?!

ooh ok hmm is there any way i can send you only you my script because its a lot to put on here. Also i need advice on how to easily manage my time and how much i write because half my program is just getting the frames of the picture then turning it into a sprite

Author:  Tony [ Fri Nov 06, 2009 6:25 pm ]
Post subject:  RE:Procedures?!?!?!?!

I have a lot of personal work to do, but a lot of other people might be able to give you advice.

You can either post just a specific block of code that demonstrates what you are trying to accomplish, or if you need to include everything, you could upload a file.

Author:  Chaoskiller [ Fri Nov 06, 2009 6:27 pm ]
Post subject:  Re: Procedures?!?!?!?!

ok ill upload a file for anyone that wants to see Smile

Author:  B-Man 31 [ Sat Nov 07, 2009 11:20 am ]
Post subject:  Re: Procedures?!?!?!?!

Chaoskiller @ Fri Nov 06, 2009 6:27 pm wrote:
ok ill upload a file for anyone that wants to see Smile


Why didn't you include the pictures, if you uploaded a .zip or .rar file with all the pictures and code included, we may be able to help you because we can see the exact problem is.

Author:  andrew. [ Wed Nov 25, 2009 8:54 pm ]
Post subject:  Re: RE:Procedures?!?!?!?!

Euphoracle @ Wed Nov 04, 2009 8:55 am wrote:
it is a complete and utter nightmare to find and fix the bug.
Pretty sure that's impossible to do in Turing anyways (with processes at least) because it's unpredictable in nature and there isn't anything you can do about it.


: