Interacting Objects

BlitzMax Forums/BlitzMax Beginners Area/Interacting Objects

Grovesy(Posted 2005) [#1]
Me again. I want to ask a question about getting object to interact with each other. I know that one object can call a function on another object. My probablem isn't that straight forward. Heres what im trying to do:

I am making a level edit (that bits easy). but nearly every object can interact with each other. For example a switch object can turn a light object on/off/ or set the brightness level/colour.

Now, i could 'hard code' the switch object so that it know about all the different function calls it can perform on the light object. But if i have, for example, a 100 objects i don't want to hard code each one so it knows about the other 99!

So... and here comes the question after all that (assuming i haven't lost u!)... how can u get objects to know about other objects callable functions during run time? Ie maybe it could do some sort of query to say "What functions do u have i can call, and what parameters can i set?"

Any ideas, Thanks all


Dreamora(Posted 2005) [#2]
Have you tried to use function pointers? So you simply add a field "field function (target:object,value:int);field targ:object;field value:int" to your object and when the action is started the function calls

"function (targ,value)"

You just have to set the function and the params.


ImaginaryHuman(Posted 2005) [#3]
Sounds like you need some kind of `map` of the play area with links between parts? Linked list?


Grovesy(Posted 2005) [#4]
Dreamora, That sort of sounds like what im after, but still a little confused. Would this work is there where many functions that can be called?

Don't suppose you could show a little example could u?

Cheers


Dreamora(Posted 2005) [#5]
Sure sure



I hope this helps to see the idea. The function can replaced with any function you want (as well as with functions within types)

The set_use_target is needed because I haven't found a possibility to set function pointers to functions with parameters.


Jay Kyburz(Posted 2005) [#6]
Is their defiantly no way to ideate over an object at runtime and determine what fields, methods or functions is has?


Dreamora(Posted 2005) [#7]
As this is defined by its type, this is no problem. A simple if type(instance) call will tell you if it is of type "type" and you can act according to that.

But for dynamic functionality especially for RPG with scripts etc function pointers are by far the best solution as you can put ANY functionality in it.


FlameDuck(Posted 2005) [#8]
how can u get objects to know about other objects callable functions during run time?
You make them adhere to a specific interface, or extend an Abstract class.

Is their defiantly no way to ideate over an object at runtime and determine what fields, methods or functions is has?
No. BlitzMAX does not support runtime reflection.


Grovesy(Posted 2005) [#9]
hummmm, I'm still trying to get my head around this.

Do you know if it is possible to call a function from a string?? Because i was thinking i could have a method in each class called something like callFunction(call:String) which accepts a string and the string can be the method call "LightValue, 1" this could then call the method LightValue with the parameter 1.

Guess what i want to do is convert string to function call!

Is this possible?


Dreamora(Posted 2005) [#10]
No its not possible. The only thing you could do would be a select "call" if you seperated it and then call the function. But as this isn't dynamic it its of use for scripting systems ...


Thats where function pointers are for (see example above that I haven't written just for fun)


Arowx(Posted 2005) [#11]
OK How about every object having a simple message_event method this could receive id's and command strings.
Type TBaseEvent
  Method message_event(sender_id:Int, commands:String)
End Type

Type TMessageHandler
  'Stores all objects based on Id
  'and all obejct types as groups
  'Ideally the TBaseEvents constructor 
  'should add the object to the message handler
End Type


However each object would only have certain commands it can run e.g. Light On/Off "R+", "R-" using a simple select case or list based (althoug a hashMap would be better for speed) depending upon the number of events you need.

Then all you need do is set the objects that can trigger interactions up with their message strings and a link to the message handler.

Additional concepts like spatial distance triggers could be added by extending the message handler for differnt trigger types.

It's only a concept but I'd be interested to hear your views.

Regards

Merx


Grovesy(Posted 2005) [#12]
Hi again Dreamora. Appologies for looking for an alternative, i tend to do that when i don't understand something lol.

Im still confused as to how this would work with multiple methods. Below is a quick function I have put together to show what im trying to acheve. maybe you could explain how I can use your idea in my type.

Type TLight
  Field value 'Whether the light is on or off 0/1
  Field brightness 'Brightness of light 0 - 100
  Field red, green, blue 'Colour of the light

  Method SetLightValue(iValue:Int)
    value = iValue
  End Method

  Method GetLightValue()
    return value
  End Method

  Method SetLightBrightness(iValue:Int)
    brightness = iValue
  End Method

  Method GetLightBrightness()
    return brightness
  end Method

  Method SetLightColour(r:Int, g:Int, b:Int)
    red = r
    green = g
    blue = b
  end method

  Method GetLightColour()
    return r + ", " + g + ", " + b
  end method
End Type


Hopefully the above shows a very simple object type that might be used. So what needs to be added to allow another object to find out what methods it has and what parameters are needed?

Thanks again


Dreamora(Posted 2005) [#13]