[Solved] Pointing to variables

Posted By: PlaystationThree

[Solved] Pointing to variables - 01/09/09 00:16

This is kinda hard to explain in 3dgs terms because its from Adobe Flash's actionscript.

Let's say I have a fer variables: var1 var2 var3.

Say I want to go through all of them in sequence, setting each to a certain value. In actionscript I would do:

Code:
function goThrough(number){
    var i = 1;
    while(i<=number){
        ["var"+i] = random(10);
    }
}
goThrough(3);
 


while in lite-c you would have to:

Code:
   ...
    var1 = random(10);
    var2 = random(10);
    var3 = random(10);
   ...
 


Essencially what it does is go through all the vars and sets each to a random number. What the ["var"+i] section does is search for the variable "varX" where X is whatever i is. So if i == 2 then it searches for var2. Can you do something like that in lite-c? I read the sections in the manual about variables but nothing...
Posted By: heinekenbottle

Re: Pointing to variables - 01/09/09 00:44

An array might work.
Code:
function goThrough(var number)
{
	var myArray[128];
	var i;
	for(i = 0; i < number; i++)
	{
		myArray[i] = random(10);
		diag_var("myArray: %f\n",myArray[i]);
		wait(1);
	}
}


EDIT: My code had some C++ syntax that Lite-C did not like.
Posted By: PlaystationThree

Re: Pointing to variables - 01/09/09 00:48

Good point, but actually what i'm doing is going through a set of TEXT* structs and changing their strings. I guess I should have made that clear in my first post. Sorry.
Posted By: MMike

Re: Pointing to variables - 01/09/09 03:59

well i love action script to anf the fact in can merge strings just like
var name= "you" + " second" + bla ba and it will result in the complete sentense.

but lite c can't work like that.. as i know..
Posted By: PlaystationThree

Re: Pointing to variables - 01/09/09 05:57

Yeah, I would have stuck with Flash because the script system is great, but it doesn't have 3d built in. The 3rd party API suck too.
Posted By: pegamode

Re: Pointing to variables - 01/09/09 07:45

If you'd like to store pointers you could use my GSHashmap. You find it under contributions. With this hashmap you can store any pointer under a certain key and get it back. The key is a string that you could build by adding a number to a prefix in a loop as you would do it in flash.

Regards,
Pegamode
Posted By: PlaystationThree

Re: Pointing to variables - 01/09/09 08:14

@pegamode
That sounds what i'm looking for. Thanks for the post, and great contribution.
Posted By: Tobias

Re: Pointing to variables - 01/09/09 08:19

Why use an external plugin when the language already offers what you need? I think the C/C++ method of arrays is far better than actionscript, it's much faster and more logical.

Use an array just as heinekenbottle suggests, only without the superflous wait(). When you need TEXT* well then use an array of TEXT*.

Code:
TEXT* texts[128];

function goThrough(var number)
{
	var i;
	for(i = 0; i < number; i++)
	{
		texts[i] = txt_create(1,0);	
	}
}

Posted By: pegamode

Re: Pointing to variables - 01/09/09 08:33

You can use both to solve this problem ... an array or a hashmap. What to choose depends on your needs. Both have advantages and disadvantages. You have to compare arrays with hashmaps and then you have to decide which one fits better your needs.
Posted By: Quad

Re: Pointing to variables - 01/09/09 10:39

you can check var_for_name too.
© 2024 lite-C Forums