Jump to content

Pass a string as a byte* ?

Matthew Nicholls

Recommended Posts

In lua you can use a string to pass the extra parameter to SendMessage()


Lua: entity:SendMessage( message, delay, extra )


but in C you have to passs a byte*


C: void SendEntityMessage( TEntity entity, str message, int delay=0, byte* extra=0 )


How can I pass in a string value in C++ so it can be used in the lua script of the model. I've been using it without the extra parameter but I would like to use it as well.

Link to comment
Share on other sites

You should be able to just cast whatever you are sending to (byte*). I'm not 100% sure of the conversion between C++ and Lua however. When everything is in C++ that type can be anything and casting it works.


Try something like:




string test = "hello";

SendEntityMessage(ent, msg, 0, (byte*)test.c_str());

Link to comment
Share on other sites

It compiles but I can't retrieve the string in lua it returns userdata type. I want to be able to send a int or string as I think it is a string the lua function takes. The value I want in the end is an int.

I tried to send an int like this;

   int myint = 1;
   int * start= new int;
   start = &myint;

  SendEntityMessage(model, "move",(byte*)start);
delete[] start;


I'm not great with C++ pointers so I have no idea what I'm doing lol

Link to comment
Share on other sites

Rick is right. You have to pass the extra parameter as string.

So in case you want to send an int, you have cast it ti string by using itoa, and then pass the string to SendEntityMessage.

Lua does these kind of conversions automatically, but in C you have to do them yourself.

Ali Salehi | Programmer


Intel Core i3 2100 @ 3.0GHz | GeForce GTS 450 | 4GB DDR3 RAM | Windows 7 Ultimate x64

LE 2.50 | Visual Studio 2010 | RenderMonkey 1.82 | gDEBugger 5.8 | FX Composer 2.5 | UU3D 3 | xNormal 3.17




Link to comment
Share on other sites

I don't think you will be able to use a byte pointer to return the string from C to Lua as Lua will simply regard this as a userdata type (equiv of a void* in C) and can do nothing with it other than allow it to be returned as a pointer to a subsequent C routine. There is no type casting in Lua as far as I'm aware. You could sent the value as a separated item tagged onto the message parameter and process those in your callback routine (e.g. 'move|1')


[EDIT] Ah, didn't see Lord Hippo's reply!

Intel Core i5 2.66 GHz, Asus P7P55D, 8Gb DDR3 RAM, GTX460 1Gb DDR5, Windows 7 (x64), LE Editor, GMax, 3DWS, UU3D Pro, Texture Maker Pro, Shader Map Pro. Development language: C/C++

Link to comment
Share on other sites

Another work around might be to use the message as a flag to indicate to read some entity key that you set in C for that entity. Basically telling Lua that you updated an entity key value attached to this entity.


Something like:


SetEntityKey(ent, "speed", "25");
SendEntityMessage(ent, "updated.speed");


Then in the lua entity message function look for "updated.speed" message and when you see it read the "speed" entity key from the model.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...