Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Label as a string var
#1
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 Smile
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
Reply
#2
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 Smile
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? Smile ) And published the source code and a tutorial - have a look in the wiki ( <!-- m --><a class="postlink" href="http://www.boriel.com/wiki/en/index.php/ZX_BASIC:Tutorials">http://www.boriel.com/wiki/en/index.php ... :Tutorials</a><!-- m --> )
Reply
#3
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 Big Grin The source code I released is for that (well, and see how a bad programmer I am) :mrgreen:

Cheers
Reply
#4
I'm playing with <!-- m --><a class="postlink" href="http://www.worldofspectrum.org/infoseekid.cgi?id=0027713">http://www.worldofspectrum.org/infoseek ... id=0027713</a><!-- m --> 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 Wink

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!
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)