Matthew Nicholls Posted January 25, 2012 Share Posted January 25, 2012 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. Quote Link to comment Share on other sites More sharing options...
Rick Posted January 25, 2012 Share Posted January 25, 2012 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: C++: string test = "hello"; SendEntityMessage(ent, msg, 0, (byte*)test.c_str()); Quote Link to comment Share on other sites More sharing options...
Matthew Nicholls Posted January 25, 2012 Author Share Posted January 25, 2012 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; while(stuff){ SendEntityMessage(model, "move",(byte*)start); } delete[] start; I'm not great with C++ pointers so I have no idea what I'm doing lol Quote Link to comment Share on other sites More sharing options...
Rick Posted January 25, 2012 Share Posted January 25, 2012 I think in Lua that extra parameter can only be a string (remember reading a thread about that). So once you are able to send strings you'd have to send your number as a string and in Lua convert it to a number. Quote Link to comment Share on other sites More sharing options...
LordHippo Posted January 25, 2012 Share Posted January 25, 2012 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. Quote 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 More sharing options...
Pixel Perfect Posted January 25, 2012 Share Posted January 25, 2012 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! Quote 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 More sharing options...
Rick Posted January 25, 2012 Share Posted January 25, 2012 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. Quote Link to comment Share on other sites More sharing options...
Matthew Nicholls Posted January 25, 2012 Author Share Posted January 25, 2012 Thanks for the help guys. I think I'll have to make a longer string and divide it up when it gets to Lua. Or just make more separate messages. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.