Topic:   Objects   (Read 62488 times)


0 Members and 1 Guest are viewing this topic.

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Objects
« on: January 23, 2011, 10:22:33 AM »
One of Sc's greatest weaknesses is the lack of objects.

So what do I mean?

An object is like a normal variable. Like an int or a string. Except it can hold multiple numbers and strings. Here's an example.

Bad Guy Object Contains:
Name
HP
STR
DEF
Weapon

If Sc worked with objects it'd look like this:
Code: [Select]
//This creates the object
LET badGuy = new BadGuy
badGuy.name = "Darktanyon"
badGuy.hp = 5
badGuy.str = 3
badGuy.def = 6
badGuy.weapon = "Whip of Misfortune"
//Now that the bad guy is all set lets add him to an array
APPENDARRAY badGuyArray, badGuy

You see how useful this is? One object holds many variables that you can store in a single array.

So I'm thinking just like we artificially created arrays before Sc officially got them, we should artificially create objects.
Anyone have any ideas?

WarHampster


  • GMG Extraordinaire

  • ***


  • Posts: 1501

  • The People's Moderator
    • Arcade of the Absurd
Re: Objects
« Reply #1 on: January 23, 2011, 10:29:36 AM »
They wouldn't be real objects, the best we could fake would probably be structs.

Here's how it would have to work:

SC doesn't support arrays of arrays, so we'd have to use strings to store all the local variables. We could do what we used to do for arrays, write a method to process a specially formatted string as an struct. Then you could store the "structs" in an array.

Ok so we would make a method for each class. "MAKESTRUCT localvar1 localvar2... string$"

Then the method would format the string parameter as an enemy struct and return it.
« Last Edit: January 23, 2011, 10:33:34 AM by WarHampster »

Connors


  • ^ This guy is amazing.

  • ****


  • Posts: 2374

  • It's a secret to everyone...
Re: Objects
« Reply #2 on: January 23, 2011, 11:25:31 AM »
I like it! I say we try out his idea! XD
Warning: The above post may have been modified multiple times.

"In a great game, the character must never perfectly obey the user's command"
 - Tim Rogers

http://connorspuzzles.tumblr.com/

Charlo


  • GMG-er

  • **


  • Posts: 451
Re: Objects
« Reply #3 on: January 23, 2011, 11:38:36 AM »
In Bricker I used parallel arrays to achieve something like objects for the bricks.  Each brick had the following properties:

x
y
height
width
r value
g value
b value
solidness (1 or 0)

I started out by defining brick strings in an array like brick$(1) = "20, 20, 50, 50, 255, 0, 0, 0" etc.  I then used a method to parse the first value of each brick string into a brickx array.  So now the brickx array contains the x coordinate of every brick.  I did the same for every other attribute, so now I've got 8 arrays in parallel.  Referencing, say, the seventh item in each array gave me all the information for the seventh brick, and I could change them all individually.  

The problem would be that there would be way too many arrays if you wanted to make an RPG where EVERYTHING was an object.  Obviously you also wouldn't be able to do anything resembling "objectname.property" syntax either.

*Edit* Silver's idea is roughly the same as mine (I think), although I try to avoid string manipulation as much as possible so storing variables long term in strings doesn't sound good to me.
« Last Edit: January 23, 2011, 11:40:25 AM by Charlo »

EqwanoX


  • Administrator

  • GMG Extraordinaire

  • *****


  • Posts: 1180
Re: Objects
« Reply #4 on: January 23, 2011, 02:23:09 PM »
you can easily already do this with string arrays and you can have as many dimentions as you want by useing seperators

in one of my games i store all enemy locations in one string

let enemyxy$="3-2,3-4,6-7,8-6"

let xy=val(nthfield$(enemyxy$,",",1))   //this would return the "3-2"

//then just can split the x and y  with nthfield by changine the seperator to "-"

this is also how i store the enemy stats in gangwars

//1-2 xy on map, 3-4 xy on sprite sheet, 5 damage, 6 hp, 7evade, 8 accuracy,
let npc$(1)="3,6,2,8,6,90,20,10"

you can take it even further, this is basically exacly what gan described:

//stats are stored from the fifth digit allowing four spaces for a label
let npc$(1)="xmap3,ymap6,xspr2,yspr8,dam-6,hp--90,evd-20,acc-10"

let x$=nthfield$(npc$(1),",",6)    //returns the 6th field "hp--90"
let hp(1)=val(mid$(x$,5,5))       //returns 5 digits starting from the 5th digit

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: Objects
« Reply #5 on: January 23, 2011, 02:34:51 PM »
I've made a working artificial objects test:
http://cl.ly/0c1w1p2q3D1X3R3H0O0n

In Open Game:
Code: [Select]
//Set up bad guy object
LET BadGuy = 5
LET BadGuyName = 1
LET BadGuyHp = 2
LET BadGuyStr = 3
LET BadGuyDef = 4
LET BadGuyWeapon = 5


LET badGuy$ = CreateObject$(BadGuy)
LET badGuy$ = SetObject$(badGuy$,BadGuyName,"Dartanyon")
LET badGuy$ = SetObjectNum$(badGuy$,BadGuyHp,5)
LET badGuy$ = SetObjectNum$(badGuy$,BadGuyStr,2)
LET badGuy$ = SetObjectNum$(badGuy$,BadGuyDef,6)
LET badGuy$ = SetObject$(badGuy$,BadGuyWeapon,"Whip of Misfortune")

PRINT GetObject$(badGuy$,BadGuyName)
PRINT GetObject$(badGuy$,BadGuyHp)
PRINT GetObject$(badGuy$,BadGuyStr)
PRINT GetObject$(badGuy$,BadGuyDef)
PRINT GetObject$(badGuy$,BadGuyWeapon)

Then I have 5 methods to handle creation, getting and setting of objects.
Method Name: CreateObject$
Parameters: object
Code: [Select]
LET returnString$ = ""
FOR i = 1 to object-1
   LET returnString$ = returnString$ + ";"
NEXT
RETURN returnString$
Method Name: SetObject$
Parameters: object$,objectVar,setVar$
Code: [Select]
LET val = COUNTFIELDS(object$, ";")//number of fields separated by ";"
IF objectVar > 1 THEN
   FOR x = 1 to objectVar-1
      LET var2$ = var2$ + NTHFIELD$(object$, ";", x) + ";"//gets each field of var$ and adds it to end of var2$ and then adds separator
   NEXT
END IF
IF objectVar+1 < val THEN
   FOR x = objectVar+1 to val
      LET var3$ = var3$ + ";" + NTHFIELD$(object$, ";", x)
   NEXT
END IF
LET object$ = var2$ + setVar$ + var3$//put it back together again
LET var2$ = ""//set variables back to nothing so they are fresh for your next change
LET var3$ = ""
RETURN object$
Method Name: GetObject$
Parameters: object$,objectVar
Code: [Select]
return NTHFIELD$(object$, ";", objectVar)
Method Name: SetObjectNum$
Parameters: object$,objectVar,setVar
Code: [Select]
LET val = COUNTFIELDS(object$, ";")//number of fields separated by ";"
IF objectVar > 1 THEN
   FOR x = 1 to objectVar-1
      LET var2$ = var2$ + NTHFIELD$(object$, ";", x) + ";"//gets each field of var$ and adds it to end of var2$ and then adds separator
   NEXT
END IF
IF objectVar+1 < val THEN
   FOR x = objectVar+1 to val
      LET var3$ = var3$ + ";" + NTHFIELD$(object$, ";", x)
   NEXT
END IF
LET object$ = var2$ + str$(setVar)+ var3$//put it back together again
LET var2$ = ""//set variables back to nothing so they are fresh for your next change
LET var3$ = ""
RETURN object$
Method Name: GetObjectNum
Parameters: object$,objectVar
Code: [Select]
return val(NTHFIELD$(object$, ";", objectVar))


How it works is that you first define the object properties. The LET BadGuy = 5 tells it how many variables are in the object. Then each variable is given a name and the number of the variable inside the object. So LET BadGuyName = 1. The first variable in the object.
You can set and get objects using the appropriate methods.

x


  • GMG-er

  • **


  • Posts: 247
Re: Objects
« Reply #6 on: January 23, 2011, 06:26:32 PM »
I get the feeling you don't actually know what OOP is. What you have created is a struct, not an object. For your information an object isn't "like a normal variable. Like an int or a string. Except it can hold multiple numbers and strings. Here's an example.". That is whats called a a reference, or pointer to an object, the object isn't a "variable" in its own right. In addition the actual programming paradigm you have created is a struct, commonly used in C programming ( http://en.wikipedia.org/wiki/Struct ), an object would enable you to encapsulate methods, static variables, and data hiding. Lack of more complex features like polymorphism, inheritance and overloading is obviously out of the question due to complexity.
« Last Edit: January 23, 2011, 06:28:14 PM by x »

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: Objects
« Reply #7 on: January 23, 2011, 06:29:57 PM »
Yes, yes, getting all technical.

I use the word object because it is easier to understand than struct for someone who has never programmed in anything but simple game makers.

x


  • GMG-er

  • **


  • Posts: 247
Re: Objects
« Reply #8 on: January 23, 2011, 06:31:54 PM »
Quote
Yes, yes, getting all technical.

I use the word object because it is easier to understand than struct for someone who has never programmed in anything but simple game makers.

Wouldn't using incorrect terminology confuse beginners even more when they start using structs and objects? Calling this a struct doesn't diminish its usefulness  :P
« Last Edit: January 23, 2011, 06:32:38 PM by x »

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: Objects
« Reply #9 on: January 23, 2011, 06:38:11 PM »
I'm fairly sure it won't matter. This artificial object test I made is incredibly similar to Classes which in turn is also similar to Structs. They are all close so I doubt it matters.

The goal is to make game making easier to understand and use. One reason sprites are called sprites. When in OpenGl they use textures. Unless a custom sprite class is made.

Bah, doesn't matter. Call it what you wish. It's functionality resembles both.
« Last Edit: January 23, 2011, 06:38:51 PM by Gandolf »

x


  • GMG-er

  • **


  • Posts: 247
Re: Objects
« Reply #10 on: January 23, 2011, 06:40:51 PM »
Quote
I'm fairly sure it won't matter. This artificial object test I made is incredibly similar to Classes which in turn is also similar to Structs. They are all close so I doubt it matters.

The goal is to make game making easier to understand and use. One reason sprites are called sprites. When in OpenGl they use textures. Unless a custom sprite class is made.

Bah, doesn't matter. Call it what you wish. It's functionality resembles both.

Its functionality does NOT resemble both. Thats my point. Is there any data hiding? Can I use methods? Static variables?

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: Objects
« Reply #11 on: January 23, 2011, 06:47:39 PM »
Classes can hold variables. Variables can be accessed, set, and read. Classes can be stored in arrays.
These artificial objects can do the same. They resemble classes even if they don't fully duplicate functionality.
They also resemble structs.

But the word object is easier to understand than struct to a beginner. For example, a weapon object. Holds the name, damage, cost, ect. Makes more sense to an untrained mind than a weapon struct. Chances are when they move to a more advanced language they'll just use classes as objects instead of structs.

x


  • GMG-er

  • **


  • Posts: 247
Re: Objects
« Reply #12 on: January 23, 2011, 06:57:09 PM »
Quote
Classes can hold variables. Variables can be accessed, set, and read. Classes can be stored in arrays.
These artificial objects can do the same. They resemble classes even if they don't fully duplicate functionality.
They also resemble structs.

But the word object is easier to understand than struct to a beginner. For example, a weapon object. Holds the name, damage, cost, ect. Makes more sense to an untrained mind than a weapon struct. Chances are when they move to a more advanced language they'll just use classes as objects instead of structs.

There are so many things wrong with this post I am actually starting to feel a little bit angry. Classes CAN'T be stored in arrays, classes CAN'T hold variables. Classes are blue prints from which you create objects. Objects usually can't be stored in arrays either, its the POINTER or REFERENCE variable that is. Time for a quick objects lesson.
When we create an object:
className foo = new className (this is pseudocode)
What we are doing is creating a reference variable called foo, which usually stores an integer value pointing to the location in memory of the object we just created. This is why in C based languges we can actually screw with references big time by doing this: foo++. We have new moved our pointer over 1 bit in memory. The actual object is not stored in the variable foo. Got it?

Now, this does NOT resemble an object. To be an object you MUST and I repeat MUST enable basic features like methods and data hiding. Which you simply do not have. But if you would stop banging your head against the wall and spewing fallacious statements at me, I could give you some ideas of how to enable these features.

Also in my opinion the word "structure" is just as user friendly as the word "object" for describing whats being done.

EqwanoX


  • Administrator

  • GMG Extraordinaire

  • *****


  • Posts: 1180
Re: Objects
« Reply #13 on: January 23, 2011, 06:58:42 PM »
i dont see how thats easier than just putting all the enemy stats in one string array like this:

LET badguy$(1)="dartanyon,5,2,6,whip of misfortune"

then when needed, all the stats are loaded into arrays.

x


  • GMG-er

  • **


  • Posts: 247
Re: Objects
« Reply #14 on: January 23, 2011, 07:05:54 PM »
Quote
i dont see how thats easier than just putting all the enemy stats in one string array like this:

LET badguy$(1)="dartanyon,5,2,6,whip of misfortune"

then when needed, all the stats are loaded into arrays.

It isn't, the only reason a person would use a struct over an array would be because of three reasons. You can store any type of variable (int, long, string, etc you name it), you can even store objects in structs, its easier to access than arrays since you can just go "blah.variable"; its like syntactical eye candy.