September 19, 2018, 02:49:34 PM

Author Topic: Add a variable to a variable.  (Read 146 times)

Offline Borislav

  • Jr. Member
  • **
  • Posts: 39
Add a variable to a variable.
« on: September 13, 2018, 05:29:51 PM »
Basically to make a variable variable.
Here is some pseudo-code
Code: [Select]
number = 54
var+number = 43 #var54
I want to get number from a for loop and I want to make loads of variables with their own specific names generated.


Offline Holzchopf

  • Jr. Member
  • **
  • Posts: 90
Re: Add a variable to a variable.
« Reply #1 on: September 13, 2018, 05:45:22 PM »
Not possible that way because BMX is compiled and not interpreted, therefore the exact variable name has to be known at compile-time and can't be generated at runtime.

BUT..! You can use arrays instead:
Code: [Select]
Local var:Int[100] ' var with 100 fields (indexed from 0 to 99)
number = 54
var[number] = 43 ' var at index 54

Offline Borislav

  • Jr. Member
  • **
  • Posts: 39
Re: Add a variable to a variable.
« Reply #2 on: September 13, 2018, 05:55:31 PM »
Not possible that way because BMX is compiled and not interpreted, therefore the exact variable name has to be known at compile-time and can't be generated at runtime.

BUT..! You can use arrays instead:
Code: [Select]
Local var:Int[100] ' var with 100 fields (indexed from 0 to 99)
number = 54
var[number] = 43 ' var at index 54
You can't make an object in B3D using an array right?
I am actually trying to make an object using these variables.
I am basically making a value of objects and they are going to be numerated so I could specifically modify each one of them at any time.

Offline bsisko

  • Jr. Member
  • **
  • Posts: 17
Re: Add a variable to a variable.
« Reply #3 on: September 14, 2018, 08:34:03 AM »
Do you understand types Borisslav?

If I understand what you're asking, you might try something like this!

Code: [Select]
type MyObject
   Field ID : Int
   Field Another var  : Int
   Field Another var  : Int
end type


for Item = 1 to 100
    ThisObject.MyObject = New MyObject
    ThisObject.ID  = Item
next


In the rest of the code for your program you will reference the ID number of the type to as a handle to your object!

Offline Matty

  • Sr. Member
  • ****
  • Posts: 460
    • MattiesGames
Re: Add a variable to a variable.
« Reply #4 on: September 14, 2018, 09:03:11 AM »
Yes you can have an array of objects in blitz3d.

I believe declare the array as:

Dim myarray.mytype[n]

Although been so many years no idea of the exact syntax anymore or even whether b3d uses square brackets for arrays.

Offline Holzchopf

  • Jr. Member
  • **
  • Posts: 90
Re: Add a variable to a variable.
« Reply #5 on: September 14, 2018, 09:23:42 AM »
Oh, is it B3D now..? :-\

IIRC either Dim myarray.mytype(n) or Local myarray.mytype[n] - the second one is called a blitzarray. They can be defined as local or global, be passed to or returned from functions but can't be multidimensional or be resized.

Offline Borislav

  • Jr. Member
  • **
  • Posts: 39
Re: Add a variable to a variable.
« Reply #6 on: September 15, 2018, 07:47:51 AM »
Thank you!

Offline Borislav

  • Jr. Member
  • **
  • Posts: 39
Re: Add a variable to a variable.
« Reply #7 on: September 15, 2018, 12:03:06 PM »
Do you understand types Borisslav?

If I understand what you're asking, you might try something like this!

Code: [Select]
type MyObject
   Field ID : Int
   Field Another var  : Int
   Field Another var  : Int
end type


for Item = 1 to 100
    ThisObject.MyObject = New MyObject
    ThisObject.ID  = Item
next


In the rest of the code for your program you will reference the ID number of the type to as a handle to your object!
But is that compatible with Blitz3D/MiniB3D objects?

Offline fielder

  • Jr. Member
  • **
  • Posts: 32
Re: Add a variable to a variable.
« Reply #8 on: September 15, 2018, 01:56:09 PM »
MiniB3d is for Blitzmax .. and this code is for Blitzmax
Blitz3D is not compatible with this code.. is a different language.

i suggest you to learn something about arrays... is the most simple thing to use for this kind of usages.

you can also do something like your pseudocode .. using files...

for example create a file called "var+number" and write into it an integer.... this is a VERY SLOW solution... but works.. i use this for example to add a thumbnail to a certain item...

i have an item called  652567256 taken from an array (or a barcode reader... or a lot of other things..)...  the software check if the file 652567256.jpg exist... if exist the image will be added to item description on main application :)

Oh, is it B3D now..? :-\
Yes you can have an array of objects in blitz3d.
this forum is BlitzMax / BlitzMax NG i think is better to NOT publish B3d code on it.