forums

TNT Basic Forums > Programming Q&A
Accessing variables from other procs
< Last Thread     Next Thread >
Author
Thread        Post A Reply

11-10-2003 18:04

Posted by:
mark_667

Location:
England

Click Here to Email mark_667   Find more posts by mark_667

Hi,
is there any way of making a normal integer a global from inside a procedure? I have a variable (current_player_sprite) which is dependant on the outcome of an if statement in a procedure, can other procs use this in any way when displaying the sprite so I don't have to needlessly repeat lots of code (see below). This is probably best explained with an example of what I'm trying to do:
e.g.
'Lots more code here
Procedure MovePlayer()
while gameOver=false
current_player()

int x=sprite x(current_player_sprite)
int y=sprite y(current_player_sprite)

if poll input (1) or pressed(56) 'Hold shift for speed boost when moving
float Xvel=9, Yvel=9 'Speed increases by 3
end if

if right or pressed (124)
repeat
sprite current_player_sprite,x=x+Xvel,y
draw frame
until not pressed (124) or right
end if

if left or pressed(123)
repeat
sprite current_player_sprite,x=x-Xvel,y
draw frame
until not pressed (123) or left
end if

if up or pressed(126)
repeat
sprite current_player_sprite,x,y=y-Yvel
draw frame
until not pressed (126) or up
end if

if down or pressed (125)
repeat
sprite current_player_sprite,x,y=y+Yvel
draw frame
until not pressed (125) or down
end if

wend
end proc
'More code here
Procedure Current_player()
if sprite exists (0) then int current_player_sprite=0
if sprite exists (1) then int current_player_sprite=1
if sprite exists (4) then int current_player_sprite=4
if sprite exists (6) then int current_player_sprite=6
if sprite exists (8) then int current_player_sprite=8
if sprite exists (9) then int current_player_sprite=9
end proc

Sorry if the posting's a bit long, but I want to try and avoid the confusion my threads usually generate :)

11-10-2003 18:55

Posted by:
-wyvern

Location:
Bedroom

Find more posts by -wyvern

It is generally considered good programming to declare all variables at the very beggining of a program, so you can set global stuff from there. This makes things a lot easier to understand. Oh, and procedures are best used for manipulating numbers or drawing graphics, rather than housing sections of game code. Use labels for that.

Sorry to be so scathing of your code, but it will make things a lot easier.

11-10-2003 22:20

Posted by:
eekaydee

Location:
CA, USA

Click Here to Email eekaydee   Find more posts by eekaydee

quote:
Use labels for that

As in goto labels? It's much cleaner to use procs for sections of code than goto statement labels.

11-11-2003 02:07

Posted by:
MapMaker

Location:
Edmonton, Alberta (Canada)

Click Here to Email MapMaker   Find more posts by MapMaker

I've run into the same problem Mark has.

I'm trying to make an options menu for my game that would modify the number of planets on the map.... but the number of planets is stored in a global array that is declared at the beggining.

Is there any way to go around this, like being able to change the number of indexes in an array after it has already been pre - declared?

11-11-2003 11:02

Posted by:
-wyvern

Location:
Bedroom

Find more posts by -wyvern

doesn't dim planets[x,x] work? Apparently you can use dim to dimension an array, but I am not sure whether it works twice.

11-11-2003 15:34

Posted by:
MapMaker

Location:
Edmonton, Alberta (Canada)

Click Here to Email MapMaker   Find more posts by MapMaker

Ya, that worked
I had to leave the indexes out though when I first declare them, (array[]), then dim them later (dim[array[x]).

11-11-2003 17:09

Posted by:
eekaydee

Location:
CA, USA

Click Here to Email eekaydee   Find more posts by eekaydee

You can do that!? Now I have yet another thing which I worked around for no reason...

11-11-2003 19:29

Posted by:
mark_667

Location:
England

Click Here to Email mark_667   Find more posts by mark_667

quote:
It is generally considered good programming to declare all variables at the very beggining of a program, so you can set global stuff from there. This makes things a lot easier to understand. Oh, and procedures are best used for manipulating numbers or drawing graphics, rather than housing sections of game code. Use labels for that.

This may be a simple problem, but I'm not that much of an amateur! Sorry - I've not been very clear. I'm not actually using a global variable. The post title explains it better than my description! I have one variable of which the outcome of the value is decided on a piece of code (see the Current_player() in my original post), but I'd like this to be used by other procs in detecting which of the multiple possible player sprites is currently active on-screen. If it wasn't in the proc, I'd have to repeat it about a million times elsewhere! However, I can't find a way of referencing the variable's value from any of the other procedures that need to use it?
thanks for all your feedback :)

11-11-2003 23:42

Posted by:
Mark Tully

Location:
TNT HQ, England

Click Here to Email Mark Tully   Find more posts by Mark Tully

I'm not sure I understand your problem. Can you not return the current player as the procedure's result and use 'Result Int' to get the returned value in the calling procedure?

Mark

11-12-2003 10:51

Posted by:
mark_667

Location:
England

Click Here to Email mark_667   Find more posts by mark_667

quote:
Can you not return the current player as the procedure's result and use 'Result Int' to get the returned value in the calling procedure?


How would I do that based around the code posted above? Also, can a procedure have more than one result? I don't really have a need for it here, I was just wondering what is possible through this method.

11-13-2003 21:30

Posted by:
Mark Tully

Location:
TNT HQ, England

Click Here to Email Mark Tully   Find more posts by Mark Tully

Procedures can only return one value at the moment, although it can be a string, integer or float variable. If you wish to return more then your only hope is to use global variables at this time.

So if you want a procedure to return the current player sprite, you could write it like this:

Procedure Get_Current_Player()
int player
if sprite exists (0) then player=0
if sprite exists (1) then player=1
if sprite exists (4) then player=4
if sprite exists (6) then player=6
if sprite exists (8) then player=8
if sprite exists (9) then player=9
end proc (player)

And call it like this
Get_Current_Player()
resultantPlayer = Result Int

Hope this helps (and that I've not completely misunderstood what you were asking!)

Mark

11-13-2003 21:38

Posted by:
Mark Tully

Location:
TNT HQ, England

Click Here to Email Mark Tully   Find more posts by Mark Tully

Oh, and if you wanted to make a variable in the main program temporarily global just in a particular procedure, you can use the 'shared' command. See http://www.tntbasic.com/learn/help/guides/sharedvariables.htm .

Mark

11-15-2003 11:44

Posted by:
mark_667

Location:
England

Click Here to Email mark_667   Find more posts by mark_667

Fixed!

Thanks Mark looks like your example worked; I didn't really know about the shared variables thing, but thanks for informing me it looks like it could prove quite useful :)

All times are GMT        Post A Reply

Forum Jump:
< Last Thread     Next Thread >

< Contact Us - TNT Basic >

Powered by: vBulletin Lite Version 1.0.1 Lite
Copyright © Jelsoft Enterprises Limited 2000.