Ads keep us online. Without them, we wouldn't exist. We don't have paywalls or sell mods - we never will. But every month we have large bills and running ads is our only way to cover them. Please consider unblocking us. Thank you from GameBanana <3

VScript/Squirrel - Abstract/Inheritance Example

A Config Script for Counter-Strike: Global Offensive

Members see zero ads. Membership is 100% free
92 2,891 bytes Raw Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
class Shape //Abstract Base Class, These fields are important and is the fundamentals of a Shape!  
{
    //The shapes colour and type
    shapeColour = null; shapeType = null;

    //The shapes location on screen
    pointX = null; pointY = null;
   
    //The shapes size on screen
    sizeX = null; sizeY = null;

    //Constructor that initialize the fields (Remember, Encapsulation!)
    constructor(colour, _type, x, y, w, h)
    {
        this.shapeColour = colour;
        this.shapeType = _type;
        this.pointX = x;
        this.pointY = y;
        this.sizeX = w;
        this.sizeY = h;
    }
   
    //Function which display back the information to screen
    function Display()
    {
        ::print(format("There is a shape thats a '%s' which is the colour '%s', located at X:%i, Y:%i with Width:%i and Height:%i\r\n", this.shapeType, this.shapeColour, this.pointX, this.pointY, this.sizeX, this.sizeY))
    }

    function SetChangePoints(x,y) { this.pointX = x; this.pointY = y; }
}

class Square extends Shape
{
    //Square now has everything from Shape, only need to call the base constructor!
    constructor(colour, x, y, w, h) { Shape.constructor(colour, "Square", x, y, w, h) }

    //Overriding the Display() inherited from Shape
    function Display()
    {
        ::print(format("Hi, I am a Sqaure! I am '%s', located at X:%i, Y:%i with Width:%i and Height:%i\r\n", this.shapeColour, this.pointX, this.pointY, this.sizeX, this.sizeY))
    }
}

class Triange extends Shape
{
     //Triange now has everything from Shape, only need to call the base constructor!
    constructor(colour, x, y, w, h) { Shape.constructor(colour, "Triange", x, y, w, h) }

    //Overriding the Display() inherited from Shape
    function Display()
    {
        ::print(format("Sup, I'm triangle! I'm '%s', I'm at X:%i, Y:%i with Width:%i and Height:%i!!!\r\n", this.shapeColour, this.pointX, this.pointY, this.sizeX, this.sizeY))
    }
}

class Circle extends Shape
{
     //Circle now has everything from Shape, only need to call the base constructor!
    constructor(colour, x, y, w, h) { Shape.constructor(colour, "Circle", x, y, w, h) }
}

local shapeArray =
[
    Square("red", 0, 0, 20 20),
    Triange("blue", 0, 0, 20 20),
    Circle("green", 0, 0, 20 20)
]

function AddPrintBar()
{
    ::print("============================================================================================================\r\n")
}

function PrintArray(arr)
{
    AddPrintBar()
    foreach (i in arr)
        i.Display()
    AddPrintBar()
}

function rndint(max) {
    local roll = 1.0 * max * rand() / RAND_MAX;
    return roll.tointeger();
}

PrintArray(shapeArray)

for(local i = 0; i < shapeArray.len(); i++)
    shapeArray[i].SetChangePoints(rndint(100), rndint(100))

PrintArray(shapeArray)

Learn VScript - Do it. Even a Squirrel can!

Here is an example of what an OOP class in Squirrel looks like (VScript)

IF DOWNLOADING, SAVE FILE AS .nut


Use this site to experiment with this code: https://tio.run/#squirrel

In here is:
  • Abstraction (Base class “Shape Class”)
  • Inheritance (Square and Triangle inherit their own Display() method) – known as an override
  • Polymorphism – Class has the same fields, methods but each element in the array is different (Each object is unique, aren’t the same)
  • Encapsulation – Only assigning the fields using a constructor, fields are kept private – hidden!

The code does the following:
  • Display what the array has before directly changing the position of each shape
  • Iterate through the array and assign each shapes X & Y coordinates between 0 and 100 (Randomise)
  • Display back the new array again

Comments

Sign up to access this!

No comments yet

Embed

menu
Share banner
Image URL
HTML embed code
BB embed code
Markdown embed code

Credits

Creator
TheE7Player
TheE7Player Joined 1mo ago
Offline
Coder, hehe.

Submitter

Sign up to access this!
Sign up to access this!
Sign up to access this!

Game

Sign up to access this!

Category

License

menu

Share

  • Share on Reddit
  • Share on Twitter
  • Share on Facebook
  • favorite 1
  • remove_red_eye 412
  • access_time 1mo
  • access_time 1mo

More Other/Misc Scripts