Author Topic: Dynamically access variables  (Read 5533 times)

0 Members and 1 Guest are viewing this topic.

Offline Okimoka

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 8
  • Rating: +0/-0
    • View Profile
Dynamically access variables
« on: December 13, 2016, 01:06:17 pm »
Is there a way to shorten this code?

Code: [Select]
If L=1
 Pt-On(...,Pic1)
End
If L=2
 Pt-On(...,Pic2)
End
If L=3
 Pt-On(...,Pic3)
End
...

I tried accessing the images using memory adresses but it doesn't seem like the Pic variables have constant memory space between them.
Is there a way to evaluate an expression as a variable?
Is this already the most efficient you can get?
Thank you for your help!

Offline Sorunome

  • Fox Fox Fox Fox Fox Fox Fox!
  • Support Staff
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 7920
  • Rating: +374/-13
  • Derpy Hooves
    • View Profile
    • My website! (You might lose the game)
Re: Dynamically access variables
« Reply #1 on: December 13, 2016, 01:09:56 pm »
Well, you could do something like
Code: [Select]
Pt-On(....,L=1?Pic1,(L=2?Pic2,(L=3?Pic3))))
Or, preferable, probably a pointer-table, however it slipped my mind right now how to do those in axe....
If you are wondering what that question-mark stuff does: https://derpybot.sorunome.de/axe.php?q=%3F

THE GAME
Also, check out my website
If OmnomIRC is screwed up, blame me!
Click here to give me an internet!

Offline Okimoka

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 8
  • Rating: +0/-0
    • View Profile
Re: Dynamically access variables
« Reply #2 on: December 13, 2016, 01:35:49 pm »
Very nice, the ternary solution is already so much cleaner!
If I understood right, a pointer-table would require me to write the adresses of all Pic variables into a Buffer first?

Offline Runer112

  • Project Author
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2289
  • Rating: +639/-31
    • View Profile
Re: Dynamically access variables
« Reply #3 on: December 13, 2016, 01:48:58 pm »
You can do this by writing the addresses of the three sprites into a buffer, yes. The code would look something like this:

Code: [Select]
Pt-On(X,Y,{L*2-2+Data(Pic1ʳ,Pic2ʳ,Pic3ʳ)}ʳ)
However, best would be to declare the sprite data in sequence so each one's data immediately follows the next, which allows this:

Code: [Select]
[0123456789ABCDEF]→Pic1
[FEDCBA9876543210]→Pic2
[FFFFFFFFFFFFFFFF]→Pic3
...
other stuff here
...
Pt-On(X,Y,L*8-8+Pic1)

And a suggestion: in either case, your life would be made easier by using zero-based numbering. That generally translates into simpler/more efficient code. In the first case, you could remove the -2, and in the second case, you could remove the -8.
« Last Edit: December 13, 2016, 03:29:49 pm by Runer112 »

Offline Okimoka

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 8
  • Rating: +0/-0
    • View Profile
Re: Dynamically access variables
« Reply #4 on: December 13, 2016, 03:40:37 pm »
Works fantastically, thank you both so much for your time!
I revised the program to use zero-indexing, good hint  :thumbsup:

Offline Sorunome

  • Fox Fox Fox Fox Fox Fox Fox!
  • Support Staff
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 7920
  • Rating: +374/-13
  • Derpy Hooves
    • View Profile
    • My website! (You might lose the game)
Re: Dynamically access variables
« Reply #5 on: December 13, 2016, 03:48:19 pm »
You can do this by writing the addresses of the three sprites into a buffer, yes. The code would look something like this:

Code: [Select]
Pt-On(X,Y,{L*2-2+Data(Pic1ʳ,Pic2ʳ,Pic3ʳ)}ʳ)
However, best would be to declare the sprite data in sequence so each one's data immediately follows the next, which allows this:

Code: [Select]
[0123456789ABCDEF]→Pic1
[FEDCBA9876543210]→Pic2
[FFFFFFFFFFFFFFFF]→Pic3
...
other stuff here
...
Pt-On(X,Y,L*8-8+Pic1)

And a suggestion: in either case, your life would be made easier by using zero-based numbering. That generally translates into simpler/more efficient code. In the first case, you could remove the -2, and in the second case, you could remove the -8.
You actually only have to name the first block, so that your code looks like this (assuming now you have zero-based numbering):
Code: [Select]
[0123456789ABCDEF]→Pic1
[FEDCBA9876543210
[FFFFFFFFFFFFFFFF
...
other stuff here
...
Pt-On(X,Y,L*8+Pic1)

THE GAME
Also, check out my website
If OmnomIRC is screwed up, blame me!
Click here to give me an internet!