Virtual object management: Giving objects their own code

Started by LemonWizard, January 15, 2012, 09:42:02 PM

Previous topic - Next topic

LemonWizard

This is incomplete.
But here's the sample.
I've been working on this concept for a long time.

PlayBASIC Code: [Select]
; PROJECT : Project1
; AUTHOR : NA
; CREATED : 1/15/2012
; ---------------------------------------------------------------------

writefile "C:/object1.txt", 1
writeint 1, 100
for temp=1 to 100
writestring 1, "test"
next temp
closefile 1


type object1
x1
y1
x2
y2
code$(100)
name$
endtype
;simple interpreter

dim objects(10) as object1

for temp=1 to 10
readfile "C:/object1.txt", 1
objects(temp).name$="enemysprite"
amount=readint(1)

;dim objects(temp).code$(amount)
for temp2=1 to amount
objects(temp).code$(temp2)=readstring$(1)
next temp2
closefile 1

next temp

for temp=1 to 100
print "object number 1 command number "+ str$(temp)+" is equal to : "
print objects(1).code$(temp)
sync
cls rgb(0,0,0)

waitkey
waitnokey
next temp




function object_hits_object(number1, number2)


x1=objects(number1).x1
y1=objects(number1).y1
x2=objects(number1).x2
y2=objects(number1).y2

x3=objects(number2).x1
y3=objects(number2).y1
x4=objects(number2).x2
y4=objects(number2).y2

;check collision on right side
if x2=>x3
if x1<=x4
if y1<=y4
if y2=>y3
result=true
endif
endif
endif
endif



endfunction, result



The idea is that it loads blocks of string code into the objects themselves.
Hopefully I can make a simple scripting system which will allow the code for a single object in game to be hundreds of lines of code long.
Making abstracting objects easier since most of the code for an object will be contained externally.
we shall see.

kevin

  The script doesn't need to be a part of the object at all..  Rather, when script is executed, it's passed a pointer to the owner, but that's getting a bit of ahead of ourselves.    For starters you need to use a more dynamic approach like so,

PlayBASIC Code: [Select]
   Type tScript
CodeArrayHandle
EndType

Dim Scripts(0) as tScript

cr$=chr$(13)+chr$(10)

For lp =1 to 10
; make some text to represent the script in the demo
Code$="; Script #"+str$(lp)+Cr$
for lp2=0 to rnd(10)
code$+=" Line #"+str$(lp2)+cr$
next

; create the script
NewScript(Code$)
next


; create an array stub
MakeArray ScriptCode$()


; show whats in the scripts are in the scripts() array
For lp =0 to GetArrayElements(Scripts())
; check if there's anything at this position in the array
if Scripts(lp)

; Assign our script code array stub the array handle of our script
ScriptCode$() =Scripts(lp).CodeArrayHandle

; call the show routine
ShowArray(ScriptCode$())
endif
next


Sync
waitkey




Function NewScript(Code$)
index=getfreecell(Scripts())

if Index>0
MakeCodeArray(Code$)
Scripts(Index).CodeArrayHandle=MakeCodeArray(Code$)
endif

EndFunction Index


Function ShowArray(Code$())

Cls 0
for lp =0 to GetArrayElements(Code$())
print Code$(lp)
next
Sync
waitkey
waitnokey

EndFunction


Function MakeCodeArray(Code$)
Dim code$(0)
Code$=replace$(code$,chr$(10),"")
Count=SplitToArray(code$,chr$(13),Code$())
EndFunction Code$()





  This creates a string array for each script fragment,  the script structure just holds the handle of the created array,  which can access via an array stub.  


Related examples:

   The following related examples use the CallFunction command to conceptually attach methods to types.

  * Action GUI
  * Abstraction Layer
  * Hell Eagle Mock Up