Skip to the article
NeuraCoreUser Wiki
โ† Back to this chapter
Embedded Python

Your first script

Two lines that change nothing, so you can see the shape of a result before anything acts on it.

Read before you change anything

Paste this, press Run, and look at the output area.

print(avatar.get_balance())
print(avatar.get_avatar_state())
  • avatar is the object that was already there.
  • get_balance asks for the Linden dollar balance. It reads and changes nothing.
  • get_avatar_state asks what the avatar is doing: standing, sitting, flying, walking.
  • print puts the answer in the output area. Without it a script can run perfectly and appear to do nothing.

What came back is not a sentence

The output looks like this, near enough:

{'balance': 1250}

That is a Python dictionary, not text. Which means you can take one value out of it and use it:

money = avatar.get_balance()
print(money["balance"])
  • The first line keeps the whole answer in a name of your choosing.
  • The second takes the one field out of it. The square brackets and the quotes are how Python reads a dictionary.

Some answers are lists

An action that reports on several things gives you a list, and a loop is how you walk it.

for person in avatar.get_nearby_avatars():
    print(person["name"], person["id"], person["position"])
  • Each turn of the loop puts one nearby resident in person.
  • name is their name, id is the key Second Life knows them by, and position is where they are in the region.
  • A name can be empty for a moment after somebody arrives, because the region has not sent it yet. That is normal and the topic on waiting explains it.

Then let it act

Now something that reads first and only then does anything.

state = avatar.get_avatar_state()
print(state)

if state.get("sitting"):
    avatar.set_sitting(False)

avatar.move_forward(duration=2.0)
avatar.stop()
  • The state is read and printed, so you can see what the script saw.
  • get is a safer way to read a dictionary than square brackets: it answers nothing rather than failing when the field is absent.
  • The avatar only stands up if it was sitting. A script that stood up unconditionally would take the avatar off whatever it was on.
  • duration is in seconds, so this walks forward for two of them, then stops. Always stop: movement carries on until something ends it.

Build it one line at a time

Run the reading part, look at the output, and only then add the line that acts. It is a slower way to write five lines and a much faster way to find the one that was wrong.