ENIGMA Forums

Outsourcing saves money => Issues Help Desk => Topic started by: ojars on May 31, 2019, 06:50:41 am

Title: Error implementing struct
Post by: ojars on May 31, 2019, 06:50:41 am
Hi!
I got an error, when tried to implement a struct into my code. I followed an example from https://enigma-dev.org/forums/index.php?topic=2674.msg25861#msg25861 (https://enigma-dev.org/forums/index.php?topic=2674.msg25861#msg25861), where this example worked on Goombert's computer. In Definitions I created a struct
Code: [Select]
struct circle {
  var x = 0, y = 0;
  double radius;
  double get_area() { return pi * radius * radius; } // Simple method
  circle(double r = 1) { radius = r; }; // Optimizing this falls on the language plugin
  ~circle() { }
};

Script
Code: [Select]
///test_circle()

circle *test = new circle();
test.x = 200;
show_message(string(test.x));

In the o_game Create event
Code: [Select]
test_circle();
And in the Cleanup
Code: [Select]
delete test;
This is an error:
Syntax error in script `test_circle'
Line 3, position 26 (absolute 45): Unknown function or script `circle'

Then I put the code from script directly into the create event and the error is the same:
Event[0, 0] Check `o_game::create...Syntax error in object `o_game', Create event:0:
Line 4, position 26 (absolute 64): Unknown function or script `circle'

What's wrong in my code?
Title: Re: Error implementing struct
Post by: Goombert on June 01, 2019, 03:44:47 am
If I recall correctly, I think it's possible the definitions frame may be bugged and isn't saving. In other words, it reverted immediately after you closed the game settings frame. I am planning to look into this area soon now that I have fixed included file editing, but am extremely busy. I will update if I get any of it done, thank you for your patience. As you are discovering, ENIGMA may be rough around the edges, but this situation is drastically improving with our introduction of continuous integration testing.
Title: Re: Error implementing struct
Post by: ojars on June 01, 2019, 04:55:20 am
Thanks, I'l be patient.
Title: Re: Error implementing struct
Post by: ojars on June 10, 2019, 02:04:51 pm
Eventually I found a right way how to implement structs: declaration should be done in Definitions, initialization should be done in Initialization and no need to create instance on the heap, simply put
Code: [Select]
circle test;and the code works. Unfortunately, ENIGMA cannot recognize inline function get_area(). When I tried to calculate area with a function, I got an error
Quote
  Event[8, 0] Check `o_game::draw...Syntax error in object `o_game', Draw event:0:
Line 24, position 30 (absolute 768): Unknown function or script `get_area'

At least now I can use struct for lightweight objects, I believe it is more convenient than data structures;

Title: Re: Error implementing struct
Post by: Dragonite on June 13, 2019, 11:00:39 pm
Alternatively, you could try using GMEdit's type magic:
https://github.com/GameMakerDiscord/GMEdit/wiki/Using-type-magic
https://github.com/GameMakerDiscord/GMEdit/wiki/Using-%23import-magic#import-compkgsome-as-nsalias
Title: Re: Error implementing struct
Post by: Goombert on June 15, 2019, 01:04:05 pm
Yeah, I recently had the chance to try GMEdit, it's pretty cool, I recommend too.