3D Canvas Community
3D Canvas Discussion Forums
Plug-In Development and Scripting
Physics simulator: Global variable storage?|
Go
![]() |
New
![]() |
Find
![]() |
Notify
![]() |
Tools
![]() |
Reply
![]() |
|
|
Junior Member |
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. |
||
|
|
Member |
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 |
|||
|
|
Junior Member |
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. |
|||
|
|
Member |
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 |
|||
|
|
Junior Member |
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. |
|||
|
|
Member |
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) ... |
|||
|
|
Junior Member |
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 Thanks, Cal. |
|||
|
|
Member |
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 |
|||
|
|
Junior Member |
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:
Cal. |
|||
|
|
Member |
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 |
|||
|
|
Junior Member |
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. |
|||
|
|
Member |
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 |
|||
|
|
Junior Member |
Geo --
quote: 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: 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 Thanks for all of the suggestions, Cal. |
|||
|
|
Junior Member |
Aha... I'm happy to find out that I'm wrong
|
|||
|
|
Member |
You're doing bad, bad, stuff!
Richard |
|||
|
|
Member |
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 |
|||
|
|
Junior Member |
quote: Heh... Is there a better way? I am wide open for ideas! Essentially I would like to do the following:
So, an interactive selection process is pretty important. Currently, I have to generate a textual parameter file by hand. quote: 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. |
|||
|
|
Member |
I don't have any better solution for you. If it works for you great! But it wasn't intended to work that way.
Richard |
|||
|
| Previous Topic | Next Topic | powered by eve community |
| Please Wait. Your request is being processed... |
|
3D Canvas Community
3D Canvas Discussion Forums
Plug-In Development and Scripting
Physics simulator: Global variable storage?
