Amabilis Software    3D Canvas Community    3D Canvas Discussion Forums  Hop To Forum Categories  Plug-In Development and Scripting    Physics simulator: Global variable storage?
Go
New
Find
Notify
Tools
Reply
  
-star Rating Rate It!  Login/Join 
Junior Member
Posted
I am writing a physics simulator for 3D Canvas, and so far it is working really well (objects bounce off ground with gravity and trajectories, etc.). 8)

However, I wonder if there is a way to set some global variables in an interactive script (ie. from Tools -> Run Script -> ...) that can be later retrieved from a "Component [animation] script". Currently, the component's animation script has to have all parameters hard-coded, whereas I would like to assign some custom parameters to an object or group that persist and can be retrieved by AnimateObject() calls.

Any help would be greatly appreciated,
Cal.
 
Posts: 66 | Registered: Fri November 07 2003Reply With QuoteEdit or Delete MessageReport This Post
Member
Posted Hide Post
hiya
Not quite sure what you're asking, so I'll answer anyway :-)

Could you do it by putting the variables in a text file and reading them from the script.

I've just started doing something similar for ... well see my post on a script to snap-to.  I write the coordinate info to a file with one script, and read it with another.

George
 
Posts: 190 | Registered: Fri November 07 2003Reply With QuoteEdit or Delete MessageReport This Post
Junior Member
Posted Hide Post
That might be a good idea... I'll give it a try.

Basically, what I want to do is get the user to select an object to animate. Then, the user is able to select various other objects for collision detection purposes. Finally, they enter initial trajectory and coefficients (restitution, static/dynamic friction, etc.).

When the AnimateObject call is made during animation, these parameters need to be called upon. Using a text file for the parameterized data sounds like a good idea.

Thanks,
Cal.
 
Posts: 66 | Registered: Fri November 07 2003Reply With QuoteEdit or Delete MessageReport This Post
Member
Posted Hide Post
goodoh

if you haven't used them in VBScript before, the code you need is in the scripts I posted last week for snap-to.  Just remember the speed of light is constant.

George
 
Posts: 190 | Registered: Fri November 07 2003Reply With QuoteEdit or Delete MessageReport This Post
Junior Member
Posted Hide Post
George -- thanks for the pointers... I am now using file I/O for the parameters, which is better.

However, in some circumstances it would be nice to generate global variables that have scope [u]that spans between[/u] objects.

It appears that all variables that are defined in a component script only have scope that is defined to the object that contains the script.

How can one create truly global variables besides file I/O?

One of the reasons for this need is to accomplish more complex tasks that require dynamic computation across many objects, rather than simple parameterization. For example, collision detection between many objects or "swarm" behavior would be much easier if there was some way to share variables between objects.

Any ideas?

Thanks,
Cal.
 
Posts: 66 | Registered: Fri November 07 2003Reply With QuoteEdit or Delete MessageReport This Post
Member
Posted Hide Post
do I understand the question yet?  Global variables go at the top of the script.  I've not done animation scripts but I assume it's the same.  I also define classes at top of script: can't remember if you have to.  Here's an example from the top of one of mine: In this Scene, Face and S can be accessed from anywhere.  Is that what you want?
George
-----------code---------
Option Explicit    
Class SwarmClass
   Public    SwarmRadius       'The radius
   Public    NumParticles    
End Class
'Declare global variables
   Public    Scene
   Public    Face  
   Const     Pi= 3.14159265358979  
   Public    S 'as SwarmClass    

Sub Main (CanvasApp)

...
 
Posts: 190 | Registered: Fri November 07 2003Reply With QuoteEdit or Delete MessageReport This Post
Junior Member
Posted Hide Post
Thank you very much for your help, George --

Assume that you wrote the SwarmClass and placed the code in ObjectA's component script. Are you saying that the class instantiation (S) will be visible to ObjectB's component script?

If there is visibility between component scripts, then that's great. I had assumed that there wasn't visibility because I tried to define the same named global variables in two component scripts (but did not use the "public" keyword) and there was no conflict in the assignments, which led me to believe a global in a component script only has scope local to that component script.

Thanks for any help -- I have moderate experience with C++, but only started Visual Basic a couple days ago, so I need to learn a lot Smile I am happy to see the support for scripting in 3D Canvas as it really helps open up some functionality.

Thanks,
Cal.
 
Posts: 66 | Registered: Fri November 07 2003Reply With QuoteEdit or Delete MessageReport This Post
Member
Posted Hide Post
ohhh finally I geddit.  Do you want to have two bouncy balls and say "for this animation Gravity is 200m/s/s" and just set gravity once for all bouncy balls?

Problem is I have no experience with component scripting for animation.  But I find it unlikely that you are able to do what you want.  Unless there is a heirarchy thing: is a group script executed before an object script, and can that object script refer to it's group script?  I doubt it.

I can think of ways to fudge it: using the text files for example.  And a script can get certain properties for other objects - so you could for example make a "ghost" object whose only purpose is to allow you to use it's coordinates as parameters for the scripts.  Does that make sense.?  

Can't help you otherwise.
Luck
George
 
Posts: 190 | Registered: Fri November 07 2003Reply With QuoteEdit or Delete MessageReport This Post
Junior Member
Posted Hide Post
Yes.... that's pretty much what I want. I was hoping that there would be some sort of hierarchical scope in scripting.

I have just confirmed that one cannot do this. The scope of a global variable is tied to the Component or Group script in which it is defined. Even though the Group script is run just before the component script, the globals aren't available.

Text files won't work when one is dynamically calculating data that needs to be passed between objects. And yes, it is possible to traverse a scene to enact the changes, but this is really too clumsy.

The idea of a ghost object is interesting, but what would be really nice is either:
  • global script that can contain variables/classes which are accessible from all other component/group scripts
  • custom data fields for an object


Cal.
 
Posts: 66 | Registered: Fri November 07 2003Reply With QuoteEdit or Delete MessageReport This Post
Member
Posted Hide Post
Well... it's probably not designed for this purpose, but you could set a value in the registry as your "global". I'm not sure how to do this from a script though.

Richard
 
Posts: 2378 | Registered: Fri November 07 2003Reply With QuoteEdit or Delete MessageReport This Post
Junior Member
Posted Hide Post
There's an idea I hadn't thought of... I imagine it would be a much slower access mechanism, but perhaps it may work.  :-/

As for the simulator script, all I have left to do is perform some interactive object selection by the user in the script. I'm hoping that it's possible to show the user a dialog box, then allow them to select an object/face, then continue with the script, etc. I don't have access to 3DC right now, but I am very hopeful ??? that we can interactively allow selection during a script's execution.

Thanks,
Cal.
 
Posts: 66 | Registered: Fri November 07 2003Reply With QuoteEdit or Delete MessageReport This Post
Member
Posted Hide Post
I would go back to the text file idea.  Where one or more files keeps the values of your globals.  You write a sub that is included in each script so that:

ChangeGlobal (varname, ValueToBeSet)

does the work in each component script..  I just thought you might be able to use the clipboard, but a quick search suggests vbscript has no clipboard object.  Javascript might be able to do it, but I've had no luck writing javasript for 3DC.

I don't believe you can interact with object in a script.  you can a bit with a plugin, but they don't do animation ha ha

George
 
Posts: 190 | Registered: Fri November 07 2003Reply With QuoteEdit or Delete MessageReport This Post
Junior Member
Posted Hide Post
Geo --

quote:
I would go back to the text file idea.  Where one or more files keeps the values of your globals.  You write a sub that is included in each script so that:

ChangeGlobal (varname, ValueToBeSet)

does the work in each component script..


Unfortunately this is probably too inefficient when one wants to share a lot of data between objects that is calculated on the fly and is recalculated often. Perhaps there might be an efficient way to code it so that the text I/O is kept limited.... hmm... I'll give it some thought.

quote:
I don't believe you can interact with object in a script.  you can a bit with a plugin, but they don't do animation ha ha


That's what I was afraid of  :'(. I don't have VBA, so I assume that I can't write a plugin. This, combined with the fact that we can only select a single object means that making this script's usage intuitive might be difficult to do Frown.

Thanks for all of the suggestions,
Cal.
 
Posts: 66 | Registered: Fri November 07 2003Reply With QuoteEdit or Delete MessageReport This Post
Junior Member
Posted Hide Post
Aha... I'm happy to find out that I'm wrong Wink

  • I don't need VBA to write a plug-in, unless I want special dialog boxes, etc.

  • Most important of all -- I got multiple-object selection working from within a script!!! ;D Entering a script does not prevent one from having interactive object/face selection! The only problem I have is that the InputBox or MsgBox windows get put into the background once I reselect something in the perspective view, so I am forced to select the "VBScript" task (in the bottom Windows toolbar) to resume my script. I can set the modality of the MsgBox windows, but this seems to only apply to the "VBScript" task. Perhaps I'll find a way to bring the MsgBox to the front periodically. Any ideas?
 
Posts: 66 | Registered: Fri November 07 2003Reply With QuoteEdit or Delete MessageReport This Post
Member
Posted Hide Post
You're doing bad, bad, stuff!  Smile  Having a script running while selecting faces in the main window...  ???

Richard
 
Posts: 2378 | Registered: Fri November 07 2003Reply With QuoteEdit or Delete MessageReport This Post
Member
Posted Hide Post
I don't know how useful this might be, but here goes...

I wrote a level exporter for DarkBasic once that required both active local and global variables in the 3DC environment as well as a way to make them persistent.

This is what I did:

Each object in 3DC is in a group of some form, right? So I used the group names to give the objects a "friendly name", and used the actual object name as a delimited variable string.

For instance, you have a sphere in the scene. You name its group "Ball", and you rename the object within the group to something like "varname0;varname1;varname2;varname3".

Then to any script that needs those specific variables, you would pass the object's (not group) name as a parameter string.

Use the Split() function with ";" as the delimiter and spool those values out into placeholder variables in the script...so you're basically splitting the parameter string, assigning these values to a scriptside variable array, and then using those indices for the variable data you require.

For global data, I use a single object at the scene root level. I name its group Globals and for the object name, I assign an appropriate parameter string. Because you know that the Globals object always exists and its name never changes, you can parse its variable string in any other script.

Anyway, that was the basic approach. I also wrote a frontend to make the parameter strings easier to assign, so all I had to do was select an object and run the SetProperties plugin I wrote, then tick off checkboxes or optionbuttons before clicking OK/Cancel to update the object's parameter string.

I hope this helps. I have to rewrite the whole thing because the level exporter code (and a lot of other things) were lost when my old 200mhz died.

-Mel
 
Posts: 228 | Registered: Fri November 07 2003Reply With QuoteEdit or Delete MessageReport This Post
Junior Member
Posted Hide Post
quote:
You're doing bad, bad, stuff!    Having a script running while selecting faces in the main window...  


Heh...  Is there a better way? I am wide open for ideas! Essentially I would like to do the following:


  • MsgBox "Select object/face #1 for surface collision detection. Press OK when done or Cancel when finished."
  • -> Select an object
  • -> Click OK
  • MsgBox "Select object/face #2 for surface collision detection. Press OK when done or Cancel when finished."
  • -> Select an object
  • -> Click OK
  • MsgBox "Select object/face #3 for surface collision detection. Press OK when done or Cancel when finished."
  • -> Click Cancel
  • MsgBox "Select object #1 for initial trajectory. Press OK when done or Cancel when finished."
  • -> Select an object
  • -> Click OK
  • InputBox "Enter object #1 velocity vector (vX, vY, vZ)" ...
  • etc.
  • MsgBox "Select object #2 for initial trajectory. Press OK when done or Cancel when finished."
  • -> Select an object
  • -> Click Cancel
  • etc.


So, an interactive selection process is pretty important. Currently, I have to generate a textual parameter file by hand.

quote:

Each object in 3DC is in a group of some form, right? So I used the group names to give the objects a "friendly name", and used the actual object name as a delimited variable string.


Hmmm... A very interesting idea! Apart from the likely string length limitation and temporary clutter with oddly-named objects, I think this sounds like a reasonable approach. The global object will help solve a few problems.

Thanks to everyone for your help on this,
Cal.
 
Posts: 66 | Registered: Fri November 07 2003Reply With QuoteEdit or Delete MessageReport This Post
Member
Posted Hide Post
I don't have any better solution for you. If it works for you great! But it wasn't intended to work that way.  Smile

Richard
 
Posts: 2378 | Registered: Fri November 07 2003Reply With QuoteEdit or Delete MessageReport This Post
 Previous Topic | Next Topic powered by eve community  
 

Amabilis Software    3D Canvas Community    3D Canvas Discussion Forums  Hop To Forum Categories  Plug-In Development and Scripting    Physics simulator: Global variable storage?

© Amabilis Software 2003-2007