Here's a chunk of code that WORKS when it's on the main timeline:
var DysonTarget:String = "S"+(random(40)+1);
this[DysonTarget].MyType = "Dyson Sphere";
this[DysonTarget].gotoAndStop(this[DysonTarget].MyType);
It's choosing a number between 1 and 40, adding an S before it, and going into one of forty movie clips on the main stage with instance name S1, S2. . . S40 etc. Then it will display an image in the chosen clip. But to make this truly work the way I want to, I have to put the above code inside a movie clip. So I tried this, after declaring my variable on the main stage:
_root.this[DysonTarget].MyType = "Dyson Sphere";
_root.this[DysonTarget].gotoAndStop(this[DysonTarget].MyType);
It didn't compile, the error message said "Expected a field name after the '.' operator. Trying it with _parent returned the same message. With _level0 didn't work at all, and placing the _root and _parent inside the bracket didn't work either. I haven't been able to find any answer online because trying to type "this" into a search is too vague to return an answer about the actual command.
. . .help me :(
A friend of mine who is a software developer helped me on this one. Here's what we figured out:
First you declare variable DysonTarget on the main timeline:
var DysonTarget:String = "S" + (random(40)+1);
Then inside the movie clip, use this:
_level0[DysonTarget].MyType = "Dyson Sphere';
_level0[_level0.DysonTarget].gotoAndStop(_level0[_level0.DysonTarget].MyType);
I've tried this a few other ways, and the above method is the only one that works the way it's supposed to. But it works! My impression is the brackets tell it to look for an object named what the variable is set to, rather than an object with the same name as the variable.
Related
Forgive me, I'm not a proper JS programmer and still getting my head around a lot of concepts.
Suppose one had a group of similar, 2-frame/2-state rollover movie clips nested inside a containing clip, which has the instance name "Map". Each clip uses a 4 digit ID number preceded by an "s" as an instance name – e.g., "s6566".
Suppose one then wanted to capture those respective instance names to define a variable, such that one small script could allow each of these movie clips to display their ID on rollover/active state (in this case "6566"), across multiple files.
Ultimately I have thousands of these little clips spread across several dozen documents, and it seems it should be fairly simple to grab each symbol's instance name/ID, strip off the "s" from the beginning (there because instance names can't begin with a numeral), and apply said ID as dynamic text to it's respective symbol's rollover/active frame.
Is there a method of achieving this goal? I wish I had some example code to include here, but I'm not quite sure how to begin, other than to lay out the problem thusly. Haven't yet been able to find any info on capturing instance names, and I'm not sure whether it's possible. Thanks.
Children of MovieClips are stored as references using their instance name. You can see the format in the exported library JS file. Note that Animate will convert some instance names to remove unsupported characters or duplicates.
Here is some untested pseudo-code to get you started.
// You can iterate a MovieClip and get the names
for (var name in someMovieClip) {
// Ignore anything not starting with an s
if (name.substr(0,1) != "s") { continue; }
// remove the s
var newName = name.substr(1);
// The child can be accessed using bracket-access with its name
var child = someMovieClip[name];
// The child should have text instances if it is set up how you described
// Set the text to the newName
child.textInstance.text = newName
}
Don't forget to update the stage after you make changes. If you already have Ticker set up to do that, it should update immediately.
I hope that helps. If you have follow-up questions, let me know.
I'm making a simple concept game, in which I've made buttons which are targets, when the user clicks said targets, it executes this code:
on (release){
_global.targetCount++;
Target1._visible=false;
if(_global.targetCount==3){
gotoAndStop(4);
}
}
the global variable was declared on the frame like this:
_global.targetCount = 0;
and the buttons do disappear when I click on them like they should, but as soon as I click the final 3rd one and it disappears, it doesn't successfully check that the if(_global.targetCount==3) and proceed to the 4th frame.
I've tried declaring the variable differently like so:
var targetCount:Number = 0;
also tried doing it like this but on using the check code button it said my syntax was wrong:
var _global.targetCount:Number = 0;
and calling every instance as just targetCount, but that didn't fix it either,
I've searched and tinkered with the code, but I can't find clear examples on global variables, the little I've used here I found by reading this:
https://www.kirupa.com/developer/actionscript/tricks/global.htm
So I was wondering if anyone here could help me by letting me know the many mistakes I've done, and how to improve them.
All help is gladly appreciated!
Every keyframe on stage is new closure. If you have variable on frame 2 and you want change/set or read value of this variable on frame 3, that variable does not exist and it is undefined. If you will try increment that undefined value you get NaN and gotoAndStop(NaN) doing nothing.
Insert trace(_global.targetCount); between _global.targetCount++; and Target1._visible=false; for debug.
Very new to python - just started actually using it yesterday. I'm running a for loop that scans a text file and copies specific parts of it into variables that will then be put into a class "post". I want to create a new post at the bottom of the loop, named "post0", "post1", and so on, corresponding with the number of times the for loop has been run. This is what I'm trying to use:
postname = globals()['post%s' % s]
And I currently am trying to have it print the name of the post every time it creates one with a simple print(postname). 's' is the variable the for loop runs off of, if that makes sense. It starts at 0 and runs up to the number of lines in the text file, currently 424.
When I run the code, it returns "KeyError: post0". What am I doing wrong?
Also, reading around here it seems that creating variables this way is bad practice. If there is a more efficient way to do it I'd be happy to try that instead, but I'd also like to know how to make this method work just so I understand the concept. Thanks.
Edit: Problem solved! See my answer below.
I created a list postlist = [] outside of the loop, then inside the loop I append the list with a "post_" where "_" is the 's' variable. Looks like this:
postlist.append('post%s' % s)
Python is cool!
I just wanted to know how you put a cursor in a specific place in a live template for IntelliJ
For example:
# $var$ is an insance of the $objectType$ class
assert isinstance($var$, $objectType$)$END$
What happens here is that your cursor gets dragged to $var$ in the comment string first and then to your other values inside assert. What I wanted to know is how you chose where the cursor goes first.
I've read the documentation, but this is not mentioned, although a lot of other things are.
You can arrange the order that your variables are visited in. You find the information under bullet number five in this IntelliJ help document: http://www.jetbrains.com/idea/webhelp/creating-and-editing-template-variables.html
To arrange variables in the order you want IntelliJ IDEA to switch between associated input fields, use the Move Up and Move Down buttons.
Edit
You have to update the macro definition to similar to this:
# $varComment$ is an insance of the $objectTypeComment$ class
assert isinstance($var$, $objectType$)$END$
And then you define the order and expression to something like this (I didn't have any good expression for the var and orderType for you):
Since you fill in the Skip if defined for the two comment variable they will just take the values from the var and orderType and fill it in. This will do exactly what you are looking for :-)
I'm using GameMaker:Studio Pro and trying to execute a script stored in a variable as below:
script = close_dialog;
script_execute(script);
It doesn't work. It's obviously looking for a script named "script". Anyone know how I can accomplish this?
This question's quite old now, but in case anyone else ends up here via google (as I did), here's something I found that worked quite well and avoids the need for any extra data structures as reference:
scriptToCall = asset_get_index(scr_scriptName);
script_execute(scriptToCall);
The first line here creates the variable scriptToCall and then assigns to it Game Maker's internal ID number for the script you want to call. This allows script_execute to correctly find the script from the ID, which doesn't work if you try to pass it a string containing the script name.
I'm using this to define which scripts should be called in a particular situation from an included txt file, hence the need to convert a string into an addressable script ID!
You seem to have some confusion over how Game Maker works, so I will try to address this before I get around to the actual question.
GML is a rather simple-minded beast, it only knows two data types: strings and numbers. Everything else (objects, sprites, scripts, data structures, instances and so on) is represented with a number in your GML code.
For example, you might have an object called "Player" which has all kinds of fancy events, but to the code Player is just a constant number which you can (e.g.) print out with show_message(string(Player));
Now, the function script_execute(script) takes as argument the ID of the script that should be executed. That ID is just a normal number. script_execute will find the script with that ID in some internal table and then run the script.
In other words, instead of calling script_execute(close_dialog) you could just as well call script_execute(14) if you happened to know that the ID of close_dialog is 14 (although that is bad practice, since it make the code difficult to understand and brittle against ID changes).
Now it should be obvious that assigning the numeric value of close_dialog to a variable first and then calling script_execute on that variable is perfectly OK. In the end, script_execute only cares about the number that is passed, not about the name of the variable that this number comes from.
If you are thinking ahead a bit, you might wonder whether you need script_execute at all then, or if you could instead just do this:
script = close_dialog;
script();
In my opinion, it would be perfectly fine to allow this in the language, but it does not work - the function call operator actually does care about the name of the thing you try to call.
Now with that background out of the way, on to your actual question. If close_dialog is actually a script, your suggested code will work fine. If it is an extension function (or a built-in function -- I don't own Studio so what do I know) then it does not actually have an ID, and you can't call it with script_execute. In fact, you can't even assign close_dialog to a variable then because it does not have any value in GML -- all you can do with it then is call it. To work around this though, you could create a script (say, close_dialog_script which only calls close_dialog, which you can then use just as above.
Edit: Since it does not seem to work anyway, check whether you have a different resource by the name of close_dialog (perhaps a button sprite). This kind of conflict could mean that close_dialog gives you the ID of the sprite, not of the script, while calling the script directly would still work.
After much discussion on the forums, I ended up going with this method.
I wrote a script called script_id()
var sid;
sid = 6; //6 = scriptnotfound script :)
switch (argument0) {
case "load_room":
sid = 0;
break;
case "show_dialog":
sid = 1;
break;
case "close_dialog":
sid = 3;
break;
case "scrExample":
sid = 4;
break;
}
return sid;
So now I can call script_execute(script_id("close_dialog"));
I hate it, but it's better than keeping a spreadsheet... in my opinion.
There's also another way, with execute_string();
Should look like this:
execute_string(string(scriptName) + "();");