Pages: 1
Author Topic: Error implementing struct  (77,329 Views)
Offline (Unknown gender) ojars

Member
Joined: May 2019
Posts: 16
View profile
Posted on: May 31, 2019, 11: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, where this example worked on Goombert's computer. In Definitions I created a struct 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///test_circle()

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


In the o_game Create event test_circle();

And in the Cleanupdelete 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?
Offline (Unknown gender) Goombert

Developer
Joined: Jan 2013
Posts: 2,991
View profile
Reply #1 Posted on: June 01, 2019, 08: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.
Offline (Unknown gender) ojars

Member
Joined: May 2019
Posts: 16
View profile
Reply #2 Posted on: June 01, 2019, 09:55:20 AM
Thanks, I'l be patient.
Offline (Unknown gender) ojars

Member
Joined: May 2019
Posts: 16
View profile
Reply #3 Posted on: June 10, 2019, 07: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
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
QuoteEvent[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;

Offline (Unknown gender) Dragonite

Member
Joined: Mar 2017
Posts: 20
View profile
Reply #4 Posted on: June 14, 2019, 04:00:39 AM
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
Offline (Unknown gender) Goombert

Developer
Joined: Jan 2013
Posts: 2,991
View profile
Reply #5 Posted on: June 15, 2019, 06:04:05 PM
Yeah, I recently had the chance to try GMEdit, it's pretty cool, I recommend too.
Pages: 1