Jump to content
Sign in to follow this  
daimyo21

Createvehicle through IsServer, Addaction to object so player can use JIP HELP!

Recommended Posts

So Im creating a dynamic misson where i spawn a bunch of objects in buildings via server side. I want to addaction to all these objects so players cna see them.

So I create vehicle in my init.sqf with this: (please dont pay attention to stupid inefficiency scripting.)

if(isServer) then {


_nObject = position player nearestObject "Land_House_C_10_dam_EP1";
_nObject = bt25;


food1="land_basket_ep1" createVehicle getpos _nobject;
_genfood = position _nobject nearestobject "land_basket_ep1";
_genfood="land_basket_ep1" createvehicle getpos _nobject;
_genfood setPos (_nobject buildingpos (random 20));
_genfood addAction[("<t color=""#FF0000"">" + ("Take Food!") +"</t>"),"example_scripts\take_food.sqf", [], 1, false, false, "", ""];
_genfood="land_basket_ep1" createvehicle getpos _nobject;
_genfood setPos (_nobject buildingpos (random 20));
_genfood addAction[("<t color=""#FF0000"">" + ("Take Food!") +"</t>"),"example_scripts\take_food.sqf", [], 1, false, false, "", ""];

_genfood="land_basket_ep1" createvehicle getpos _nobject;
_genfood setPos (_nobject buildingpos (random 20));
_genfood addAction[("<t color=""#FF0000"">" + ("Take Food!") +"</t>"),"example_scripts\take_food.sqf", [], 1, false, false, "", ""];

_genfood="land_basket_ep1" createvehicle getpos _nobject;
_genfood setPos (_nobject buildingpos (random 20));
_genfood addAction[("<t color=""#FF0000"">" + ("Take Food!") +"</t>"),"example_scripts\take_food.sqf", [], 1, false, false, "", ""];

deletevehicle food1;
publicvariable "_genfood";

};

It woks great! BUT obviously addaction is local so it doesnt show up.

I try to just do this at the end after if (isServer):

_genfood addAction[("<t color=""#FF0000"">" + ("Take Food!") +"</t>"),"example_scripts\take_food.sqf", [], 1, false, false, "", ""];

But this doesnt work either... I can do it clientside through a waituntil !iserver whatever... but then objects spawn everytime a player JIP.... been spending countless hours on this dilemma. Please help. Thanks!

Share this post


Link to post
Share on other sites

Dont add the addaction like that in the script use this command to set the init field of the object it will be global then.

http://community.bistudio.com/wiki/setVehicleInit

food1="land_basket_ep1" createVehicle getpos _nobject; 
_genfood = position _nobject nearestobject "land_basket_ep1"; 
_genfood="land_basket_ep1" createvehicle getpos _nobject; 
_genfood setPos (_nobject buildingpos (random 20)); 
[color="Red"]_genfood setVehicleInit  "this addAction[("<t color=""#FF0000"">" + ("Take Food!") +"</t>"),"example_scripts\take_food.sqf";"
processInitCommands [/color]

Share this post


Link to post
Share on other sites

try this

_genfood setVehicleInit "this addAction [""Take Food"",""example_scripts\take_food.sqf""];";
processInitCommands;

Also don't call everything the same name. Name each object individually so you can set them with no risk of conflict.

publicvariable "_genfood"; 

not sure about making a private variable public. try something else without the "_" such as "GENFOOD".

hope that helps

Share this post


Link to post
Share on other sites
Dont add the addaction like that in the script use this command to set the init field of the object it will be global then.

http://community.bistudio.com/wiki/setVehicleInit

food1="land_basket_ep1" createVehicle getpos _nobject; 
_genfood = position _nobject nearestobject "land_basket_ep1"; 
_genfood="land_basket_ep1" createvehicle getpos _nobject; 
_genfood setPos (_nobject buildingpos (random 20)); 
[color="Red"]_genfood setVehicleInit  "this addAction[("<t color=""#FF0000"">" + ("Take Food!") +"</t>"),"example_scripts\take_food.sqf";"
processInitCommands [/color]

Truly appreciate both fast replies, its been stressing me out all day. I was trying the setVehicleInit command before and I just didnt know how to place it correctly and it would mess up the script.

So I think using the same item name over and over might of caused conflict. But it seems to work and work for JIP players.

I didnt want to have to name 100-300 objects individually. I have an array of classname objects that itll randomize through, but I dont want to have to do gen1 gen2 gen3 gen4 gen5 choose object from array.. etc.. How do I make objects dynamic and generically be set apart from each other??

---------- Post added at 03:42 ---------- Previous post was at 02:57 ----------

try this

_genfood setVehicleInit "this addAction [""Take Food"",""example_scripts\take_food.sqf""];";
processInitCommands;

Also don't call everything the same name. Name each object individually so you can set them with no risk of conflict.

publicvariable "_genfood"; 

not sure about making a private variable public. try something else without the "_" such as "GENFOOD".

hope that helps

_genfood setVehicleInit "this addAction [""Take Food"",""example_scripts\take_food.sqf""];";
processInitCommands;

That works... but I cant seem to colorize it anymore with:

genfood setVehicleInit "this addAction[("<t color=""#FF0000"">" + ("Take Food!") +"</t>"),"example_scripts\take_food.sqf"];";
processInitCommands;

Share this post


Link to post
Share on other sites

Didn't test.

SHK_pveh_foodAction = {
 {
   _x addAction[("<t color=""#FF0000"">" + ("Take Food!") +"</t>"),"example_scripts\take_food.sqf", [], 1, false, false, "", ""];
 } foreach _this;
};
"SHK_pv_foodAction" addpublicvariableeventhandler {(_this select 1) call SHK_pveh_foodAction};

if(isServer) then { 
 _nObject = position player nearestObject "Land_House_C_10_dam_EP1"; 
 _nObject = bt25; 

 SHK_pv_foodAction = [];

 for "_i" from 1 to 4 do {
   _genfood="land_basket_ep1" createvehicle getpos _nobject; 
   _genfood setPos (_nobject buildingpos (random 20)); 
   SHK_pv_foodAction set [count SHK_pv_foodAction, _genfood];
 };
 publicvariable "SHK_pv_foodAction";
 if !isdedicated then { SHK_pv_foodAction call SHK_pveh_foodAction };
} else {
 if (isnull player) then {
   [] spawn {
     waituntil {!isnil "SHK_pv_foodAction"};
     SHK_pv_foodAction call SHK_pveh_foodAction;
   };
 };
};

However, this line...

_nObject = position player nearestObject "Land_House_C_10_dam_EP1";

On dedicated server there won't be a player so it won't work.

Share this post


Link to post
Share on other sites

Shuko strikes again!! Lol

I was reading PVPs link but wasn't sure how to apply it.

Thanks shuko for your example. Will revisit my mission using that method. Very logical.

daimyo, make sure you use double " around you texts. Or use '

Edited by roy86

Share this post


Link to post
Share on other sites

sender:

Public_VehicleEntity = createVehicle ..
publicVariable "Public_VehicleEntity";

receiver:

(init.sqf - define the code just once)

"Public_VehicleEntity" addPublicVariableEventHandler
{
_parameters = (_this select 1);
//do stuff
};

Share this post


Link to post
Share on other sites
Didn't test.

SHK_pveh_foodAction = {
 {
   _x addAction[("<t color=""#FF0000"">" + ("Take Food!") +"</t>"),"example_scripts\take_food.sqf", [], 1, false, false, "", ""];
 } foreach _this;
};
"SHK_pv_foodAction" addpublicvariableeventhandler {(_this select 1) call SHK_pveh_foodAction};

if(isServer) then { 
 _nObject = position player nearestObject "Land_House_C_10_dam_EP1"; 
 _nObject = bt25; 

 SHK_pv_foodAction = [];

 for "_i" from 1 to 4 do {
   _genfood="land_basket_ep1" createvehicle getpos _nobject; 
   _genfood setPos (_nobject buildingpos (random 20)); 
   SHK_pv_foodAction set [count SHK_pv_foodAction, _genfood];
 };
 publicvariable "SHK_pv_foodAction";
 if !isdedicated then { SHK_pv_foodAction call SHK_pveh_foodAction };
} else {
 if (isnull player) then {
   [] spawn {
     waituntil {!isnil "SHK_pv_foodAction"};
     SHK_pv_foodAction call SHK_pveh_foodAction;
   };
 };
};

However, this line...

_nObject = position player nearestObject "Land_House_C_10_dam_EP1";

On dedicated server there won't be a player so it won't work.

Shuko strikes again!! Lol

I was reading PVPs link but wasn't sure how to apply it.

Thanks shuko for your example. Will revisit my mission using that method. Very logical.

daimyo, make sure you use double " around you texts. Or use '

Shuko, first off, mad respect for your scripting skills. I learn so much from you. Secondly, code you gave me works just like I had it before except it doesnt show the addaction also for any of the object. Could it be because _genfood is named after 4 objects? Thats what I did before in hopes that since it was server side, then it would just make a generic item for all.

Also roy, I tried adding the "" to different places, from outter to in basically while rechecking code for mishaps.

Share this post


Link to post
Share on other sites

im wondering if you guys are trying to be more advanced than actually needed for this simple task:

Also im wondering why first asigning _noObject to a house then reasigning it to bt25, then creating a global basket and using that, and why the public variable need??

Anyway, this below should work just fine... in theory at least.

try this:

if(isServer) then {
_allTheFood = [];  // array to collect all the foods in.
_nObject = position player nearestObject "Land_House_C_10_dam_EP1";

// create 4 food baskets, using createVehicle array instead.
{
	_genfood = createVehicle ["land_basket_ep1", getpos _nObject, [], 0, "NONE"];
	_genfood setPos (_nObject buildingpos (random 20));
	_allTheFood = _allTheFood + [_genFood];
} foreach [1,2,3,4];

//use spawn to create actions for all clients for all the ffood baskets, note that there is no if (isServer) in it, so its for all clients.
_action = _allTheFood spawn {
	{_x addAction[("<t color=""#FF0000"">" + ("Take Food!") +"</t>"),"example_scripts\take_food.sqf", [], 1, false, false, "", ""]} foreach _this;
};
};

Edit: and if still issue with JIP, run the script in a spawn from a 1 sec delayed trigger with a variable condition:

condition: onstart

on act:

_null = [] spawn {
  <code above>
};
onstart = false;

place onstart = true; in any non player object initline, in editor.

edit2: use this in a trigger with the above condition:

if (isServer) then {
_null = [] spawn {
	if (isServer) then {
		_allTheFood = [];
		_nObject = position player nearestObject "Land_House_C_10_dam_EP1";

		{
			_genfood = createVehicle ["land_basket_ep1", getpos _nObject, [], 0, "NONE"];
			_genfood setPos (_nObject buildingpos (random 20));
			_allTheFood = _allTheFood + [_genFood];
		} foreach [1,2,3,4];

		_action = _allTheFood spawn {
			{_x addAction[("<t color=""#FF0000"">" + ("Take Food!") +"</t>"),"example_scripts\take_food.sqf", [], 1, false, false, "", ""]} foreach _this;
		};
	};
};
};

it will make sure only server launches the creation spawn script, and then apply correct actions for each client, but now its maybe becoming clear to me that JIP will maybe not work, unless maybe actions is added to each playable units instead...

I dunno, JIP is deffinetly not a strong side of mine.

Edited by Demonized

Share this post


Link to post
Share on other sites

Hey Daimyo,

_genfood setVehicleInit  "this addAction[("<t color=""#FF0000"">" + ("Take Food!") +"</t>"),"example_scripts\take_food.sqf";"
processInitCommands 

Just after the vehicleInit part, everytime the use of " after the first time it will see the syntax as no longer part of the init field. by using "" it views it as part of the whole single init field. (if that makes any sense)

I havn't been able to test this yet but should look something like this:

_genfood setVehicleInit  "this addAction[""<t color='#FF0000'>Take Food!</t>"",""example_scripts\take_food.sqf""];";
processInitCommands;

this applies to any command inside another but the relavance is a bit redundant as shuko and PVP have provided a much better solution.

hope that helps

Share this post


Link to post
Share on other sites
im wondering if you guys are trying to be more advanced than actually needed for this simple task:

Also im wondering why first asigning _noObject to a house then reasigning it to bt25, then creating a global basket and using that, and why the public variable need??

Anyway, this below should work just fine... in theory at least.

try this:

if(isServer) then {
_allTheFood = [];  // array to collect all the foods in.
_nObject = position player nearestObject "Land_House_C_10_dam_EP1";

// create 4 food baskets, using createVehicle array instead.
{
	_genfood = createVehicle ["land_basket_ep1", getpos _nObject, [], 0, "NONE"];
	_genfood setPos (_nObject buildingpos (random 20));
	_allTheFood = _allTheFood + [_genFood];
} foreach [1,2,3,4];

//use spawn to create actions for all clients for all the ffood baskets, note that there is no if (isServer) in it, so its for all clients.
_action = _allTheFood spawn {
	{_x addAction[("<t color=""#FF0000"">" + ("Take Food!") +"</t>"),"example_scripts\take_food.sqf", [], 1, false, false, "", ""]} foreach _this;
};
};

Edit: and if still issue with JIP, run the script in a spawn from a 1 sec delayed trigger with a variable condition:

condition: onstart

on act:

_null = [] spawn {
  <code above>
};
onstart = false;

place onstart = true; in any non player object initline, in editor.

edit2: use this in a trigger with the above condition:

if (isServer) then {
_null = [] spawn {
	if (isServer) then {
		_allTheFood = [];
		_nObject = position player nearestObject "Land_House_C_10_dam_EP1";

		{
			_genfood = createVehicle ["land_basket_ep1", getpos _nObject, [], 0, "NONE"];
			_genfood setPos (_nObject buildingpos (random 20));
			_allTheFood = _allTheFood + [_genFood];
		} foreach [1,2,3,4];

		_action = _allTheFood spawn {
			{_x addAction[("<t color=""#FF0000"">" + ("Take Food!") +"</t>"),"example_scripts\take_food.sqf", [], 1, false, false, "", ""]} foreach _this;
		};
	};
};
};

it will make sure only server launches the creation spawn script, and then apply correct actions for each client, but now its maybe becoming clear to me that JIP will maybe not work, unless maybe actions is added to each playable units instead...

I dunno, JIP is deffinetly not a strong side of mine.

So I gave your code a try and tried and it didnt even show the addaction on startup, this is getting very frustrating.. Could I create a trigger in game that detect :

player == player && genfood = position _nobject nearestobject "land_basket_ep1";

with on act:

genfood addAction[("<t color=""#FF0000"">" + ("Take Food!") +"</t>"),"example_scripts\take_food.sqf", [], 1, false, false, "", ""]};

I figure I can just addaction by trigger in-game?

Share this post


Link to post
Share on other sites

well code works just fine, did test and setPosed myself on the position of the basket, actions there in red text.

the problem is the buildingpos or buildingname itself, its incorrect and ends up sending all baskets to location [0,0,0]

Edited by Demonized

Share this post


Link to post
Share on other sites

Well, running addaction just on server wont add anything to clients.

Share this post


Link to post
Share on other sites
Well, running addaction just on server wont add anything to clients.

addaction in a spawn would run that script snippet on all clients right?

meaning giving all clients actions.

Share this post


Link to post
Share on other sites

The spawn is only started on the server, so no.

Share this post


Link to post
Share on other sites

ok, im a little confused shuko.

So let me get this right, what you are saying is this: ?

if (isServer) then {
  hint "only server sees this";
  _null = [] spawn {
     hint "only server sees this as well, even though we did not use any isServer check inside the spawn";
  };
};

Share this post


Link to post
Share on other sites

You are calling it within a isserver block, so naturally only server runs the spawn command.

Share this post


Link to post
Share on other sites

alright, ive always figured spawn called a "fresh" script wich could be used for any client/server, no matter where it was called from.

So you could apply new isServer, isDedicated, isplayer etc checks and run codes correct for whatever client you needed inside the spawn.

Guess i was very wrong, this changes a few things...

Share this post


Link to post
Share on other sites

Call, spawn, execvm etc all respect IFs.

Share this post


Link to post
Share on other sites

Well, after trying shukos code on first page, the script worked once even with JIP and then I tried 5 more times and only the objects spawned without addaction...

How can JIP be this impossible.

All im doing is creating a dynamic building from the game.

Delcaring it as a building "Classname" so i can use buildingpos.

Then declaring the name I gave it in-game "bt25" as a backup.

so

_nobject =  bt25;

Then I spawn wicker baskets inside the building via Server side.

Then i run a client script:

if (!isNull player) then { call compile preprocessFileLineNumbers "initClient.sqf"; }; 

that then takes all baskets and says to addactions to them.

genfood1 = nearestObjects [nobject, ["land_basket_ep1"], 5000];
genfood1 addAction[("<t color=""#FF0000"">" + ("Take Food!") +"</t>"),"example_scripts\take_food.sqf", [], 1, false, false, "", ""];

EDIT * just for reference, Im starting my own dedicated up, connecting to it while persistent = 1, using shukos code on page1 and doing this at the top ^^

ALSO, the script works on server load/ first mission load/ Anytime I #reassign or #restart mission, it doesnt work anymore... WTH??

Edited by Daimyo21

Share this post


Link to post
Share on other sites

#restart hasnt worked ever, in the last 10 years of these games. Gotta use #missions.

PS. I did say I didnt test it, its just there to provide ideas. ;)

Share this post


Link to post
Share on other sites
#restart hasnt worked ever, in the last 10 years of these games. Gotta use #missions.

PS. I did say I didnt test it, its just there to provide ideas. ;)

#restart restarts the mission but its a bit buggy

Btw I know it wasnt tested, trust me ive been modifying the script and trying my own ideas. I just dont get why

genfood1 = nearestObjects [bt25, ["land_basket_ep1"], 5000];
genfood1 addAction[("<t color=""#FF0000"">" + ("Take Food!") +"</t>"),"example_scripts\take_food.sqf", [], 1, false, false, "", ""];

wouldnt always grab all Land_basket_ep1 baskets on the map and add the action to them via called on Client script...

Also why does it all work on FIRST server load tho when I change mission and change it back it suddenly doesnt work... doesnt make any sense since the init is ran every time the mission is loaded...

Share this post


Link to post
Share on other sites

Did a quick test and seemed to work nicely.

[] spawn {
 waituntil {!isnil "SHK_pv_foodAction"};
 {
   _x addAction[("<t color=""#FF0000"">" + ("Take Food!") +"</t>"),"example_scripts\take_food.sqf", [], 1, false, false, "", ""];
 } foreach SHK_pv_foodAction;
};

if(isServer) then { 
 _nObject = bt25;

 _baskets = [];

 for "_i" from 1 to 4 do {
   _genfood="land_basket_ep1" createvehicle getpos _nobject; 
   //_genfood setPos (_nobject buildingpos (random 20)); 
   _baskets set [count _baskets, _genfood];
 };
 SHK_pv_foodAction = _baskets;
 publicvariable "SHK_pv_foodAction";
};

Share this post


Link to post
Share on other sites
Did a quick test and seemed to work nicely.

[] spawn {
 waituntil {!isnil "SHK_pv_foodAction"};
 {
   _x addAction[("<t color=""#FF0000"">" + ("Take Food!") +"</t>"),"example_scripts\take_food.sqf", [], 1, false, false, "", ""];
 } foreach SHK_pv_foodAction;
};

if(isServer) then { 
 _nObject = bt25;

 _baskets = [];

 for "_i" from 1 to 4 do {
   _genfood="land_basket_ep1" createvehicle getpos _nobject; 
   //_genfood setPos (_nobject buildingpos (random 20)); 
   _baskets set [count _baskets, _genfood];
 };
 SHK_pv_foodAction = _baskets;
 publicvariable "SHK_pv_foodAction";
};

Works great so far! imma test it further with a friend in time to confirm any bugs.

Appreciate it Shuko, your the man.

Also on a side note, is it possible to claim multiple buildings as _one.

_allbluehouses = nearestObjects [player, ["HouseClassName"], 500];

Spawn 50 baskets in _allbluehouses ?

Just wondering.. not a big deal if its crazy complicated.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×