creating variables with for-loop (ascending number)

About advanced scenes, and the Thyme scripting language used in Algodoo.

creating variables with for-loop (ascending number)

Postby hiltropper » Sun Oct 25, 2020 6:29 pm

Hi there!
At first, this is my context of code:
Code: Select all
p=(-1);
q=0;
x=54; //Gesamtmenge
y=9; //Zeilen, Spalten => x/y
offset=[0,0];

for(x, (i)=>{
   
   math.mod(i,y) == 0 ? {p = p+1; q=0}:{p = p; q = q+1};
   
      Scene.addLaserPen({
         pos:=[p,q]+offset;
         fadeDist:=10;
         colorHSVA:=[(i*5), 1.0, 1.0, 1.0];
         buttonDestroy:="f";
         size:=0.5;
         maxRays:=1;
         rotation:=math.pi;
         onLaserHit:=(e)=>{scene.my.perceptron=e.other.colorHSVA(2)};
      });
      
   
})


So, now the problem:

Code: Select all
onLaserHit:=(e)=>{scene.my.test = something};

is the part where i would like to have something like this:
Code: Select all
onLaserHit:=(e)=>{scene.my.test {i} = something};

and of course i know that's not how it works. I actually don't know if it can be done at all. In the end there should exist (for this example 54) some variables, named with ascending numbers at the end of their name.
Code: Select all
scene.my.test0 = something;
scene.my.test1 = something;
scene.my.test2 = something;
scene.my.test3 = something;
...

Could anybody tell me if it is possible and if not if there is an easy workaround because i found none (besides of creating the lasers and then go into every single script menue and typing in the variables by hand which reaaally annoys and is prone to typos). Please help! Thanks in advance!
yup yup
yuuuuuuup
yupyupyupyupyupyupyupyupyyyuuuup
hm... signatures...
hiltropper
 
Posts: 85
Joined: Mon Dec 20, 2010 12:02 pm
Location: Germany

Re: creating variables with for-loop (ascending number)

Postby icrls984 » Thu Nov 05, 2020 8:04 pm

I think you can create lists like so:
Code: Select all
test = [something, something1, something2]

access element with:
Code: Select all
test(0)

or a range
Code: Select all
test(0..2)

to get length use:
Code: Select all
string.length(test)

There is a list append operator you can use:
Code: Select all
test = test ++ [something3]



If you want anything else you need to implement your own functions.
Spoiler: show
I was just playing around, it seems you can also use test[0] as a shorthand for [test(0)]
.
User avatar
icrls984
 
Posts: 80
Joined: Thu Jun 13, 2013 6:33 am

Re: creating variables with for-loop (ascending number)

Postby Xray » Fri Nov 06, 2020 1:22 am

hiltropper -- Sounds like you need a way to write to a single array element. If that's the case, then check out the following discussion between myself and other users. You will see 2 or 3 different ideas for writing to or changing the value of a single array element:

viewtopic.php?f=13&t=12152&start=0
User avatar
Xray
 
Posts: 500
Joined: Sun Jun 17, 2012 6:12 am
Location: USA

Re: creating variables with for-loop (ascending number)

Postby hiltropper » Thu Nov 19, 2020 7:46 pm

Thanks for the replies, glad this forum ain't dead :thumbup: though your suggestions are not quite what i thought of, they have given me some ideas for workarounds.
yup yup
yuuuuuuup
yupyupyupyupyupyupyupyupyyyuuuup
hm... signatures...
hiltropper
 
Posts: 85
Joined: Mon Dec 20, 2010 12:02 pm
Location: Germany


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 4 guests