Assigning python scripts to game objects via code?

Is there any way to assign a script to a game object with code? I’m asking because currently with how my “Spawn an object into the game” system is coded, I have to have two seperate objects, each having a script for AI and the other having a script for if that ship was used by a player.

Here’s an example of what I mean. (EvE models, heh :slight_smile: )
d=1359669671&thumb=1
One of the two ships has a script for player movement, etc, while the other has a script specifically for AI functions.
What I want to do is use one object, spawn it into the game, then assign the appropriate script. Any nudge in the right direction would be appreciated… however, I’d prefer if you don’t post something along the lines of “this is how you do it” because I like to learn how to tie the API together in new ways and don’t feel like I learn anything if someone tells me exactly how its done.

so

Condition-------python

in logic,

you will need either Rays, or cones or near etc, to control your ai,

or some type of A* or nav-mesh system,

I use vision cones to spot waypoints, and smells, and players, the waypoints are generated with python rules, but it is still experimental, so the map steers the AI, to a certain extent, and the “waypoints” are clues left by the player,
like smells, footprints etc, and are randomly applied based on rules

Bellow is a triggered script,

Attachments

Scene.blend (458 KB)

It might be convenient to assign scripts by hand by accessing the Python controller, but I don’t notice a method to do so in the API. So, this could rather be a matter of script / game design.

You could use a simple variable (‘incontrol’, for example) to tell what function to run. Then, all ships can have the same script, and you can just set the variable on the ones you’re taking control of.

You could also go with a Component-based design. You would make an empty list of components on a common game object script. Then, you would loop through those and initialize them if necessary and update them. You then could abstract the player control out into a component, and add it in to the ship that you want (same thing with AI control or other physical capabilities, like the ability to carry objects, firing on enemies, or exploding when health is too low). All ships would run the same base script, and perhaps have a simple script on initialization to add in the components that you need.

so

Condition-------python

in logic,

you will need either Rays, or cones or near etc, to control your ai,

or some type of A* or nav-mesh system,

I use vision cones to spot waypoints, and smells, and players, the waypoints are generated with python rules, but it is still experimental, so the map steers the AI, to a certain extent, and the “waypoints” are clues left by the player,
like smells, footprints etc, and are randomly applied based on rules

No that’s not what I meant, but thanks for the reply! I mean I want to spawn a ship into the game that has no scripts attached to it and then do something along the lines of GameLogic.player.addScript(“Player_Script.py”) and then add an AI, and assign the AI script the same way, but to the AI instead of the player.

@Goalatio: It’s actually much easier and faster to create two separate objects with the different sets of code and then handle which one you add with the spawner.

Edit:
Got here a little bit late haha

yeah I was afraid of that :confused: it’s just back when I used .asm and ruby to do this stuff it was a personal blaspheme to have “wasted space” like that (using two of the same thing to only have one different attribute) but if it’s unavoidable I guess I’ll have to deal with it, heh

It may be easier to have the object have an empty that is the “player” or the “script container”

a script can adjust a property just like a player, so the ship has the properties, throttle, firing, strafing etc, and the AI just changes the properties to trigger behaviors

another neat idea, is that the AI could leave the ship and walk/fly etc and use another ship…
by changing it’s message targets,

I use logic based for loops triggered by dynamic sensors all the time,

Like
message--------------------and----------------property Count=1;
if Count=0(state check)-/

and then have another conditional like this

if Count is less then Max and equal to or greater then 1, --------------and--------------Count = Count+1

if Count = Max-------------------------------and----------------------------------------------property = 0

this can trigger actions, based on time, ping pong or all kinds of things,

so your AI just sends messages just like the player to the “Ship”

So your “Player” is just a set of logic bricks

Keypress X---------------and-----------------------message “Ship” whatever

and Ai is just the same, accept the messages are triggered with a property sensor, and your “Ai” adjust the property to try and steer

If I have a vision cone Center, Right, Far Right, etc, I can use these to apply torque to aim, have this combined with applying forces to move, and avoid collisions…
and you have an AI :slight_smile:

Attachments

CodeItAll.blend (522 KB)


yep I already have it set up to use properties/triggers for throttle and such. ai/player share the same script in that sense. I think for now until (and if) it starts creating an issue with file size from having two of every ship on the “data layer” of the scene I’ll go and see if there’s a solution to this. Anyway, thanks for the input everyone :slight_smile:

here’s the ai movement code (very basic… just testing/experimenting with different stuff for now). ai_data is a hash created in another script, and the ai’s object data is added to it from an ai_init script. thread can be closed now :slight_smile:

Edit: Also, this thread did help me out quite a lot, thank you all for the suggestions! This is a great community :slight_smile: I ended up rearranging a bit of the structure of coding to fit some of the suggested ideas.

import GameLogic
import game_core

ai          = GameLogic.getCurrentController()
coll        = ai.sensors['collision']
obj         = ai.owner
moving      = ai.actuators['move']

ai_obj  = GameLogic.ai_data[("{0}", obj)]

speed       = [0.0, 0.0, 0.0]
rotation    = [0.0, 0.0, 0.0]

moving.useLocalDLoc = True
moving.useLocalDRot = True

if coll.positive:
    ai_obj.health -= 1
    ai_obj.engines_act = True
else:
    ai_obj.engines_act = False
    #speed[2] = -ai_obj.speed
    
if ai_obj.engines_act == True:
    #speed[2] = -ai_obj.speed
    for x in range(0,len(obj.children[0].children)-1):
        obj.children[0].children[x].setVisible(True)    #Enable "Engine fire"
else:
    for x in range(0,len(obj.children[0].children)-1):
        obj.children[0].children[x].setVisible(False)   #Disable "Engine fire"

if ai_obj.health <= 99: #testing
    del GameLogic.ai_data[("{0}", obj)] #Remove current ship from AI hash when dead
    obj.endObject()
    
moving.dLoc = speed
moving.dRot = rotation

ai.activate(moving)
  1. I mentioned in my post a way to get past using two different objects for each individual ‘mode’ of the object (use components, use a property with two separate functions (or just an if/else statement), etc).

  2. You can mark threads as solved yourself in Advanced Edit mode of your first post in the thread (i.e. threads don’t generally get closed here unless they’re very old, or something goes horribly awry).

The object is just an empty, or a model of your pilot , you keep the ship for both, and an object “empty” edits the properties using python, I don’t think extra Empties add much CPU unless they are dynamic or a rigid body, using “No Collision”

so the “object” that you add to the ship is more or less a control engager, that can be replaced in game,
So shooting a pilot, could stop a ship completely… without blowing it up,

this way you get a player model in the object, as well as his movements to steer, or you can “kill” a enemy AI, and then take his ship / pillage it for resources (which are scarce in space)

Got it, thanks. I’m new here dispite my account age.

Also, any object can edit the properties / location etc of any other, if it has the “Key” for that item,

so your “Ai” could be one script that each frame moves from ship to ship, setting up there next 30 frames of movement, and then moving on, without ever having a single line of script in the “ship”,

You need models for your own game?
I am pretty sure people would help :slight_smile:

I figured out how to use one object for multiple “handlers” (player, ai, etc)
Have 3 states, the first state is an initialization state, which checks a bool game property on the object.
If true, it sets the state to 2, which is the player handler for movement etc
If false, it sets the state to 3, which is the ai handler :slight_smile:

Have you had a look at my ret?

Golatatio?

yep :slight_smile: thanks. however I don’t like using logic bricks where I can avoid it. I prefer python

Try and thread that python… :slight_smile:

One radar and some simple math could run the python Version or you could use rays etc,