SetObjectMaterial help!
#1

Hi guys, I don't want to use SetObjectMaterial in this way:
PHP код:
public OnPlayerCommandText(playerid,cmdtext[])
{
    if(!
strcmp(cmdtext,"/mycommand",true))
    {
        new 
Float:XFloat:YFloat:Z;
        new 
myobject;
        
GetPlayerPos(playeridXYZ);
        
myobject CreateObject(19371XYZ+0.50.00.00.0300.0);
        
SetObjectMaterial(myobject019341"egg_texts""easter_egg01"0xFFFFFFFF);
        
//Replaces the texture of our object with the texture of object 19341
        
return 1;
    }
    return 
0;

but I want that all the object with the id 19461 (for example) have the texture that I choose. Is it possible to do something like that or I have to use the structure above?
Thanks all!
Reply
#2

I wrote a little stock function for this a while ago, I placed some comments in the code as well, so that people would be able to understand it.
I normally don't give out pre-made code because people might not learn anything from it, but since this code has a lot of explanation, and is quite advanced to explain step by step, here you go.
PHP код:
//Change object material function snippets -- By Jstylezzz (http://forum.sa-mp.com/member.php?u=1401...1.95388184
#define MAX_CREATED_OBJECTS 4 //Amount of objects in total, add one extra for the NULL, CHANGE TO AMOUNT OF OBJECTS OF PROJECT (put this after your includes, and BEFORE the variable declarations)
new Object[MAX_CREATED_OBJECTS]; //Will hold the actual object
new ObjectModel[MAX_CREATED_OBJECTS]; //Holds the object's model
new ObjectCount//Keeps track of the number objects created and will be used to assign new object ID's
stock j_CreateDynamicObject(mod,Float:x,Float:y,Float:z,Float:rx,Float:ry,Float:rz//Custom function for creating objects
{
    
Object[ObjectCount] = CreateDynamicObject(mod,x,y,z,rx,ry,rz); //Creates the object with the parameters passed to the stock function
    
ObjectModel[ObjectCount] = mod//Assigns the object's model to the object slot, so it can be accessed later on
    
printf("Created object: %d in object slot: %d"ObjectModel[ObjectCount], Object[ObjectCount]); //For debug, can be removed if you want. This prints the object ID and the object slot in the console
    
ObjectCount++; //Increases the object count so that the empty object slot comes up for the next time this function is used
}
stock ChangeObjectMaterial(modmidxtmodtxdname[], txtrname[], matcolor 0//Syntax: ChangeObjectMaterial(object model id, material index, target object model, txd name, texture name, optional: material color)
{
    for(new 
i=0MAX_CREATED_OBJECTSi++) //Loop through the created objects
    
{
          if(
ObjectModel[i] == mod)//Checks if the object ID in the current slot is the same as the model we want to replace
          
{
              
SetDynamicObjectMaterial(Object[i],midxtmod,txdnametxtrnamematcolor); //Changes the object material with the parameters passed to the function
          
}
    }
}
//Now, to create objects just do
j_CreateDynamicObject(objectIDxPosyPoszPos,   rotXrotYrotZ); // The standard CreateDynamicObject function, exactly the same. Only difference is the 'j_' in front of it, so that our custom function is used instead of the normal streamer function, which does not use our custom object slots and all that stuff.
//Now, to change the object material of all objects with a specific object ID
ChangeObjectMaterial(object model idmaterial indextarget object modeltxd nametexture nameoptionalmaterial color); 
I hope it's explained enough. If you need any help please let me know. It should work, I haven't tested it recently though.
Oh yeah, also be sure to have streamer included, otherwise this will not work. If you don't want to use streamer there's an easy workaround which I can also help you with

Good luck, and let me know!

-- I also uploaded this on Pastebin, since viewing it here might be a little small and unclear..

EDIT: I realize this might be quite advanced, maybe a little too advanced for you (guessing you're a beginner scripter). Don't worry about not understanding the code, if you try and don't blindly copy/paste the code, you'll understand soon enough. Only thing I can't say enough, is don't blindly copy/paste
Reply
#3

Quote:
Originally Posted by Jstylezzz
Посмотреть сообщение
I wrote a little stock function for this a while ago, I placed some comments in the code as well, so that people would be able to understand it.
I normally don't give out pre-made code because people might not learn anything from it, but since this code has a lot of explanation, and is quite advanced to explain step by step, here you go.
PHP код:
//Change object material function snippets -- By Jstylezzz (http://forum.sa-mp.com/member.php?u=1401...1.49346267
#define MAX_CREATED_OBJECTS 4 //Amount of objects in total, add one extra for the NULL, CHANGE TO AMOUNT OF OBJECTS OF PROJECT (put this after your includes, and BEFORE the variable declarations)
new Object[MAX_CREATED_OBJECTS]; //Will hold the actual object
new ObjectModel[MAX_CREATED_OBJECTS]; //Holds the object's model
new ObjectCount//Keeps track of the number objects created and will be used to assign new object ID's
stock j_CreateDynamicObject(mod,Float:x,Float:y,Float:z,Float:rx,Float:ry,Float:rz//Custom function for creating objects
{
    
Object[ObjectCount] = CreateDynamicObject(mod,x,y,z,rx,ry,rz); //Creates the object with the parameters passed to the stock function
    
ObjectModel[ObjectCount] = mod//Assigns the object's model to the object slot, so it can be accessed later on
    
printf("Created object: %d in object slot: %d"ObjectModel[ObjectCount], Object[ObjectCount]); //For debug, can be removed if you want. This prints the object ID and the object slot in the console
    
ObjectCount++; //Increases the object count so that the empty object slot comes up for the next time this function is used
}
stock ChangeObjectMaterial(modmidxtmodtxdname[], txtrname[], matcolor 0//Syntax: ChangeObjectMaterial(object model id, material index, target object model, txd name, texture name, optional: material color)
{
    for(new 
i=0MAX_CREATED_OBJECTSi++) //Loop through the created objects
    
{
          if(
ObjectModel[i] == mod)//Checks if the object ID in the current slot is the same as the model we want to replace
          
{
              
SetDynamicObjectMaterial(Object[i],midxtmod,txdnametxtrnamematcolor); //Changes the object material with the parameters passed to the function
          
}
    }
}
//Now, to create objects just do
j_CreateDynamicObject(objectIDxPosyPoszPos,   rotXrotYrotZ); // The standard CreateDynamicObject function, exactly the same. Only difference is the 'j_' in front of it, so that our custom function is used instead of the normal streamer function, which does not use our custom object slots and all that stuff.
//Now, to change the object material of all objects with a specific object ID
ChangeObjectMaterial(object model idmaterial indextarget object modeltxd nametexture nameoptionalmaterial color); 
I hope it's explained enough. If you need any help please let me know. It should work, I haven't tested it recently though.
Oh yeah, also be sure to have streamer included, otherwise this will not work. If you don't want to use streamer there's an easy workaround which I can also help you with

Good luck, and let me know!

-- I also uploaded this on Pastebin, since viewing it here might be a little small and unclear..

EDIT: I realize this might be quite advanced, maybe a little too advanced for you (guessing you're a beginner scripter). Don't worry about not understanding the code, if you try and don't blindly copy/paste the code, you'll understand soon enough. Only thing I can't say enough, is don't blindly copy/paste
First of all thank you, but when I compile I get these errors
Код:
error 017: undefined symbol "ObjectModel"
error 017: undefined symbol "Object"
P.S. I have changed your "SetDynamicObjectMaterial" in "SetObjectMaterial".
Reply
#4

SetDynamicObjectMaterial is for objects created by the streamer plugin. SetObjectMaterial is for regular objects created with CreateObject, be sure to keep that in mind.
Check my code again, you forgot to copy new ObjectModel[MAX_CREATED_OBJECTS] and new Object[MAX_CREATED_OBJECTS].

Hope it works for you
Reply
#5

Quote:
Originally Posted by Jstylezzz
Посмотреть сообщение
SetDynamicObjectMaterial is for objects created by the streamer plugin. SetObjectMaterial is for regular objects created with CreateObject, be sure to keep that in mind.
Check my code again, you forgot to copy new ObjectModel[MAX_CREATED_OBJECTS] and new Object[MAX_CREATED_OBJECTS].

Hope it works for you
I don't know why, but when I compile without "j_CreateDynamicObject" and "ChangeObjectMaterial" there are no errors but when I add these 2 codes it gives me errors. I put j_CreateDynamicObject under OnGameModeInit and ChangeObjectMaterial under OnPlayerConnect. Where is wrong? Here the errors:
Код:
error 017: undefined symbol "ObjectModel"
error 017: undefined symbol "SetDynamicObjectMaterial"
warning 203: symbol is never used: "matcolor"
warning 203: symbol is never used: "tmod"
warning 203: symbol is never used: "midx"
Reply
#6

Add these outside a function preferably near the top of your script:

PHP код:
new Object[MAX_CREATED_OBJECTS];
new 
ObjectModel[MAX_CREATED_OBJECTS];
new 
ObjectCount
Reply
#7

Quote:
Originally Posted by Glenn332
Посмотреть сообщение
Add these outside a function preferably near the top of your script:

PHP код:
new Object[MAX_CREATED_OBJECTS];
new 
ObjectModel[MAX_CREATED_OBJECTS];
new 
ObjectCount
Already done!
Reply
#8

Quote:
Originally Posted by FreddiewJO
Посмотреть сообщение
Already done!
Then ObjectModel error should be fixed

error 017: undefined symbol "SetDynamicObjectMaterial"

Means that either your streamer doesnt allow it or you dont have a streamer include?
Reply
#9

Quote:
Originally Posted by Glenn332
Посмотреть сообщение
Then ObjectModel error should be fixed

error 017: undefined symbol "SetDynamicObjectMaterial"

Means that either your streamer doesnt allow it or you dont have a streamer include?
ObjectModel is not fixed and yes, I have streamer.inc
Can you post here a simple example of this script? Thanks
Reply
#10

Can you post here what you have so far? Be sure to keep the following points in mind:
  • After all the #include code, put that #define from my code
  • After the define, paste the variable declarations (new ObjectModel etc)
The stocks should be alright there where you put them. If you still can't manage to get it working, I'll post an example.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)