Author Topic: Need help finding objects ID through script [SOLVED]  (Read 843 times)

I spawned a bot using console command
Code: [Select]
%bot = new aiPlayer(%name){datablock=playerStandardArmor; position=findClientByName("enterNameHere").player.getPosition();}; but I'm trying to access the ID of the bot, i tried %bot.getID(); but it doesnt work. If anyone knows how I can accomplish this any help would be great!

function Armor::onTrigger(%this,%obj,%slot,%on)
   {
      %objectID = %this.getID();
      if(isObject(%obj.client))
      {
         if(%on)
         {
            if(%slot == 4)
            {
               if (%obj.client.isCarryingBody == 0)
               {
                  Findclientbyname(%obj.client.name).player.mountobject(%objectID,1);
                  %obj.client.isCarryingBody = 1;
               }
               else if (%obj.client.isCarryingBody == 1)
               {
                  %objectID.dismount();
                  %obj.client.isCarryingBody = 0;
               }
            }
         }
      }
      parent::onTrigger(%this,%obj,%slot,%on);
   }

I need help getting that object's ID ingame is there any possible way?
« Last Edit: January 21, 2013, 11:07:34 PM by Gordo12699 »

It's because you don't understand how variables work. You should read up on some tutorials.

Is there anywhere I can find a tutorial for variables?


Thank you sir, but it didn't help at all.  I think %client doesn't fit either of those criteria and wouldn't %bot = something similar to %client?
« Last Edit: January 21, 2013, 02:02:24 PM by Gordo12699 »

Thank you sir, but it didn't help at all.  I think %client doesn't fit either of those criteria and wouldn't %bot = something similar to %client?
I don't even know how to begin to explain this in a way that you will understand it (because you have failed to understand others).

Ok. %bot is the bot's object. If you do /getID on the bot it will also tell you the bot's object, but it's just not stored in a variable. The bot is literally the fake player you see, it has no client. So a bot is similar to %player as in the function you can use with it.
« Last Edit: January 21, 2013, 02:35:27 PM by jes00 »

tl;dr if you just do echo(%bot); it will show you the ID.


Sorry, should've clarified that I meant if he put that in the console along with his spawning command, that it would show the ID.

Replace %bot with $bot

As Port mentioned, there are things called variable scopes. %variables can not be referenced outside the function they're created in. These are called local variables. $variables persist through all scopes, meaning you can define it in one function and reference it in another. For example:

Code: [Select]
function thisWill()
{
    %number = 5;
    notWork();
}
function notWork()
{
    echo(%number + 5);
}

As the names suggest, this will not work because %number is defined inside the scope of thisWill, and notWork is in a different scope. However, if you replaced that variable with its global equivalent ($number) it will work.

It may seem like you should just use global variables all the time because it's easier, but this is generally bad practice. I don't want to overwhelm you, but now that you know what the problem is you can go back to Yola's tutorial and learn more about local and global variables.

Trinick, your explanation helped. I will try all your suggestions guys, thanks.

« Last Edit: January 21, 2013, 10:50:26 PM by Gordo12699 »

Bump, God I feel so horrible doing a triple post.