Is here a way for Table to hold executable function?

Discuss any general programming issues here
Post Reply
Bugala
Posts: 825
Joined: Sun Feb 14, 2010 7:11 pm

Is here a way for Table to hold executable function?

Post by Bugala » Sun Aug 29, 2010 4:30 pm

I was thinking two different solutions to my problem, but after imlpementing and executing the code, i noticed i had error in my thinking.


Thing is, In my Point n click type adventure game, i have all the locations stored on separate tables called table.

an example of room and the problem could be

room[startroom] = { variable1=1, variable2 = variable3, variable4=function1()}



Variable1=1 work fine and no problem.

Hoever, Variable2=variable3, and variable4=function1() dont work the way i want them to.


Problem is, that because i save the tables as .bin files and then load them to the table as needed. Therefore it means, that at point I am saving the .bin file, it will also choose numbers for variable2 and variable4.


What I am trying to have is that at point in game that player enters the room, variable2 would get numerical value of variable3.

And similarly, when entering the room, i would like function1() to be executed, and then variable4 receive the value of the functions return value.


So is there some way i can have those tables run those functions and variable=variable thing in real time?


For so far only solution is to make some sort of table entry "special=1" and then in code,
if special=1 THEN the stuff i wanted.

jalih
Posts: 254
Joined: Fri Jun 18, 2010 8:08 pm
Location: Finland

Re: Is here a way for Table to hold executable function?

Post by jalih » Sun Aug 29, 2010 7:39 pm

Any reason, you can't just load all the room data at once when setting up a game? Are there that many rooms or are they really huge?

I would just write a simple parser that loads all the rooms from a text file and put them in a table.

Bugala
Posts: 825
Joined: Sun Feb 14, 2010 7:11 pm

Re: Is here a way for Table to hold executable function?

Post by Bugala » Sun Aug 29, 2010 7:48 pm

I could write all the Tables even in to main code.

just easier to follow this way.

However, even if i do put them all in main file, it still doesnt solve the problem that when table has like:

table = { variable1 = function1() }

It would still be executing that function1 right at beginnig of program, instead of checking it everytime you are entering the room.

The idea with that function is that for example at one place in game there is ferry that needs to be fixed.

If you try to go to room "other side of river" from room "map", then if Function1() returns 0, you will end up to room "ferry".

If Function1() returns 1 (meaning ferry is fixed) then it goes to room "otherside of river" directly.

jalih
Posts: 254
Joined: Fri Jun 18, 2010 8:08 pm
Location: Finland

Re: Is here a way for Table to hold executable function?

Post by jalih » Sun Aug 29, 2010 8:56 pm

Bugala wrote: table = { variable1 = function1() }

It would still be executing that function1 right at beginning of program, instead of checking it everytime you are entering the room.
How about just:

Code: Select all

table = { variable1 = function1}
and then call it directly when entering the room:

Code: Select all

table.variable1
Better yet, why not to make a spesific EnterRoom method?

Code: Select all

; Define one level. You could put these in the table

Room1 = {}
; define needed variables
   FerryFixed = 0 ; Begins with broken state

Function Room1:EnterRoom()
   if Self.FerryFixed = True
      Return(True)
   Else
      return(False)
    EndIf
EndFunction
So, you could just call Room1:EnterRoom() and check return value before entering room.

jalih
Posts: 254
Joined: Fri Jun 18, 2010 8:08 pm
Location: Finland

Re: Is here a way for Table to hold executable function?

Post by jalih » Sun Aug 29, 2010 11:05 pm

Sorry, my previous post was missing something...

Better yet, why not to make a spesific EnterRoom method?

Code: Select all

; Define one level. You could put these in the table
Room1 = {}
; define needed variables
Room1.FerryFixed = 0 ; Begins with broken state

Function Room1:EnterRoom()
   if Self.FerryFixed = True
      Return(True)
   Else
      return(False)
    EndIf
EndFunction
So, you could just call Room1:EnterRoom() and check return value before entering room.[/quote]

Bugala
Posts: 825
Joined: Sun Feb 14, 2010 7:11 pm

Re: Is here a way for Table to hold executable function?

Post by Bugala » Mon Aug 30, 2010 12:40 am

Darned i didnt know about that "self." thing. That had saved me lot of trouble propably.

Your solution looks like it might be answer to my problem. However, my head is too messed right now fom continuous programming/image manipulation that i could understand it well enough.

Hope im able to understand it completely at later point.

Post Reply