Simple entityByName function

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

Simple entityByName function

Postby Kilinich » Wed Dec 20, 2017 11:51 am

Hi, bros!
There are scene.entityByID and scene.entityByGeomID functions helps to access some object's but it's really complicated to use with not-linked via hinge or fixate objects.
So I write simple funtion scene.my.entityByName(name), and it's very easy to use:

1) Just copy code to console and press enter
2) add _name to any geom or entity and call scene.my.entityRegName(e.this) in onSpawn event.
3) use scene.my.entityByName(name) in any script!

Rating: rated 5.6
Filesize: 59.23 kB
Comments: 2
Ratings: 2
download


Code: Select all
scene.my.xFor = (n1, n2, code) => {
  n2 > n1 ? {
    m := (n1 + n2) / 2;
    scene.my.xFor(n1, m, code);
    scene.my.xFor(m + 1, n2, code)
  } : {code(n1)}
};
scene.my.entityList := [];
scene.my.entityRegName := (e) => {
  string.length(scene.my.entityList) > 0 ? {
    updated := false;
    newList := [];
    scene.my.xfor(0, string.length(scene.my.entityList)-1, (i)=>{
      scene.my.entityList(i)(0) == e._name ? {
        newList = newList ++ [[e._name, e.entityID]];
        updated = true
      }:{
         newList = newList ++ [scene.my.entityList(i)]
      }
    });
    updated ? {} : {newList = newList ++ [[e._name, e.entityID]]};
    scene.my.entityList = newList
  }:{scene.my.entityList = [[e._name, e.entityID]]};
};
scene.my.entityByName := (name) => {
  string.length(scene.my.entityList) > 0 ? {
    index := -1;
    scene.my.xfor(0, string.length(scene.my.entityList)-1, (i)=>{
      scene.my.entityList(i)(0) == name ? {index = i}:{}
    });
    index < 0 ? undefined : {scene.entityByID(scene.my.entityList(index)(1))}
  } : undefined
};
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

Re: Simple entityByName function

Postby eeieeio » Thu Dec 05, 2019 12:20 pm

Hello,
Thank you for the post
"Simple entityByName function"
I'm sure it will be very helpful to me except for one thing. Step 2. For some reason when I try to add _name to the script menu of the entity, it does not appear in the list. This seams to be a recent problem. I have been able to add _name in the past, but not now. Any ideas why?

Dave
eeieeio
 
Posts: 25
Joined: Thu Nov 30, 2017 7:02 pm


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 2 guests

cron