Physion

  • Increase font size
  • Default font size
  • Decrease font size
Welcome, Guest
Username Password: Remember me

Introduction to Scripting... (Multi-Sections)
(1 viewing) (1) Guest
  • Page:
  • 1

TOPIC: Introduction to Scripting... (Multi-Sections)

Introduction to Scripting... (Multi-Sections) 3 years, 2 months ago #877

SECTION-1A

(This POST contains MULTIPLE sections)

This is designed to fill the gap with those just starting, even though there is a myriad of tech info throughout the Forum, and various help sites, it becomes daunting, and one gets put off ! when just starting.

Physion as it is, on launching, does a pretty good job of simulating most things within a fairly simple environment, and MOST menus/options are fairly self explanatory... but sometimes even some of those options/settings utilizations are overlooked.

The MAIN purpose of THIS category, is to delve into things 'under-the-hood' in regards to achieving OTHER things, with regard to what is presumably initially obvious... as we can't talk about 'Coding' etc without grasping what is following, first...

For Eg, the first thing people do is obviously draw boxes, circles, etc, and if, while the sim is running, watching them fall and interact with each other physically. You can draw shapes prior to, or while, the sim is running. Notice that STOP and then UNDO resets the sim, in stages. (Things will get more technical shortly!!)

It's important to get past this in basics, so people understand later, what is happening, and how objects are referenced !!
Whether a sim is running or not, (though best when it is not), after you have created an 'Object', and it is selected, (presently active for mods by using the 'Mouse-Pointer' icon), you will notice a LIST on the righthand side. These are a list of that objects current PROPERTIES. Things like its current X, or Y position on the screen, size, colour, and many others. Here you can fine-tune most settings, which will be explained again later... but ONE of the properties is NAME. For IMPORTANT objects in a scene, you should change this to meaningful names, like 'MainDrive', or 'Piston1' etc, instead of the default ambiguous names like 'item37' and 'item56' etc.
ESPECIALLY if you are trying to make sense of the name refs within CODING (soon).

Section 1B...
will explain how Objects.Properties can be Referenced !!!!!! in the simplest form.
Glenn
1x1 square diag = root-2. Cube diag = root-3. Beautiful !
Last Edit: 3 years, 2 months ago by Glenn555.
The topic has been locked.

Introduction to Scripting... Section 1B... 3 years, 2 months ago #878

Most modern programing languages, (unlike old DOS style BASIC etc), are what is called... "Object-Oriented'. This means that no matter where an 'object' is on the screen, (within 'Windows' etc), then the OBJECT has already been created, by what ever means, and references to it, reference the Object and its Property, with a FULLSTOP inbetween.... (Back to that in a min...)

Start a new project, and draw a BOX.
Select it from the 'Physion Mouse Pointer Icon', and change it's 'Name' Property to...
Box1

Note: that in Javascript, and in relation to object names and properties... that they are CASE-SENSITIVE, so 'box1' is NOT the same as 'Box1', and a property of 'radius' is NOT valid if it expects a property name of 'Radius' !!!!
How does that affect me?? Read on...

Up until now, everything is happening automatically with Physion, performing normal tasks. But lets say we want to intervene in ANOTHER way ? like changing its Friction ?
Well you CAN change its Property on screen, as mentioned above, but that does not let us change it on the fly under program control while the Sim is running and certain EVENTS occur... (more later).
So as a PRECURSOR to that, lets TYPE stuff to do what we want, but in a direct way for now, using the keyboard....

We will start with the 'Console', where you can type commands directly, without the GUI, (Graphics User Interface).
You can hit the 'Tilde' key, (or Shift-Tilde)?, which looks like a little Sine-Wave on the Keyboard, or if you have problems with that, then from the menus above...
select View | Dock-Widgets | Console

HERE, you can interact with Physion, scenes, objects, properties and many other things, (mostly beyond the scope of this discussion yet), as a precursor to the sorts of things you can include in multiple sequential lines with 'snippets' of code within events later. But don't worry about that for now...

SO, assuming you had previously created a BOX as discussed before, and named it 'Box1' (without the quotes), then we can reference it here too !
Type in the Console input bar at the bottom....

Box1.Friction = .9 (...then hit Enter...)

Notice how this referenced the OBJECT, then a FULLSTOP, and then the Property name! which is all case-sensitive...
Try changing other property names too, using this syntax....
(This all makes more sense later with sequential lines of CODE for events.)
Glenn

The next Section 1C will delve a bit deeper, before explaining about .SCRIPT files you can download, and their differences....

B.T.W.
When the Console Editing/Entry bar is clicked, pressing the UP arrow on the Keyboard, shows the LAST typed command, and multiple presses of the UP arrow go further in history! This is good so as to save retyping the whole instruction again, although you can then edit that...
1x1 square diag = root-2. Cube diag = root-3. Beautiful !
Last Edit: 3 years, 2 months ago by Glenn555.
The topic has been locked.

Re: Introduction to Scripting... Section 1C... 3 years, 2 months ago #879

I know this might seem boring at first, but there is method to the madness !

Start a NEW 'scene'.
Draw a box somewhere... but don't bother running the Sim.
The box prob defaults to 'item4', as the bottom-brick wall and the left & right brick walls default to being items 1 to 3 in a new scene! In this demo, we will NOT rename the object, for scene familiarization. Open the Console as per above instructions...
(Don't worry... All this is going somewhere!!!!)

Now type in the 'Console' bar at the bottom, (not the console results just above)...
item4.x (lowercase-x...then Return...)
It should return 'Undefined'
This is because the property name is not valid, as it expects a CAPITAL .X property!
(See the Object Properties under the GUI for spelling/capitilization)
Try the above AGAIN, by typing...
item4.X (...then Return...)
NOW you will see the current value assigned to the '.X' property ! (Capitalization)

Such Properties can be SET or READ via this call.
For example...
item4.Y
reads/returns the current value of that property.
item4.Y = 1
SETS the new property value !

Up to now, we have been entering (viewing) results/actions from SINGLE statements/queries at a time. NORMALLY, with multiple lines of 'CODE' in Events, or .Script files, each instruction is separated by a Semi-Colon, ; as a 'Delimiter'.
You can do this TOO within the 'Console', by typing the likes of...
item4.X = 0; item4.Y = 0;
and it will perform both operations at once !
The use of the SemiColon and new lines, is critical later, within multiple line editors.

(Ok... We are NEARLY there, within the next few explanations to come !!!)
1x1 square diag = root-2. Cube diag = root-3. Beautiful !
Last Edit: 3 years, 2 months ago by Glenn555.
The topic has been locked.

Re: Introduction to Scripting... Section 1D... 3 years, 2 months ago #880

OK... We will speed ahead here a bit ! Then into the interesting stuff....

Variables, like x=5, or y=x+6, basically exist as one would expect. Consider this...
I'm in a sealed room with a bunch of other people, and shout out that x=5...
I then ask someone in that room what 'x' equals, and assuming they were listening will answer '5'. But if i then went into another room with other people, and asked the same question, they wouldn't know what i'm talking about!. In fact i could THEN tell THEM that x=10, and they will know THAT, but in the previous room they think it is still '5'.
This is called SCOPE, and is very important when using variables within Functions, or within 'Events' in Physion. (I will refer to 'Javascript' for simplicity, though not exact).

When you see bits of CODE in the Forum, or in .script files, you will often see statements like... " var MasterCount = 0; "
The word 'var' defines it as a Variable, just as you would with x = 0, BUT it specifies that the variable is only recognized within that Event/Function. So ANOTHER Event for ANOTHER Object etc. does not recognize it. It is used locally in that Event.
That can be GOOD! as you can use the SAME variable name elsewhere, within their own 'Shouting-Room' so to speak.

HOWEVER, if you leave out the 'var', as in... " MasterCount = 0; ", then THIS variable has what is called 'GLOBAL' Scope. Which means that it can be used/read and acted upon within other Events also.
This is like a P.A. system over a microphone to ALL rooms of ALL people! with my analogy. Understanding this is paramount to using and understanding even basic scripts, so i really had to introduce it now!

You have seen that you can intervene apart from the G.U.I. by accessing objects properties via the command console. And touched on Variables... So now i will demonstrate how such 'typing' can also be entered sequentially within an Event. Not the console.

Events are like Triggered blocks of CODE, that occur when a certain event occurs.
Look at the scene Objects Properties, and you will see the likes of....
'OnStart', 'OnUpdate', 'OnContact' and others. These contain 'snippets' of either 1, or multiple lines of CODE which will be executed when THAT event is triggered, by THAT object.

IMPORTANT: AT THIS STAGE, ALL SIMPLE LINES MUST END WITH A SEMI-COLON (;)
(not quite true, but will do for now for simplicity)
AND YOU SHOULD ALWAYS HAVE A BLANK LINE AT THE END OF THE BLOCK.

Download this example .scene file, and load it into Physion...
(Right-Click and 'Save-As'....)
workingfiles.netii.net/PhysionStuff/Tuto...le-Events-Demo.scene
Instructions and info are within the scene.
Be sure to look at the yellow box's 'OnStart' and 'OnContact' events...
Will explain more later, but at least you can see a result.

NOTE: Physion defaults to looking for scenes in its own sub-directorys, but you can alter the path to where ever it is that the file you want exists.

Glenn
1x1 square diag = root-2. Cube diag = root-3. Beautiful !
Last Edit: 3 years, 2 months ago by Glenn555.
The topic has been locked.

Re: Introduction to Scripting... Section 1E... 3 years, 2 months ago #887

More to come... and it gets better !!!
(And understanding 'Types' of variables, and their uses)
1x1 square diag = root-2. Cube diag = root-3. Beautiful !
Last Edit: 3 years, 2 months ago by Glenn555.
The topic has been locked.
  • Page:
  • 1
Moderators: Glenn555
Time to create page: 0.45 seconds