• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

can someone explain this snipet of code?

mutazoid

Member
Im learning GML but not sure what this is.
Can someone explain this snippet of code?
Thanks :)

scripts[ev_left_button]=noone;
scripts[ev_left_press]=noone;
scripts[ev_mouse_enter]=noone;
scripts[ev_left_release]=noone;
 
A

ajan-ko

Guest
I know what is ev_left_button
but I'm not sure what is scripts[], looks like an array to me.
noone means null or nothing

he is trying to create new variable for key mapping.

This is just speculation, (cuz I don't see more code than that)
I'm just guessing he is mapping mouse button later on
ev_left_button to someone else later on, so

try shift + ctrl + F and try to find
scripts[ev_left_button]

If you confused with some red / yellow code, try to F12 it...
 
W

Wraithious

Guest
Looks like scripts[] is a local variable being set up in a create event somewhere designed to take boolean values, the arguments inside it can be either 0 or 1 or true or false
 
B

Bart Fernhout

Guest
I know what you are asking. The source code is comming from the book Gamemaker game programmingen with GML.
When the GMS comes out trough Humble bundle i bouth it. And the book i got my hands on to.
In the book they do not explain it. The only thing its say that the build in script is initialized.
But in the discription is nothing explained of this.

So if someone from the YOYO team can help us. it would be helpfull.

Bart.
 
B

Bart Fernhout

Guest
I have looking for the awnser and the only thing i could make up was that the calling scripts is an array.
scripts[ev_left_button]=noone;
scripts[ev_left_press]=noone;
scripts[ev_mouse_enter]=noone;
scripts[ev_left_release]=noone;

The scripts is not calling the scripts its just a array. for better reason thay could call this array the_mouse[ev_left_button] = noone;
that was a better way to use the array. Only the ev_left_button is a build in variable and a read only variable.
Its give the status of the mouse button
More i can not find about it.

Bart
 
C

CedSharp

Guest
Im learning GML but not sure what this is.
Can someone explain this snippet of code?
Thanks :)

scripts[ev_left_button]=noone;
scripts[ev_left_press]=noone;
scripts[ev_mouse_enter]=noone;
scripts[ev_left_release]=noone;
I'm ready to bet that this sets a script id to each event ( left button, left pressed, mouse enter and left release ) to be executed.
Setting noone as a value means that there is no script.

Later in the code, you probably will find this:
Code:
if( script_exists( scripts[ <something here> ] ) ) script_execute( scripts[ <something here> ] );
I don't see a situation where this setup would be efficient tho.
Can you provide us with more snippets to help us with the context?
 
B

Bart Fernhout

Guest
During the learnig curve of GM i had a lot of times stoped the learning and fall back to learn again.(My work is requesting this) I had discoverd some things that was written in de book for the first button program.
The events scripts are what they say. EVENTS.
So the array of events is that they are called ony when the event occures.
if you looking back at the script scr_random position : The events mouse enter and mouse leave are called. Just to set the varables or reset the variables.

If you want you can give the ev_mouse_enter just a number you like. The program wil change it when the event is occuring mostly to a 1. but the old value is rememberd and can be set again.

E.G.
Code:
// Indicates that the button is no longer being pressed.
  is_down = false;
/* If the mouse is over the button, the release script is executed and the image index is set to the over frame; otherwise, the image index is set to the default frame. */
  if (is_over)
  {
    image_index = 1;
    if (scripts[ev_left_release] != noone) //This one can be replaced by e.g. -10 if you like But you must have set it before
    {
      script_execute(scripts[ev_left_release]);     //Here the old values is reset (noone) so the next call is true
    }
  }
  else
  {
    image_index = 0;
  }
But remember that the event is called before this piece of code is called and the ev_left_release is changed to a 1 again.
This piece of coding is called predefined coding. So you are sure that when the code is done you have a state in you program that is sure (Reset the variables) So when the event is not occuring the change are not made.

For me its clear now. Its took a long time but the penny(quarter,Dime etc.) is fallen.

If this is not clear for you let me know and i wil try to make a coding the explain it all.
 

samspade

Member
Im learning GML but not sure what this is.
Can someone explain this snippet of code?
Thanks :)

scripts[ev_left_button]=noone;
scripts[ev_left_press]=noone;
scripts[ev_mouse_enter]=noone;
scripts[ev_left_release]=noone;
I'm just another person who doesn't really know the answer with a guess. scripts[] is an array. ev_left_button etc. is a game maker constant. These constants have an event_number, which I assume is unique. So the code is basically doing the following:

Code:
array[5] = -4;
array[15] = -4;
array[36] = -4;
array[42] = -4;
As to why you would do this, I don't know. Given the words and commands I would assume the game later uses commands like if (scripts[ev_mouse_enter] == true) /* do something */; where somewhere else, possibly in the game maker system itself pushing the mouse button turns ev_mouse_enter true. Or something like that.
 
C

CptChuckles

Guest
The only real use I can forsee for an array like that is to be able to call a myriad of different scripts by passing the event_type in as the array index.
Eg, script_execute( scripts[event_type] );

The best guess I have is that the above line will be placed verbatim in each event that we want to do something in, and then the array will be initialized at some point to tell what scripts are fired for which events, if any. So even during runtime, if some state changes and we want the Step event to do something else, we would set scripts[ev_step] = scr_state2; which runs totally different code on the step event than scr_state1. This would enable us to radically change the behavior of an object at runtime (and do so one event at a time). It could make for a powerful state machine, but it limits our use of scripts to those which take no arguments. There are probably better ways to implement a state machine in conjunction with scripts, such as a switch statement, which would allow us to pass arguments to appropriate scripts, and get much more flexibility that way. An array also has the possibility of being called for an index which has not been initialized, or for the index to be out of bounds. It feels like leaving execution up to accident rather than design, which rubs me the wrong way. And if we're using array indices to reference scripts in the first place, I can't see why an enum/int wouldn't be a preferable array index, or why one would choose an array over a ds_map for such an application. Hell, even a series of variables would get the job done, one for each event's current script.
 
Last edited by a moderator:
C

CedSharp

Guest
Between having to use scripts with no arguments which rely on object variables ( so script is directly depending on a specific object ) to using a switch statement which allows calling a script based on the value of one variable,
which doesn't need to be the same for each object, I think it's obvious to say the 'script[ event_type]' methodology seems weird.

I don't understand in which case that setup would be useful. GameMaker already executes all actions ( hence all scripts ) that are tied to an event,
why would you manage those events yourself?

Again, maybe I'm missing something important here, but as of right now, I wouldn't use such piece of code.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
So if someone from the YOYO team can help us. it would be helpfull.
That book has nothing to do with YYG and was written for GM7/8 IIRC... I'm pretty sure YYG would prefer it wasn't sold anymore too as it's not applicable to later and current versions of GM, which causes confusion.

As for the code snippet... It's an array being initialised in an unorthodox way using constants that aren't actually defined as values in the documentation (so if YYG changes their values you're screwed), and each entry is being set to noone which implies that they will store instance IDs later (but not necessarily...). Without context though, it's probably worse than useless and I'd forget about it if it's not explained anywhere in the book.
 

TheouAegis

Member
I don't understand in which case that setup would be useful. GameMaker already executes all actions ( hence all scripts ) that are tied to an event,
why would you manage those events yourself?
Because the code you put in the event is hard-coded into that event and will always run with that event. The method from the book would allow you to change the code in the event on the fly. Suppose your player would initially swing a sword with the left mouse button. Then he gets a powerup and now he throws a boomerang. You could say, "if powerup == boomerang instance_create(x,y,obj_boomerang);" or you could say "scripts[ev_left_button] = throw_boomerang;" when the player picks up the powerup and just have a script execute in the button event while keeping all the weapon codes in their own separate scripts.

... However, I think you'd get an error even with a value of noone trying to execute a non-existent script. Or maybe you wouldn't. I never tried running script_execute() with a negative value.
 
B

Bart Fernhout

Guest
For the people under you. I am a Siemens safety programmer. In the beginning siemens had very much hard code entries to the input and output signals.(And still do) A lare area called heap_space was used toe read and write the outputs. That is pretty nice to have but it gave also a lot of confusing. At our company some guys did like to use them directly, to hardecode the output. But did not bother to explain why. So when i am on the site to install the hardware and start the software. This will go wrong. During installation sometimes the I/O of one part is not allowed to put on one unit. Then i must changed that. No problem but in the code is nowhere to find the name giving for that piece of output. (That is a normal way of coding at our company)

At our company when we got new people who do this we learn them very fast to never do that. It is fast, but bug hunting using a variable name make it confusing, then a special called hardcode peice. Like is this bit the bit that is going wrong, or the other.
eg. In the siemens programming when we us an input of a button like start or stop. There is a fast way of coding that but a person is never that fast to hit a button fully down, in 0.1 uS. so in the code its called IX_STOP and IX_START. Where we know that "I" stands for Input and the "X" stands for for the outside world. This is faster in bug hunting. Say the user hit the start button but nothing is happening. During hunting on the nam IX_START the coder mistakenly makes the name AX_STRAT. Then when hitting the start button there is never taking an action. Cause during bug huning the input never showed up. Then the bug of wrong name is found very fast.

But here in the example the code writter was assuming that everybody was knowing this. And thats wrong.

Its make the program faster but not nicer. Special if someone else gets your code. They are wondering like we all do, with this code. So Like NOCTURNE say forget this piece of intro. Cause that is it and nothing else. Try to learn the rest and find also other good videos and books. So you can make your games like you want it. And we users can play your game and enjoy it.
 
Top