11-11-2012, 12:01 AM
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?
