• 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!
  • Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Question - Code [SOLVED] Array not recognised in GMS2

Y

yoyowan

Guest
I am playing with a script from Hryx working like a charm in GMS 1.4 but giving me error with GMS 2 (last version). Can not find what is going on...

message :
trying to index a variable which is not an array

osc_receive() script :
Code:
/* osc.gml
 * by hryx 2015
 * Public Domain
 *
 * OSC event callback.
 * Parses incoming OSC packets and returns an array: Address pattern and args.
 * An integer (false or a negative number) is returned upon error.
 * Each OSC message will be a list containing an address pattern and
 * optionally argument values. Values are translated to native GML types:
 * string, real (blob not supported).
 * Currently, #bundle messages are not supported.
 * This script must be called in an object's Networking Event through the GUI.
 */

// Information from the Networking event
var server = async_load[? "id"];
var type = async_load[? "type"];
var buf = async_load[? "buffer"];
var size = async_load[? "size"];

// Only interested in payloads on the server socket
if (server != osc_listen_socket) {
    return -1;
}
if (type != network_type_data) {
    return -1;
}

// Local 💩💩💩💩
var i;
var j;
var addrpattern;
var typetagstr;
var typetags;
var arguments;
var buf_tmp = buffer_create(4, buffer_fixed, 4);
var messages;

//// Parse the packet ////

// Address pattern
buffer_seek(buf, buffer_seek_start, 0);
addrpattern = buffer_read(buf, buffer_string);
if (string_length(addrpattern) < 1) {
    return false;
}

// Type tags
while (buffer_tell(buf) % 4 != 0) {
    buffer_seek(buf, buffer_seek_relative, 1);
}
typetagstr = buffer_read(buf, buffer_string);
if (string_char_at(typetagstr, 1) != ",") {
    return false;
}
i = 1;
while (true) {
    i++;
    var ch = string_char_at(typetagstr, i);
    if (ch == "") {
        break;
    }
    switch (ch) {
        case "i":
        case "f":
        case "s":
            typetags[i - 2] = ch;
            break;
        default:
            return false;
    }
}

// Arguments
while (buffer_tell(buf) % 4 != 0) {
    buffer_seek(buf, buffer_seek_relative, 1);
}
for (i = 0; i < array_length_1d(typetags); i++) {
    // Don't go past the end of the buffer
    var pos = buffer_tell(buf);
    if (pos >= size) {
        return false;
    }
    // Grab next argument
    tag = typetags[i];
    switch (tag) {
        case "s":
            arguments[i] = buffer_read(buf, buffer_string);
            break;
        case "i":
        case "f":
            // We must swap to big-endian for OSC
            buffer_seek(buf_tmp, buffer_seek_start, 0);
            var k = 0;
            var j = 3;
            for (j = 3; j >= 0; j -= 1) {
                var byte = buffer_peek(buf, pos + j, buffer_u8);
                buffer_poke(buf_tmp, k, buffer_u8, byte);
                k++;
            }
            if (tag == "i") {
                arguments[i] = buffer_read(buf_tmp, buffer_s32);
            }
            else if (tag == "f") {
                arguments[i] = buffer_read(buf_tmp, buffer_f32);
            }
            buffer_seek(buf, buffer_seek_relative, 4);
            break;
        default:
            return false; // Unreachable
    }
    // Seek to end of 4-bytes padding
    while (buffer_tell(buf) % 4 != 0) {
        buffer_seek(buf, buffer_seek_relative, 1);
    }
}

// That's it
messages[0] = addrpattern;
for (i = 0; i < array_length_1d(arguments); i++) {
    messages[i + 1] = arguments[i];
}

return messages;
 
Last edited by a moderator:
I

icuurd12b42

Guest
the error comes with a line number and a line of code... along with which variable you tried to access
 
Y

yoyowan

Guest
the error comes with a line number and a line of code... along with which variable you tried to access
Sorry about not being precise :

FATAL ERROR in
action number 1
of Draw Event
for object init:

trying to index a variable which is not an array
at gml_Object_init_Draw_0 (line 3) - draw_text(50,50,messages[1]);

Only 1 object in the room (3 events)

create event :
Code:
osc_init("192.168.1.91",9000,8000);
draw event :
Code:
draw_text(50,50,messages[1]);
async - networking event :
Code:
messages = osc_receive();
You also need the osc_init script :
Code:
/* osc.gml
 * by hryx 2015
 * Public Domain
 *
 * Initialize OSC settings.
 * This must be called before any other OSC functions.
 * Usage:
 *   osc_init("127.0.0.1", 8140, 8142);
 */

// Validate args
if (argument_count < 3) {
    show_error("osc_init(): Not enough arguments.", true);
}
osc_send_url = argument[0];
osc_send_port = argument[1];
osc_listen_port = argument[2];
if (!is_string(osc_send_url)) {
    show_error("osc_init(): Expected URL (string) at argument 0.", true);
}
else if (!is_real(osc_send_port)) {
    show_error("osc_init(): Expected send port (number) at argument 1.", true);
}
else if (!is_real(osc_listen_port)) {
    show_error("osc_init(): Expected listen port (number) at argument 2.", true);
}

// Create network objects
osc_send_buf = buffer_create(512, buffer_grow, 1);
osc_send_socket = network_create_socket(network_socket_udp);
osc_listen_socket = network_create_server_raw(network_socket_udp, osc_listen_port, 1);

// Set init flag
osc_is_setup = true;
 
I

icuurd12b42

Guest
try draw_text(50,50,messages);

it will show what sort of data message is

the code you posted does not show the relationship of the osc_init and the message variable aside calling the blackbox script osc_receive
 

Juju

Member
@icuurd12b42 I presume the first snippet in OP is osc_receive()... though this was by no means clear.

The first snippet only initialises messages as an array if the script doesn't encounter an error. Put in some debug commands (show_debug_message or F9 breakpoints) and see where the script is exiting.
 
Y

yoyowan

Guest
try draw_text(50,50,messages);

it will show what sort of data message is

the code you posted does not show the relationship of the osc_init and the message variable aside calling the blackbox script osc_receive
There is a relation between the 2 scripts : osc_init defines vatiable "osc_listen_socket" and osc_receive needs it

This does not explain why exact same project does work in GMS 1.4 : at start it opens a message box and shows all elements defined in the array osc_receive()
 
Y

yoyowan

Guest
[/QUOTE]
@icuurd12b42 I presume the first snippet in OP is osc_receive()... though this was by no means clear.

The first snippet only initialises messages as an array if the script doesn't encounter an error. Put in some debug commands (show_debug_message or F9 breakpoints) and see where the script is exiting.
Yes it is osc_receive() on first post, I have edited the message to be clearer thanks ;)

Have to learn a little about breakpoints and coming back...
 
Last edited by a moderator:
I

icuurd12b42

Guest
so the function returns -1, false or an array!!!

>try draw_text(50,50,messages);
this still applies.

figure out why it's failing and do handle the return of the function call a little less blindly. I know this does not fix the problem but it does fix your issue of the game crashing when the function fails.

do a
is_array(message)
 

gamemaker90

Member
I will assume that the osc_init script is in the create event, as they usually are. Now I noticed two things: 1. you use the variable interchangeably as an array and a regular variable. I nkow you can only do one or the other. 2. I noticed in the above script on the first post, you did:
var messages;
That means this variable cannot be accessed outside of the event that it was initialized in. To better put it:
if I initialize a variable like so:
var msg;
msg = "string";
msg[0] = "string0";

Then the variable has already been initialized as a regular one and NOT AN ARRAY!
I could be wrong, but in my verification test I just ran, my theory checks out. I can post the gmz if you'd like. :)
 
I

icuurd12b42

Guest
^ you are confusing us.

In gml, a variable is a container that can hold any data interchangeably. the data can be a value, a string, an array and so on. if you set the variable to a value then later set it to an array, the variable now holds an array. if you then set the variable to a value, like the script does (it returns -1 or false on fail or an array on success) then the variable will hold a value... irrespective at what the first data type the variable was used for...
 

Juju

Member
I could be wrong
Yes, you are. Variable types are mutable (and I regularly set an array variable to a real to quickly dump the contents of an array for refilling).

This does not explain why exact same project does work in GMS 1.4
It might be that your project in GMS1.4 didn't have issues with osc_receive()and never returned a non-array. Or there might be some weirdness with how GMS1 is indexing variables (that aren't arrays) as reals. Dunno! The important thing is to get it working in GMS2.
 
Y

yoyowan

Guest
try draw_text(50,50,messages);

it will show what sort of data message is
Error

Variable init.messages(100010, -2147483648) not set before reading it.
at gml_Object_init_Draw_0 (line 5) - draw_text(50,50,messages);
 
Y

yoyowan

Guest
@icuurd12b42 I presume the first snippet in OP is osc_receive()... though this was by no means clear.

The first snippet only initialises messages as an array if the script doesn't encounter an error. Put in some debug commands (show_debug_message or F9 breakpoints) and see where the script is exiting.
Same message when I put my breakpoint first line with osc_receive() script :
Code:
FATAL ERROR in
action number 1
of Draw Event
for object init :

trying to index a variable which is not an array
at gml_Object_init_Draw_0(line 3) - draw_text(50,50,messages[1]);
When I put breakpoint in osc_init() script and step in I can go through it then it goes to create event then it goes to the draw event AND after I get a red alert in the debugger : Target connection lost (and no more game window)
 
Last edited by a moderator:
I

icuurd12b42

Guest
Error

Variable init.messages(100010, -2147483648) not set before reading it.
at gml_Object_init_Draw_0 (line 5) - draw_text(50,50,messages);
so obviously
messages = osc_receive();
was never called
 

chamaeleon

Member
My initial reaction without reading too much of the code and trying to figure out all the details is that your async event is responsible for setting the messages variable using the script. Isn't it possible your draw event is fired many times (well, one time and crashing.. but if you didn't use the variable it could be many frames) before that async event is invoked, leading to an attempt to read a variable that is not yet defined?
 
Y

yoyowan

Guest
Hurrayyyyy !!!!

Found the problem, so stupid...
I was playing with this script with GMS 1.4 and I was having for the first time the same error message :(
What has changed ? my wifi card was shut down. But in GMS2 I have done my tests with my wifi card on. SO the problem was simply my firewall not allowing communications with GMS2

Thanks to all for your precious time, great community !
 
I

icuurd12b42

Guest
^ that before this

also it means your code is not handling errors
 
Top