SRL Archive - FAQ

Creating Objects in Simba?


Vacrin

2015-04-18 22:12:50

Hey everyone,

So I have been coding for awhile, but I'm new to Simba, I was curious about how making and implementing objects worked in Simba. Such as the syntax needed to import the object file, and making procedures and functions public, or by default are they public?

Thank you :)

KeepBotting

2015-04-18 22:21:31

I'm going to assume (as a programmer limited to Pascal and not much else) that "importing objects" == "including files."

To include a file, do
{$i filename.simba}
at the top of your script, after the program's identifier which looks like
program name;

This will allow you access to all routines, variables, and constants in the included file. By default, everything is public (I don't think there's a way to make them private)

Oh, and the pathing starts in C:/Simba/Includes/ by default, you can use ../filename.simba to go up a directory.

I guess, syntactically, "objects" refers to object-oriented programming concepts, which can be more easily accomplished by splitting one huge script into a multitude of organized includes?

Vacrin

2015-04-18 22:28:18

Sorta, its easier to deal with data when it structured using objects, they can also preform little tasks as well, but I like them as data holders more. Thanks to SMART all the in-game commands are really easy

KeepBotting

2015-04-18 22:52:47

From: Vacrin
Sorta, its easier to deal with data when it structured using objects, they can also preform little tasks as well, but I like them as data holders more. Thanks to SMART all the in-game commands are really easy


Yes, included files are exactly that, data holders. I believe that's what you're looking for.

You can check out a more full-fledged guide here: https://villavu.com/forum/showthread.php?t=109402

tls

2015-04-18 23:04:09

The closest thing to objects are records. They are similar to structs in c, but you can define functions and procedures same as oop( object.function() ).

https://github.com/SRL/SRL/blob/master/shared/smartbase.simba is a good example of this.

Richard

2015-04-19 09:15:18

Pascal is procedural, not OOP, but you can use data structures called records. Unfortunately you can't change the visibility of functions/procedures, once you import a file, everything is assumed to be public.

Olly

2015-04-19 09:21:30

From: Richard
Pascal is procedural, not OOP, but you can use data structures called records. Unfortunately you can't change the visibility of functions/procedures, once you import a file, everything is assumed to be public.


Well there is object pascal, which about everyone uses today.

Although the problem here is that Lape (our interpreter) does not support classes.

Richard

2015-04-19 09:23:10

From: Olly
Well there is object pascal, which about everyone uses today.

Although the problem here is that Lape (our interpreter) does not support classes.


I stand corrected!

rj

2015-04-19 11:30:22

Quick easy record example:


type
TRectangle = record
x, y, w, h:integer;
end;

var
rectangle:Trectangle;

procedure TRectangle.new(xx, yy, ww, hh:integer);
begin
self.x := xx;
self.y := yy;
self.w := ww;
self.h := hh;
end;

function TRectangle.getBounds():Tbox;
begin
result := intToBox(self.x, self.y, self.x + w, self.y + h);
end;

begin
rectangle.new(5, 5, 10, 10);
writeln(rectangle.getBounds());
end.

Vacrin

2015-05-12 14:03:52

From: rj
Quick easy record example:


type
TRectangle = record
x, y, w, h:integer;
end;

var
rectangle:Trectangle;

procedure TRectangle.new(xx, yy, ww, hh:integer);
begin
self.x := xx;
self.y := yy;
self.w := ww;
self.h := hh;
end;

function TRectangle.getBounds():Tbox;
begin
result := intToBox(self.x, self.y, self.x + w, self.y + h);
end;

begin
rectangle.new(5, 5, 10, 10);
writeln(rectangle.getBounds());
end.


I believe that is exactly what I am looking for! Just making sure, but you can make array's of objects right?

rj

2015-05-12 14:56:47

From: Vacrin
I believe that is exactly what I am looking for! Just making sure, but you can make array's of objects right?


Yes.

2 ways:

var
rectangles:array of TRectangle;

Or you can create a type

type
TRectangleArray = array of TRectangle;

var
rectangles:TRectangleArray;

You can even define the type so that it must have a certain length

TRectangleSomething = array[0..3] of TRectangle;

Vacrin

2015-05-12 16:16:25

Thank you so much! That is exactly what I was looking for!