How can I create objects and modify them via script?

pfb shared this question 4 years ago
Answered

Hi,

I'd like to do the following:

1) create some objects, like e.g. points at random positions (possibly a few tens of them)

2) change their properties individually via scripting. For instance when a button is pushed the position and color of each object is changed.

I tried with lists of objects, but changing the properties of an item individually does not seem to be possible. That is, I can change the color of all the items to the same new color, but that's not what I want.

Instinctively I'd think of arrays, but I'm not sure of how I could use arrays in ggb scripts.

The only solution I can think of now is

1) manually introduce the points A, B, C, etc

2) write a very long list of instruction that go like "change the properties of A; change the properties of B; ...". That's very cumbersome, though, because the number of object is fixed and I cannot use a for cycle.

Is there a way of using a for cycle for changing the properties of many objects? Is it possible to create the objects from a script (like in ggbOnInit) ?

How should I go about this?

Thanks a lot for your help

Comments (3)

photo
1

Wait, maybe I got it.

Should I use the "Execute" command?

photo
1

Ok, now I think I really got it.

I think that using javascript as in the example here will get me where I want.

This code in button 1 will create some points

for(var i =0;i<10;i++) 
   ggbApplet.evalCommand("A_"+i+"=(random()*10,random()*10)");
This other code in button 2 changes their properties individually

for(var i =0;i<10;i++) {
var com="SetColor(A_" + i + "," + Math.random() + "," + Math.random() + ","+ Math.random()+")";
   ggbApplet.evalCommand(com);
} 

I'd still like to know whether it's possible to do the same with ggbscript.

photo
1

Try with Execute(<generated string-list>)

.

  • Execute(Sequence("A_{"+n+"}= ("+random()10+","+random()10+")",n,1,10))
  • Execute(Sequence("ShowLabel(A_{"+n+"},false)", n,1,10))
  • Execute(Sequence("SetColor(A_{"+n+"},"+random()+","+random()+","+random()+")", n,1,10))

.

© 2023 International GeoGebra Institute