FAQ  •  Register  •  Login

Label as a string var

<<

oblo

Posts: 60

Joined: Tue Jul 12, 2011 5:23 pm

Post Tue Nov 06, 2012 8:56 pm

Label as a string var

Hi all

Long time no see around here :oops: but now I'm getting some free time to spend with ZX Basic, so I hope to come more often :)
In fact, I have a question: I want to store a label name in a variable, in order to do GOSUBs using a var, not fixed names. I made this code so far, but it fails with the message 'identifier 'eti$' is a var, not a label':

  Code:
#INCLUDE <keys.bas>
paper 0: border 0: ink 7: cls
dim eti$
eti$="l01"
while 1=1
   if Multikeys(KEY1)<>0 then
      eti$="l01"
      gosub eti$
   elseif Multikeys(KEY2)<>0 then
      eti$="l02"
      gosub eti$
   elseif Multikeys(KEY3)<>0 then
      eti$="l03"
      gosub eti$
   end If
end While
l01:
print at 0,0;ink 1;"ESTAS EN LA ETI1";
RETURN
l02:
print at 0,0;ink 2;"ESTAS EN LA ETI2";
RETURN
l03:
print at 0,0;ink 3;"ESTAS EN LA ETI3";
RETURN


Can be it done or what I'm asking is impossible right now? Note that I'm asking for a string, not numbers.

Thanks and regards
<<

britlion

Posts: 766

Joined: Mon Apr 27, 2009 7:26 pm

Location: Slough, Berkshire, UK

Post Sun Nov 11, 2012 12:01 am

Re: Label as a string var

oblo wrote:Hi all

Long time no see around here :oops: but now I'm getting some free time to spend with ZX Basic, so I hope to come more often :)
In fact, I have a question: I want to store a label name in a variable, in order to do GOSUBs using a var, not fixed names. I made this code so far, but it fails with the message 'identifier 'eti$' is a var, not a label':

  Code:
#INCLUDE <keys.bas>
paper 0: border 0: ink 7: cls
dim eti$
eti$="l01"
while 1=1
   if Multikeys(KEY1)<>0 then
      eti$="l01"
      gosub eti$
   elseif Multikeys(KEY2)<>0 then
      eti$="l02"
      gosub eti$
   elseif Multikeys(KEY3)<>0 then
      eti$="l03"
      gosub eti$
   end If
end While
l01:
print at 0,0;ink 1;"ESTAS EN LA ETI1";
RETURN
l02:
print at 0,0;ink 2;"ESTAS EN LA ETI2";
RETURN
l03:
print at 0,0;ink 3;"ESTAS EN LA ETI3";
RETURN


Can be it done or what I'm asking is impossible right now? Note that I'm asking for a string, not numbers.

Thanks and regards


It can't be done that way, since labels are static - they are memory addresses, effectively. So at run time, there's no such thing as "eti#" - a call to eti1 is replaced by a jp nnnn instruction, so far as I can tell.

You'd have to make a jump table, or an array of address, and use that if you wanted to be able to leap to specific ones. That or have a routine that has an if else else else else to gosub based on a variable. Compiled code really doesn't allow calculated gotos in this way.


Also, it's good programming practice to avoid goto and gosub where possible. I wrote a pac man game too (can I still your awesome pac man beeps intro some time for it? :) ) And published the source code and a tutorial - have a look in the wiki ( http://www.boriel.com/wiki/en/index.php ... :Tutorials )
<<

oblo

Posts: 60

Joined: Tue Jul 12, 2011 5:23 pm

Post Mon Nov 12, 2012 11:08 pm

Re: Label as a string var

Thanks. Now I realize that it's impossible, I have to redone the engine from scratch, but it seems its better than I was doing.
About the Pac-Man intro, what do you mean with 'can I still your awesome pac man beeps intro some time for it?'? If you're asking permission for re-use it, please do it :D The source code I released is for that (well, and see how a bad programmer I am) :mrgreen:

Cheers
<<

britlion

Posts: 766

Joined: Mon Apr 27, 2009 7:26 pm

Location: Slough, Berkshire, UK

Post Tue Nov 13, 2012 1:41 am

Re: Label as a string var

I'm playing with http://www.worldofspectrum.org/infoseek ... id=0027713 to see about updating it.

I wrote a tutorial for ZX Basic on how I did that - and you may find some hints and tips useful. OCMan is very cool - but hard to read. You still have line numbers. (And my ability to read in Spanish is not good, which doesn't help ;)

I try to write procedurally - so I have things like


updateGhosts()
displayScore()
MovePlayer()

As procedures - it should (language aside) hopefully be clear what each line does at the top level.

In fact - my main loop and start point is crazy simple to read (and does so much!) - Without comments and fluff - the whole running game looks like this as a loop:

  Code:
    'Main loop
    do
        updatePlayer()
        updateGhosts()
        collisionCheck()
        updateTimers()
    loop


So I'm suggesting that you can use this procedural bottom up/ top down approach to make programs easier to read - and being more modular, you can more easily re-use parts in other programs.

For example print42 is just a few subroutines, really - but it's easy to plug into another program from the library!

Return to ZX Basic Compiler

Who is online

Users browsing this forum: No registered users and 1 guest

cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by Vjacheslav Trushkin for Free Forums/DivisionCore.

phpBB SEO